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); }