示例#1
0
        public Unit CreateUnit(string name, Vector3 position, OwnerType owner = OwnerType.NONE)
        {
            UnitData unitData;

            if (!_unitsData.TryGetValue(name, out unitData))
            {
                throw new Exception("Requested unit of type '" + name + "' not available." +
                                    "Create a corresponding UnitData first.");
            }
            if (unitData.prefab.GetComponent <NavMeshAgent>())
            {
                NavMeshHit hit;
                if (NavMesh.SamplePosition(position, out hit, 1, NavMesh.AllAreas))
                {
                    position = hit.position;
                }
                else
                {
                    Debug.LogWarning("Couldn't sample a position for new unit " + name + " on the navmesh!");
                }
            }

            Unit unit = Object.Instantiate(unitData.prefab, position, Quaternion.identity);

            _createdUnits.Add(unit.GetInstanceID(), unit);
            _visibleUnits.AddLast(unit);
            unit.Initialize(unitData, owner);
            UnitCreated?.Invoke(unit);
            return(unit);
        }
示例#2
0
 public static IUnitData GetUnitData(UnitType type)
 {
     if (UnitDataDictionary.TryGetValue(type, out var data))
     {
         return(data);
     }
     data = GetNewUnitData(type);
     UnitDataDictionary[type] = data;
     return(data);
 }