static string StringFromLeafObject(object obj) { if (obj == null) { return(string.Empty); } if (obj.GetType().IsSubclassOf(typeof(Component))) { Component c = (Component)obj; if (c == null) // Component overrides the == operator, so we have to check { return(string.Empty); } return(ObjectTreeUtil.GetFullName(c.gameObject)); } if (obj.GetType().IsSubclassOf(typeof(GameObject))) { GameObject go = (GameObject)obj; if (go == null) // GameObject overrides the == operator, so we have to check { return(string.Empty); } return(ObjectTreeUtil.GetFullName(go)); } return(obj.ToString()); }
static void RestoreAllInterestingStates() { //Debug.Log("Updating state for all interesting objects"); bool dirty = false; GameObject[] roots = ObjectTreeUtil.FindAllRootObjectsInScene(); foreach (ObjectStateSaver saver in sSavedStates) { GameObject go = saver.FindSavedGameObject(roots); if (go != null) { Undo.RegisterFullObjectHierarchyUndo(go, "SaveDuringPlay"); if (saver.PutFieldValues(go, roots)) { //Debug.Log("SaveDuringPlay: updated settings of " + saver.ObjetFullPath); EditorUtility.SetDirty(go); dirty = true; } } } if (dirty) { UnityEditorInternal.InternalEditorUtility.RepaintAllViews(); } sSavedStates = null; }
static string StringFromLeafObject(object obj) { if (obj == null) { return(string.Empty); } if (typeof(Component).IsAssignableFrom(obj.GetType())) { Component c = (Component)obj; if (c == null) // Component overrides the == operator, so we have to check { return(string.Empty); } return(ObjectTreeUtil.GetFullName(c.gameObject)); } if (typeof(GameObject).IsAssignableFrom(obj.GetType())) { GameObject go = (GameObject)obj; if (go == null) // GameObject overrides the == operator, so we have to check { return(string.Empty); } return(ObjectTreeUtil.GetFullName(go)); } if (typeof(ScriptableObject).IsAssignableFrom(obj.GetType())) { return(AssetDatabase.GetAssetPath(obj as ScriptableObject)); } return(obj.ToString()); }
/// <summary> /// Recursively collect all the field values in the MonoBehaviours /// owned by this object and its descendants. The values are stored /// in an internal dictionary. /// </summary> public void CollectFieldValues(GameObject go) { mObjectFullPath = ObjectTreeUtil.GetFullName(go); GameObjectFieldScanner scanner = new GameObjectFieldScanner(); scanner.FilterField = FilterField; scanner.OnLeafField = (string fullName, Type type, ref object value) => { // Save the value in the dictionary mValues[fullName] = StringFromLeafObject(value); //Debug.Log(mObjectFullPath + "." + fullName + " = " + mValues[fullName]); return(false); }; scanner.ScanFields(go); }
/// <summary> /// Parse a string to generate an object. /// Only very limited primitive object types are supported. /// Enums, Vectors and most other structures are automatically supported, /// because the reflection system breaks them down into their primitive components. /// You can add more support here, as needed. /// </summary> static object LeafObjectFromString(Type type, string value, GameObject[] roots) { if (type == typeof(Single)) { return(float.Parse(value)); } if (type == typeof(Double)) { return(double.Parse(value)); } if (type == typeof(Boolean)) { return(Boolean.Parse(value)); } if (type == typeof(string)) { return(value); } if (type == typeof(Int32)) { return(Int32.Parse(value)); } if (type == typeof(UInt32)) { return(UInt32.Parse(value)); } if (typeof(Component).IsAssignableFrom(type)) { // Try to find the named game object GameObject go = ObjectTreeUtil.FindObjectFromFullName(value, roots); return((go != null) ? go.GetComponent(type) : null); } if (typeof(GameObject).IsAssignableFrom(type)) { // Try to find the named game object return(GameObject.Find(value)); } if (typeof(ScriptableObject).IsAssignableFrom(type)) { return(AssetDatabase.LoadAssetAtPath(value, type)); } return(null); }
/// Collect all relevant objects, active or not static Transform[] FindInterestingObjects() { List <Transform> objects = new List <Transform>(); MonoBehaviour[] everything = ObjectTreeUtil.FindAllBehavioursInScene <MonoBehaviour>(); foreach (var b in everything) { var attrs = b.GetType().GetCustomAttributes(true); foreach (var attr in attrs) { if (attr.GetType().Name.Contains("SaveDuringPlay")) { //Debug.Log("Found " + ObjectTreeUtil.GetFullName(b.gameObject) + " for hot-save"); objects.Add(b.transform); break; } } } return(objects.ToArray()); }
/// <summary> /// Parse a string to generate an object. /// Only very limited primitive object types are supported. /// Enums, Vectors and most other structures are automatically supported, /// because the reflection system breaks them down into their primitive components. /// You can add more support here, as needed. /// </summary> static object LeafObjectFromString(Type type, string value) { if (type == typeof(Single)) { return(float.Parse(value)); } if (type == typeof(Double)) { return(double.Parse(value)); } if (type == typeof(Boolean)) { return(Boolean.Parse(value)); } if (type == typeof(string)) { return(value); } if (type == typeof(Int32)) { return(Int32.Parse(value)); } if (type == typeof(UInt32)) { return(UInt32.Parse(value)); } if (type.IsSubclassOf(typeof(Component))) { // Try to find the named game object GameObject go = ObjectTreeUtil.FindObjectFromFullName(value); return((go != null) ? go.GetComponent(type) : null); } if (type.IsSubclassOf(typeof(GameObject))) { // Try to find the named game object return(GameObject.Find(value)); } return(null); }
/// <summary> /// Parse a string to generate an object. /// Only very limited primitive object types are supported. /// Enums, Vectors and most other structures are automatically supported, /// because the reflection system breaks them down into their primitive components. /// You can add more support here, as needed. /// </summary> private static object LeafObjectFromString(Type type, string value, GameObject[] roots) { if (type == typeof(float)) { return(float.Parse(value)); } if (type == typeof(double)) { return(double.Parse(value)); } if (type == typeof(bool)) { return(bool.Parse(value)); } if (type == typeof(string)) { return(value); } if (type == typeof(int)) { return(int.Parse(value)); } if (type == typeof(uint)) { return(uint.Parse(value)); } if (type.IsSubclassOf(typeof(Component))) { // Try to find the named game object var go = ObjectTreeUtil.FindObjectFromFullName(value, roots); return(go != null?go.GetComponent(type) : null); } if (type.IsSubclassOf(typeof(GameObject))) { return(GameObject.Find(value)); } return(null); }
public GameObject FindSavedGameObject(GameObject[] roots) { return(ObjectTreeUtil.FindObjectFromFullName(mObjectFullPath, roots)); }