Created
February 8, 2023 00:30
-
-
Save Denchyaknow/ae8e4f78b705c16d8dd7397b57b699e8 to your computer and use it in GitHub Desktop.
ISerializationCallbackReceiver can help control how data is Serialized and Deserialized between app restarts. Here is some notes on using it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Make all reffs are already serialized that are to be used when OnAfterDeserialize() is called. | |
public bool IsReady { get => isInitialized && Cells.Count > 0; } | |
public Config_CellData Storage { get => cellDataStorage; } | |
public bool IsInitialized { get => isInitialized; private set => isInitialized = value; } | |
[SerializeField] | |
private bool isInitialized = false; | |
[SerializeField] | |
private Config_CellData cellDataStorage = null; | |
[SerializeField] | |
private GameManager gameSifu = null; | |
private MapManager mapSifu = null; | |
public List<CellData> Cells = new List<CellData>(); | |
public List<CellData> BossCells = new List<CellData>(); | |
public List<CellData> ElevatorCells = new List<CellData>(); | |
public List<CellData> TownCells = new List<CellData>(); | |
public void OnBeforeSerialize()//Called almost every dirty frame in Editor | |
{ | |
//SaveCellData(); or not. Do loading first before saving to see if its the result you want. | |
if (!isInitialized) | |
return; | |
Log(string.Format("CellManager: Begin Serialization for {0} Land, {1} Towns, {2} Elevators, {3} Bosses", | |
cellDataStorage.Mintable.Count, cellDataStorage.Towns.Count, cellDataStorage.Elevators.Count, cellDataStorage.Bosses.Count)); | |
} | |
public void OnAfterDeserialize()//This special function is called when Unity loads this asset for the first time | |
{ | |
gameSifu = GameManager.Instance;//Will not work, Instance uses FindGameObjectOfType and | |
if(gameSifu == null) | |
{ | |
Debug.LogError("Uhoh GameSifu Null, Tell Dencho QUICKLY!"); | |
return; | |
} | |
mapSifu = gameSifu.MapSifu; | |
if (mapSifu == null) | |
{ | |
Debug.LogError("Uhoh MapSifu Null, Tell Dencho QUICKLY!"); | |
return; | |
} | |
if (!mapSifu.IsInitialized) | |
mapSifu.InitializeGridSystem();//Populate the GridSystems List | |
GridSystems = mapSifu.GridSystems; | |
if (cellDataStorage == null) | |
return; | |
Log(string.Format("CellManager: Begin Deserialization for {0} Land, {1} Towns, {2} Elevators, {3} Bosses", | |
cellDataStorage.Mintable.Count, cellDataStorage.Towns.Count, cellDataStorage.Elevators.Count, cellDataStorage.Bosses.Count)); | |
mapSifu.Version = cellDataStorage.Version; | |
Cells = new List<CellData>(); | |
for (int i = 0; i < cellDataStorage.Mintable.Count; i++) | |
{ | |
Cells.Add(cellDataStorage.Mintable[i]); | |
} | |
TownCells = new List<CellData>(); | |
for (int i = 0; i < cellDataStorage.Towns.Count; i++) | |
{ | |
TownCells.Add(cellDataStorage.Towns[i]); | |
} | |
ElevatorCells = new List<CellData>(); | |
for (int i = 0; i < cellDataStorage.Elevators.Count; i++) | |
{ | |
ElevatorCells.Add(cellDataStorage.Elevators[i]); | |
} | |
BossCells = new List<CellData>(); | |
for (int i = 0; i < cellDataStorage.Bosses.Count; i++) | |
{ | |
BossCells.Add(cellDataStorage.Bosses[i]); | |
} | |
TownGroups = new List<TownGroup>(); | |
for (int i = 0; i < cellDataStorage.TownGroups.Count; i++) | |
{ | |
TownGroups.Add(cellDataStorage.TownGroups[i]); | |
} | |
IsInitialized = GridSystems.Count > 0 && Cells.Count > 0; | |
Debug.Log("CellManager Deserialized!"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment