public static void DeserializeUnit(GameObject unit, UnitSerializeInfo info) { BattleUnit asBattleUnit = unit.GetComponent <BattleUnit>(); asBattleUnit.InitialGridPosition = info.positionInGrid; asBattleUnit.team = info.team; /* * UnitAttributes unitAttributes = unit.GetComponent<UnitAttributes>(); * * foreach(AttributeValuePair p in info.attributes) * unitAttributes.AddAttribute(p); */ }
public static UnitSerializeInfo SerializeUnit(GameObject unit) { UnitSerializeInfo toReturn = new UnitSerializeInfo(); BattleUnit asBattleUnit = unit.GetComponent <BattleUnit>(); toReturn.positionInGrid = asBattleUnit.CurrentTile.PositionInGrid; toReturn.team = asBattleUnit.team; foreach (var a in unit.GetComponent <UnitAttributes>().Attributes) { toReturn.attributes.Add(a); } return(toReturn); }
protected override List <UnitSerializeInfo> GetUnitSerializeInfos() { List <UnitSerializeInfo> SerializedUnits = new List <UnitSerializeInfo>(); if (!PlayerPrefs.HasKey("ChosenUnits")) { Debug.Log("nope "); UnitSerializeInfo unitInfo = new UnitSerializeInfo { prefab = stanPrefab, team = UnitTeam.Player }; unitInfo.positionInGrid.x = 0; unitInfo.positionInGrid.y = 0; SerializedUnits.Add(unitInfo); } else { ChosenUnits chosenUnits = JsonUtility.FromJson <ChosenUnits>(PlayerPrefs.GetString("ChosenUnits")); Debug.Log("figth"); Debug.Log(PlayerPrefs.GetString("ChosenUnits")); Debug.Log(chosenUnits.units); for (int i = 0; i < chosenUnits.units.Count; i++) { GameObject nextPrefab = GetUnitPrefab(chosenUnits.units[i]); if (nextPrefab == null) { Debug.LogError("Prefab not defined for : " + chosenUnits.units[i]); continue; } UnitSerializeInfo unitInfo = new UnitSerializeInfo { prefab = nextPrefab, team = team }; unitInfo.positionInGrid.x = spawnColumn; unitInfo.positionInGrid.y = i; SerializedUnits.Add(unitInfo); } } return(SerializedUnits); }
private void SpawnUnits() { SerializedUnits = GetUnitSerializeInfos(); foreach (UnitSerializeInfo info in SerializedUnits) { GameObject lastObject = Instantiate(info.prefab, transform.position, Quaternion.identity); Units.Add(lastObject); UnitSerializeInfo.DeserializeUnit(lastObject, info); } Units.ForEach((GameObject u) => { u.GetComponent <BattleUnit>().MoveToStartPosition(); u.GetComponent <BattleUnit>().OnDeath.AddListener((BattleUnit b, BattleUnit d, int a) => { Units.Remove(b.gameObject); Destroy(b.gameObject); } ); }); }