public void resolveFields() { IdentifiedGameObject stored, identified; foreach (StatemapField field in definitions) { if (field.type == typeof(IdentifiedGameObject)) { stored = (IdentifiedGameObject)field.value; if (stored == null || String.IsNullOrEmpty(stored.guid)) { continue; } identified = StatemapDefinition.getHeldObject(stored.guid); if (identified == null) { Debug.LogError("Unable to resolve moron gameObject statemap field with guid '" + stored.guid + "'"); continue; } stored.gameObject = identified.gameObject; } } }
/// <summary> /// Ensures that the given GameObject is present in the scene, and has the appropriate invisible child indicating its presence. /// If the [obj] is a prefab, it will be instantiated silently in the scene and assigned a guid corrosponding to its asset guid. /// If the [obj] is in the scene, it will be ensured that it has the appropriate invisible child. /// In either case, the appropriate guid to use for the [obj] will be returned. /// If the [obj] could not be found in scene or assets, a blank string is returned. /// </summary> private static IdentifiedGameObject ensureGUID(GameObject obj) { IdentifiedGameObject identified; SerializedGameObject serializedMarker; String guid; // see if we have it tracked already foreach (IdentifiedGameObject val in SerializedGameObject.identifiedObjects.Values) { if (val.gameObject == obj) { return(val); } } // scene object? serializedMarker = obj.GetComponent <SerializedGameObject>(); if (serializedMarker != null) { guid = serializedMarker.guid; // make sure that if this is a prefab, an instantiated version exists in the scene. StatemapDefinition.getHeldObject(guid); // add to registry identified = new IdentifiedGameObject(guid, obj); SerializedGameObject.identifiedObjects[guid] = identified; return(identified); } // otherwise, check if it's from the asset database (if we're in the editor). identified = ensureAssetGUID(obj); if (identified != null) { return(identified); } // in the scene but just not marked yet. guid = GUID.Generate().ToString().Replace("-", ""); serializedMarker = obj.AddComponent <SerializedGameObject>(); serializedMarker.guid = guid; identified = new IdentifiedGameObject(guid, obj); SerializedGameObject.identifiedObjects[guid] = identified; return(identified); }
private static IdentifiedGameObject ensureAssetGUID(GameObject obj) { #if UNITY_EDITOR IdentifiedGameObject ret; GameObject instantiated; SerializedGameObject marker; String guid, path; path = AssetDatabase.GetAssetPath(obj); if (String.IsNullOrEmpty(path)) { return(null); } // this is definitely an asset, see if it's already invisibly in the scene. guid = AssetDatabase.AssetPathToGUID(path); ret = StatemapDefinition.getHeldObject(guid); if (ret != null) { return(ret); } // doesn't yet exist. Instantiate an invisible copy in the instance, make sure the guid is discoverable from the prefab. guid = GUID.Generate().ToString().Replace("-", ""); instantiated = (GameObject)GameObject.Instantiate(obj); instantiated.transform.SetParent(StatemapDefinition.getGUIDHolder().transform, false); marker = instantiated.AddComponent <SerializedGameObject>(); marker.guid = guid; instantiated.SetActive(false); ret = new IdentifiedGameObject(guid, obj); SerializedGameObject.identifiedObjects[guid] = ret; return(ret); #endif }
public static System.Object propertyField(String label, float labelWidth, Type type, System.Object value, float width) { IdentifiedGameObject stored, identified; GameObject obj; EditorGUILayout.BeginHorizontal(); if (type == null) { throw new ArgumentNullException("No type specified"); } try { EditorGUILayout.LabelField(label, GUILayout.Width(labelWidth)); if (type == typeof(bool)) { return(EditorGUILayout.Toggle((bool)value, GUILayout.Width(width))); } if (type == typeof(int)) { return(EditorGUILayout.IntField((int)value, GUILayout.Width(width))); } if (type == typeof(float)) { return(EditorGUILayout.FloatField((float)value, GUILayout.Width(width))); } if (type == typeof(String)) { return(EditorGUILayout.TextField((String)value, GUILayout.Width(width))); } if (type == typeof(Vector3)) { return(EditorGUILayout.Vector3Field("", (Vector3)value, GUILayout.Width(width))); } if (type == typeof(IdentifiedGameObject)) { stored = (IdentifiedGameObject)value; // empty (never-assigned) guid? if (stored == null || String.IsNullOrEmpty(stored.guid)) { obj = (GameObject)EditorGUILayout.ObjectField(null, typeof(GameObject), true, GUILayout.Width(width)); if (stored == null && obj != null) { return(new IdentifiedGameObject(null, obj)); } if (stored != null && obj != stored.gameObject) { return(new IdentifiedGameObject(null, obj)); } return(stored); } identified = StatemapDefinition.getHeldObject(stored.guid); stored.gameObject = (GameObject)EditorGUILayout.ObjectField((GameObject)identified.gameObject, typeof(GameObject), true, GUILayout.Width(width)); return(stored); } throw new ArgumentException("Cannot support fields of type " + type.Name); } finally { EditorGUILayout.EndHorizontal(); } }