/// <summary> /// Tries to save the existing game object ids and also calls the persistent /// system save call to ensure game object to the PCF bindings are saved. /// </summary> void TrySave() { if (_state == State.SaveRequired) { _state = State.Done; Debug.LogFormat("Saving Objects {0}", _exampleObjects.Count); if (!MLPersistentStore.IsStarted) { Debug.LogError("MLPersistentStore is not started! can't save. "); return; } if (_exampleObjects.Count > 0) { ObjIds record = new ObjIds(); record.Ids = new string[_exampleObjects.Count]; int i = 0; foreach (var someObj in _exampleObjects) { record.Ids[i++] = someObj.GO.name; //Update the binding (re-store offsets) before saving someObj.Binding.Update(); Debug.Log("saving binding for: " + someObj.Binding.GameObject.name); MLPersistentStore.Save(someObj.Binding); } string jsonString = JsonUtility.ToJson(record); File.WriteAllText(_filePath, jsonString); } SetProgress(string.Format(TEXT_SAVE_COMPLETE, _exampleObjects.Count)); } }
/// <summary> /// Reads all stored game object ids. /// </summary> void ReadAllStoredObjects() { if (File.Exists(_filePath)) { Debug.LogFormat("Found file {0}", _filePath); using (var source = new StreamReader(_filePath)) { string fileContents = source.ReadToEnd(); if (!string.IsNullOrEmpty(fileContents)) { ObjIds ids = JsonUtility.FromJson <ObjIds>(fileContents); if (ids.Ids != null && ids.Ids.Length > 0) { foreach (string id in ids.Ids) { Debug.LogFormat("Found : {0} in file", id); ExampleObject exampleObj = new ExampleObject(); exampleObj.GO = Instantiate(_goPrefab, Vector3.zero, Quaternion.identity); exampleObj.GO.name = id; exampleObj.GO.SetActive(false); _exampleObjects.Add(exampleObj); } } } _state = State.StartRestore; } } }