private static void Validate(string path) { if (string.IsNullOrEmpty(path)) { return; } UnityEngine.Object[] components = AssetDatabase.LoadAllAssetsAtPath(path); for (int i = components.Length - 1; i >= 0; i--) { ScriptableComponent component = components[i] as ScriptableComponent; if (component == null) { continue; } tested.Clear(); component.hideFlags = HideFlags.HideInInspector | HideFlags.HideInHierarchy; if (component.Owner == null || !component.Owner || !Referenced(component.Owner, component)) { Debug.Log("Destroying " + components[i].ToString() + " because it is orphant."); component.Erase(); continue; } } }
/// <summary> /// Create an instance of type /// Works with values, list, array, scriptableObject, string /// Components are ignored. /// /// The owner MonoBeahviour is used in case of the creation of a ComponentMonoBehaviour for its internal binding. /// </summary> public static object CreateInstance(Type type, UnityEngine.Object owner = null) { if (typeof(GameObject).IsAssignableFrom(type)) { return(null); } else if (typeof(ComponentMonoBehaviour).IsAssignableFrom(type) && owner != null && owner is MonoBehaviour) { ComponentMonoBehaviour component = ((MonoBehaviour)owner).gameObject.AddComponent(type) as ComponentMonoBehaviour; component.Owner = owner as MonoBehaviour; return(component); } else if (typeof(ScriptableComponent).IsAssignableFrom(type) && owner != null && owner is ScriptableObject) { ScriptableComponent component = ScriptableObject.CreateInstance(type) as ScriptableComponent; component.Owner = owner as ScriptableObject; string path = AssetDatabase.GetAssetPath(owner); if (string.IsNullOrEmpty(path)) { return(null); } AssetDatabase.AddObjectToAsset(component, path); return(component); } else if (typeof(Component).IsAssignableFrom(type)) { if (owner is GameObject) { return(((GameObject)owner).AddComponent(type)); } return(null); } else if (typeof(ScriptableObject).IsAssignableFrom(type)) { return(null); } else if (typeof(UnityEngine.Object).IsAssignableFrom(type)) { return(null); } else if (type.IsArray) { return(Array.CreateInstance(type.GetElementType(), 0)); } else if (type == typeof(string)) { return(""); } else { return(Activator.CreateInstance(type, true)); } }
/// <summary> /// Called when the inspector is about to destroy this one. /// Loop in all the internal and destroy sub-components. /// </summary> public void Erase() { foreach (FieldInfo info in TypeUtility.GetFields(GetType())) { object value = info.GetValue(this); if (value is ScriptableComponent) { ScriptableComponent component = value as ScriptableComponent; if (component.Owner == Owner) { component.Erase(); } } } DestroyImmediate(this, true); }
private static ScriptableComponent CopyComponent(ScriptableObject owner, ScriptableComponent original) { Type type = original.GetType(); ScriptableComponent copy = original.Instantiate(); foreach (FieldInfo info in TypeUtility.GetFields(type)) { if (info.IsLiteral) { continue; } info.SetValue(copy, CopyObject(copy, info.GetValue(original))); } copy.Owner = owner; return(copy); }
private static bool Referenced(object owner, ScriptableComponent target) { if (owner == null || tested.Contains(owner)) { return(false); } else { tested.Add(owner); } Type type = owner.GetType(); foreach (FieldInfo info in Clipboard.GetFields(type)) { if (typeof(UnityEngine.Object).IsAssignableFrom(info.FieldType) && !info.FieldType.IsAssignableFrom(target.GetType())) { continue; } object value = info.GetValue(owner); if (typeof(IList).IsAssignableFrom(info.FieldType)) { IList list = value as IList; if (list != null) { foreach (object item in list) { if (ReferenceEquals(item, target)) { return(true); } if (item != null && item.GetType().IsClass&& Referenced(item, target)) { return(true); } } } } else if (typeof(IDictionary).IsAssignableFrom(info.FieldType)) { IDictionary dictionary = value as IDictionary; if (dictionary != null) { foreach (DictionaryEntry entry in dictionary) { if (ReferenceEquals(entry.Key, target)) { return(true); } if (ReferenceEquals(entry.Value, target)) { return(true); } if (entry.Key != null && entry.Key.GetType().IsClass&& Referenced(entry.Key, target)) { return(true); } if (entry.Value != null && entry.Value.GetType().IsClass&& Referenced(entry.Value, target)) { return(true); } } } } else if (info.FieldType.Namespace == "System" || info.FieldType.Namespace == "UnityEngine") { continue; } else if (info.FieldType.IsAssignableFrom(target.GetType())) { if (ReferenceEquals(value, target)) { return(true); } } else if (info.FieldType.IsClass && Referenced(value, target)) { return(true); } } return(false); }