protected void RestoreMotileObjects(GameState gs, string name) { foreach (KeyValuePair <string, RestorableData> kvp in gs.MotileObjectState) { try { DynamicRestorableData rd = kvp.Value as DynamicRestorableData; if (rd == null) { Debug.LogError("Local object " + kvp.Key + " has invalid data!"); } //is it in this scene string objectSceneName = rd.Scene; if (objectSceneName == name) { //we have a match! try { Transform t = transform.FindDeepChildIgnorePlaceholders(kvp.Key); GameObject go = null; if (t == null) { go = Instantiate(CoreUtils.LoadResource <GameObject>("Entities/" + rd.FormID), transform) as GameObject; } else { go = t.gameObject; } //this *should* work but hasn't been tested { go.name = kvp.Key; MotileRestorableComponent mrc = go.GetComponent <MotileRestorableComponent>(); if (mrc != null) { mrc.Restore(rd); } else { Debug.LogWarning("Motile object " + go.name + " has no restorable component!"); } } } catch (ArgumentException) { Debug.LogWarning("Tried to spawn " + rd.FormID + " but couldn't find prefab!"); } } } catch (Exception e) { Debug.LogError("Failed to restore an object!"); Debug.LogException(e); } } }
public override void Restore(RestorableData rdata) { DynamicRestorableData data = rdata as DynamicRestorableData; if (data == null) { Debug.LogError("Object " + name + " has invalid data!"); return; } BaseController controller = GetComponent <BaseController>(); if (!controller) { Debug.LogWarning("Object " + name + " has no controller!"); } //restore object properties gameObject.SetActive(data.Active); if (controller) { controller.SetVisibility(data.Visible); //controller.Tags = new HashSet<string>(data.Tags); controller.Tags.Clear(); controller.Tags.UnionWith(data.Tags); if (controller.FormID != data.FormID) { Debug.LogWarning(string.Format("Saved form ID does not match (saved:{0} , object: {1})", data.FormID, controller.FormID)); } } //restore transform transform.position = data.Position; transform.rotation = data.Rotation; transform.localScale = data.Scale; //restore rigidbody Rigidbody rigidbody = GetComponent <Rigidbody>(); if (rigidbody) { rigidbody.velocity = data.Velocity; rigidbody.angularVelocity = data.AngularVelocity; rigidbody.mass = data.Mass; rigidbody.isKinematic = data.IsKinematic; } else { //eh, do nothing } //restore extradata if (controller) { controller.RestoreEntityData(data.ExtraData); } }
public override RestorableData Save() { DynamicRestorableData data = new DynamicRestorableData(); BaseController controller = GetComponent <BaseController>(); if (!controller) { Debug.LogWarning("Object " + name + " has no controller!"); } //save object properties data.Active = gameObject.activeSelf; data.Scene = SceneManager.GetActiveScene().name; if (controller) { data.Visible = controller.GetVisibility(); data.Tags = new List <string>(controller.Tags).ToArray(); data.FormID = controller.FormID; } else { data.Visible = true; } //save transform data.Position = transform.position; data.Rotation = transform.rotation; data.Scale = transform.localScale; //save rigidbody if existant Rigidbody rigidbody = GetComponent <Rigidbody>(); if (rigidbody) { data.Velocity = rigidbody.velocity; data.AngularVelocity = rigidbody.angularVelocity; data.Mass = rigidbody.mass; data.IsKinematic = rigidbody.isKinematic; } else { //eh, do nothing } //save extra data if (controller) { data.ExtraData = controller.CommitEntityData(); } return(data); }
private void RestoreLocalObject(KeyValuePair <string, RestorableData> kvp) { DynamicRestorableData rd = kvp.Value as DynamicRestorableData; Transform t = transform.FindDeepChildIgnorePlaceholders(kvp.Key); if (t != null) { GameObject go = t.gameObject; //if it exists, restore it LocalRestorableComponent rc = go.GetComponent <LocalRestorableComponent>(); if (rc != null) { rc.Restore(rd); } else { Debug.LogWarning("Local object " + go.name + " has no restorable component!"); } } else { //if it doesn't, create it try { GameObject go = Instantiate(CoreUtils.LoadResource <GameObject>("Entities/" + rd.FormID), transform) as GameObject; if (go != null) { go.name = kvp.Key; LocalRestorableComponent rc = go.GetComponent <LocalRestorableComponent>(); if (rc != null) { rc.Restore(rd); } else { Debug.LogWarning("Local object " + go.name + " has no restorable component!"); } } } catch (ArgumentException) { Debug.LogWarning("Tried to spawn " + rd.FormID + " but couldn't find prefab!"); } } }