void _SerializeToJson() { var info = new MechanicDoorSaveInfo(); var switchList = new List <Switch>(); door = GetComponent <MechanicDoor>(); signalReceiver = GetComponent <SwitchSignalReceiver>(); if (signalReceiver && door) { info.open = door.IsOpen; info.allowEnter = door.IsAllowEnter; info.useInstantOpen = door.IsUseInstantOpen; foreach (var obj in signalReceiver.Switches) { if (obj) { switchList.Add(obj); } } info.switchStates = new bool[switchList.Count]; for (int i = 0; i < switchList.Count; i++) { info.switchStates[i] = switchList[i].IsTurnOn; } } json = JsonConvert.SerializeObject(info, Formatting.None); }
void _DeserializeToInfo(string json) { var info = JsonConvert.DeserializeObject <MechanicDoorSaveInfo>(json); door = GetComponent <MechanicDoor>(); signalReceiver = GetComponent <SwitchSignalReceiver>(); if (signalReceiver && door) { door.IsOpen = info.open; door.IsAllowEnter = info.allowEnter; door.IsUseInstantOpen = info.useInstantOpen; for (int i = 0; i < info.switchStates.Length; i++) { signalReceiver.Switches[i].IsTurnOn = info.switchStates[i]; } } }
protected override void Awake() { base.Awake(); signalReceiver = GetComponent <SwitchSignalReceiver>(); }