https://www.youtube.com/ watch?v=724qdit375U&feature=youtu.be
Код: Выделить всё
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CircleCollider2D))]
public class PlaceMonster : MonoBehaviour {
public GameObject monsterPrefab;
private GameObject monster;
private GameManagerBehavior gameManager;
void Start ()
{
gameManager = GameObject.Find("GameManager").GetComponent();
}
void Update ()
{
}
private bool canPlaceMonster()
{
int cost = monsterPrefab.GetComponent ().levels[0].cost;
return monster == null && gameManager.Gold >= cost;
}
void OnMouseUp ()
{
if (canPlaceMonster ())
{
monster = (GameObject) Instantiate(monsterPrefab, transform.position, Quaternion.identity);
gameManager.Gold -= monster.GetComponent().CurrentLevel.cost;
}
else if (canUpgradeMonster())
{
monster.GetComponent().increaseLevel();
gameManager.Gold -= monster.GetComponent().CurrentLevel.cost;
}
}
private bool canUpgradeMonster()
{
if (monster != null)
{
MonsterData monsterData = monster.GetComponent ();
MonsterLevel nextLevel = monsterData.getNextLevel();
if (nextLevel != null) {
return gameManager.Gold >= nextLevel.cost;
}
}
return false;
}
}
Код: Выделить всё
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class MonsterLevel {
public int cost;
public GameObject visualization;
public GameObject bullet;
public float fireRate;
}
public class MonsterData : MonoBehaviour {
public List levels;
private MonsterLevel currentLevel;
public MonsterLevel CurrentLevel {
get {
return currentLevel;
}
set {
currentLevel = value;
int currentLevelIndex = levels.IndexOf(currentLevel);
GameObject levelVisualization = levels[currentLevelIndex].visualization;
for (int i = 0; i < levels.Count; i++) {
if (levelVisualization != null) {
if (i == currentLevelIndex) {
levels[i].visualization.SetActive(true);
} else {
levels[i].visualization.SetActive(false);
}
}
}
}
}
void OnEnable() {
CurrentLevel = levels[0];
}
public MonsterLevel getNextLevel() {
int currentLevelIndex = levels.IndexOf (currentLevel);
int maxLevelIndex = levels.Count - 1;
if (currentLevelIndex < maxLevelIndex) {
return levels[currentLevelIndex+1];
} else {
return null;
}
}
public void increaseLevel() {
int currentLevelIndex = levels.IndexOf(currentLevel);
if (currentLevelIndex < levels.Count - 1) {
CurrentLevel = levels[currentLevelIndex + 1];
}
}
}
В основном мои места, в которых размещаются эти вещи иногда работает, иногда нет.
Что сработает, зависит от случая. Иногда мне удается повысить 3 монстра до максимального уровня, иногда — 7 до уровня 3, а остальные вообще не появляются. Я понятия не имею, почему это вызывает это. Я создал слои и много экспериментировал с настройками Eit> Projects > Physics 2D, чтобы объекты НЕ взаимодействовали друг с другом. Я пробовал много комбинаций вещей, но ничего не помогло.
Подробнее здесь: https://stackoverflow.com/questions/359 ... consistent