public void BreakReferencesOn(AdvancedScriptableObject aso) { var references = GetReferencesOn(aso); if (references.Length > 0) { for (int i = 0; i < references.Length; i++) { var refData = references[i]; if (refData.BIsArrayType) { var field = refData.ReferencerASO.GetType().GetField(refData.ReferencingFieldName); var array = field.GetValue(refData.ReferencerASO) as Array; array.SetValue(null, refData.ArrayIndex); } else { var field = refData.ReferencerASO.GetType() .GetField(refData.ReferencingFieldName); field.SetValue(refData.ReferencerASO, null); } _referenceData.Remove(refData); } } }
public static void UpdateChildren(AdvancedScriptableObject source) { //Get children assets List <AdvancedScriptableObject> children = new List <AdvancedScriptableObject>(); for (int i = 0; i < source.ProtoChildren.Count; i++) { AdvancedScriptableObject aso = source.ProtoChildren[i]; if (aso != null) { children.Add(aso); } else { Debug.LogWarning("Null child reference found!"); } } if (EditorUtility.DisplayDialog("Update Children", "Overwrite values of all children?", "Confirm", "Cancel")) { for (int i = 0; i < children.Count; i++) { CloneData(children[i], source, children[i]); } } }
void SetToPrototype() { System.Type type = _aso.GetType(); string[] availablePrototypes = AssetDatabase.FindAssets(string.Format("t:{0}", type)); if (availablePrototypes.Length > 0) { //Get the actual names of each object List <AdvancedScriptableObject> assets = new List <AdvancedScriptableObject>(); for (int i = 0; i < availablePrototypes.Length; i++) { string path = AssetDatabase.GUIDToAssetPath(availablePrototypes[i]); AdvancedScriptableObject obj = AssetDatabase.LoadAssetAtPath(path, typeof(AdvancedScriptableObject)) as AdvancedScriptableObject; //Prevent parenting to self assets.Add(obj); } GenericMenu menu = new GenericMenu(); for (int i = 0; i < availablePrototypes.Length; i++) { GUIContent gc = new GUIContent(assets[i].name); menu.AddItem(gc, false, OnSelectedPrototype, availablePrototypes[i]); } menu.ShowAsContext(); } else { Debug.LogWarning("No suitable prototypes found"); } }
void CreateObjectSelected(object userdata) { if (userdata != null) { ObjectSelection objSel = (ObjectSelection)userdata; AdvancedScriptableObject newObj = ScriptableObject.CreateInstance(objSel.Selected) as AdvancedScriptableObject; if (newObj == null) { Debug.Log("New obj is null"); } newObj.name = objSel.Selected; newObj.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(newObj, parentASO); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(newObj)); AdvancedScriptableObject newASO = (AdvancedScriptableObject)newObj; SerializedObject sObjAso = new SerializedObject(newASO); sObjAso.FindProperty("_parentAsset").objectReferenceValue = parentASO; sObjAso.ApplyModifiedProperties(); if (objSel.Property == null) { Debug.Log("Property is null"); } AlterProperty(objSel.Property, newObj); EditorUtility.SetDirty(objSel.Property.serializedObject.targetObject); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } }
public static void Delete(AdvancedScriptableObject deleteFrom, AdvancedScriptableObject objProperty) { //Examines the object and retrieves properties var properties = objProperty.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); #region Iterate Through Properties //Iterates through it's properties for (int i = 0; i < properties.Length; i++) { var curProp = properties[i]; #region ArrayTypes if (curProp.FieldType.IsArray) { Array list = curProp.GetValue(objProperty) as Array; //Debug.Log("Array found"); if (list.Length > 0) { Type elType = curProp.GetValue(objProperty).GetType().GetElementType(); if (elType.IsSubclassOf(typeof(AdvancedScriptableObject))) { AdvancedScriptableObject[] newList = list as AdvancedScriptableObject[]; //Delete all elements for (int x = 0; x < list.Length; x++) { AdvancedScriptableObject element = (AdvancedScriptableObject)newList.GetValue(x); if (element != null && BShareAsset(deleteFrom, element)) { Delete(deleteFrom, element); MonoBehaviour.DestroyImmediate(element, true); } } curProp.SetValue(objProperty, newList); } } } #endregion if (!BRelevantType(curProp)) { continue; } //Ensures value is not null var val = (AdvancedScriptableObject)curProp.GetValue(objProperty); if (val != null) { if (BShareAsset(deleteFrom, val)) { Delete(deleteFrom, val); } } } #endregion MonoBehaviour.DestroyImmediate(objProperty, true); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }
void OnSelectedPrototype(object userData) { AdvancedScriptableObject obj = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath((string)userData), typeof(AdvancedScriptableObject)) as AdvancedScriptableObject; _parent = obj; serializedObject.ApplyModifiedProperties(); ParentChanged(); }
public ASOReferenceData(AdvancedScriptableObject referencingASO, AdvancedScriptableObject referencedASO, string referencingFieldName, int arrayIndex) { _referencingASO = referencingASO; _referencedASO = referencedASO; _referencingFieldName = referencingFieldName; _arrayIndex = arrayIndex; _bIsArrayType = true; }
void BreakTieToParent() { serializedObject.FindProperty("_protoParent").objectReferenceValue = null; _parent.ProtoChildren.Remove(_aso); new SerializedObject(_parent).ApplyModifiedProperties(); _parent = null; serializedObject.ApplyModifiedProperties(); }
public void RemoveReference(AdvancedScriptableObject referencer, AdvancedScriptableObject reference) { if (_referenceData.Count > 0) { var relevant = _referenceData.FindAll(x => x.ReferencerASO == referencer); var search = relevant.Find(x => x.ReferencedASO == reference); _referenceData.Remove(search); } }
public static bool BWillCreateCircleReference(AdvancedScriptableObject mainAsset, AdvancedScriptableObject referenceAttempt) { if (AssetDatabase.GetAssetPath(mainAsset) == AssetDatabase.GetAssetPath(referenceAttempt)) { return(true); } else { return(false); } }
public ASOReferenceData[] GetReferencesOn(AdvancedScriptableObject aso) { if (_referenceData.Count > 0) { var search = _referenceData.FindAll(x => x.ReferencerASO == aso); return(search.ToArray()); } else { return(new ASOReferenceData[0]); } }
public void RemoveReferencesOn(AdvancedScriptableObject aso) { var references = GetReferencesOn(aso); if (references.Length > 0) { foreach (var refData in references) { _referenceData.Remove(refData); } } }
private void OnEnable() { _aso = (AdvancedScriptableObject)target; _parent = _aso.ProtoParent; _backupParent = _parent; ASOManager.Me.VaildateData(); GetRelevantRefData(); CheckNullChildren(); if (!_bListsCreated) { SetupLists(); } }
public void AddReferenceData(AdvancedScriptableObject referencer, AdvancedScriptableObject referenced, string referencingFieldName, int arrayIndex) { if (_referenceData.Count > 0) { var relevant = _referenceData.FindAll(x => x.ReferencerASO == referencer); var search = relevant.Find(x => x.ReferencedASO == referenced); if (search == null) { _referenceData.Add(new ASOReferenceData(referencer, referenced, referencingFieldName, arrayIndex)); } } else { _referenceData.Add(new ASOReferenceData(referencer, referenced, referencingFieldName, arrayIndex)); } }
void List_Dropdown_OnSelected(object selection) { if (selection != null) { string objSel = (string)selection; if (objSel != "Empty") { var newObj = ScriptableObject.CreateInstance(objSel); if (newObj == null) { Debug.Log("New obj is null"); } newObj.name = objSel; newObj.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(newObj, _property.serializedObject.targetObject); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(newObj)); AdvancedScriptableObject aso = (AdvancedScriptableObject)newObj; SerializedObject sObjAso = new SerializedObject(aso); sObjAso.FindProperty("_parentAsset").objectReferenceValue = _property.serializedObject.targetObject; sObjAso.ApplyModifiedProperties(); //Add new array element _list.serializedProperty.arraySize++; var newEl = _list.serializedProperty.GetArrayElementAtIndex(_list.serializedProperty.arraySize - 1); newEl.objectReferenceValue = newObj; _property.serializedObject.ApplyModifiedProperties(); EditorUtility.SetDirty(_property.serializedObject.targetObject); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } else { _list.serializedProperty.arraySize++; SerializedProperty prop = _property.GetArrayElementAtIndex(_list.serializedProperty.arraySize - 1); prop.objectReferenceValue = null; _property.serializedObject.ApplyModifiedProperties(); } } }
void ParentChanged() { if (_parent == _aso) { Debug.LogError("Cannot set parent as self!"); _parent = _backupParent; } else { if (_backupParent != null && _backupParent.ProtoChildren.Contains(_aso)) { _backupParent.ProtoChildren.Remove(_aso); } _backupParent = _parent; serializedObject.FindProperty("_protoParent").objectReferenceValue = _parent; _parent.ProtoChildren.Add(_aso); } serializedObject.ApplyModifiedProperties(); }
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { foreach (string str in importedAssets) { Object obj = AssetDatabase.LoadAssetAtPath(str, typeof(Object)); if (obj != null) { if (obj.GetType().IsSubclassOf(typeof(AdvancedScriptableObject))) { //Check if there are existing references if so then no need to update. AdvancedScriptableObject aso = (AdvancedScriptableObject)obj; if (ASOManager.Me.GetReferencesOn(aso).Length == 0) { AdvancedScriptableObjectUtility.AddReferences(aso); } } } } }
public static void PasteASO(SerializedProperty pasteTo) { AdvancedScriptableObject newObj = ScriptableObject.Instantiate(ASOManager.Me.CopyBuffer); newObj.hideFlags = HideFlags.HideInHierarchy; newObj.name = ASOManager.Me.CopyBuffer.name; SerializedObject serObj = new SerializedObject(newObj); serObj.FindProperty("_parentAsset").objectReferenceValue = pasteTo.serializedObject.targetObject; serObj.ApplyModifiedProperties(); //Merge the object AssetDatabase.AddObjectToAsset(newObj, pasteTo.serializedObject.targetObject); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(newObj)); pasteTo.objectReferenceValue = newObj; EditorUtility.SetDirty(pasteTo.serializedObject.targetObject); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); CloneData((AdvancedScriptableObject)pasteTo.serializedObject.targetObject, ASOManager.Me.CopyBuffer, newObj); }
public void RemoveAllReferenceData(AdvancedScriptableObject aso) { if (_referenceData.Count > 0) { var referencers = _referenceData.FindAll(x => x.ReferencedASO == aso); if (referencers.Count > 0) { foreach (var refData in referencers) { _referenceData.Remove(refData); } } var referenced = _referenceData.FindAll(x => x.ReferencerASO == aso); if (referenced.Count > 0) { foreach (var refData in referenced) { _referenceData.Remove(refData); } } } }
/// <summary> /// Removes and deletes any embedded subassets. used recursively /// </summary> /// <param name="aSObj"></param> public static void CleanObject(AdvancedScriptableObject aSObj) { //Debug.Log("--------BEGIN CLEANING----------"); //Debug.Log(string.Format("Cleaning [{0}]",aSObj.name)); //Examines the object and retrieves properties var properties = aSObj.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); //Iterates through it's properties for (int i = 0; i < properties.Length; i++) { var curProp = properties[i]; if (BIsIgnoredField(curProp)) { continue; } if (curProp.FieldType.IsArray) { Array list = curProp.GetValue(aSObj) as Array; if (list != null && list.Length > 0) { Type elType = curProp.GetValue(aSObj).GetType().GetElementType(); if (elType.IsSubclassOf(typeof(AdvancedScriptableObject))) { AdvancedScriptableObject[] newList = list as AdvancedScriptableObject[]; //Delete all elements for (int x = 0; x < list.Length; x++) { AdvancedScriptableObject element = (AdvancedScriptableObject)newList.GetValue(x); if (element != null && BShareAsset(aSObj, element)) { Delete(aSObj, element); MonoBehaviour.DestroyImmediate(element, true); } else { ASOManager.Me.RemoveReference(aSObj, element); } //Null cur index newList.SetValue(null, x); } curProp.SetValue(aSObj, null); } else { curProp.SetValue(aSObj, null); } } } else { //Skips over irrelevant types if (!BRelevantType(curProp)) { continue; } //Ensures value is not null var val = (AdvancedScriptableObject)curProp.GetValue(aSObj); if (val != null) { if (BShareAsset(aSObj, val)) { Delete(aSObj, val); MonoBehaviour.DestroyImmediate(val, true); } else { ASOManager.Me.RemoveReference(aSObj, val); curProp.SetValue(aSObj, null); } } } } new SerializedObject(aSObj).ApplyModifiedProperties(); EditorUtility.SetDirty(aSObj); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); new SerializedObject(aSObj).UpdateIfRequiredOrScript(); EditorApplication.RepaintHierarchyWindow(); //Debug.Log("--------CLEANING FINISHED----------"); }
public static AdvancedScriptableObject MergeAll(AdvancedScriptableObject assetToMergeWith, AdvancedScriptableObject referencingObj, AdvancedScriptableObject toMerge, bool bInstanceMerging = false) { //Copies the object var toMergeCopy = MonoBehaviour.Instantiate(toMerge); toMergeCopy.name = toMerge.name; toMergeCopy.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(toMergeCopy, assetToMergeWith); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(toMergeCopy)); //Examines the object and retrieves properties var properties = toMerge.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); #region Iterate Through Properties //Iterates through it's properties for (int i = 0; i < properties.Length; i++) { var curProp = properties[i]; //Debug.Log(curProp.Name); if (curProp.Name == "_parentAsset") { //Debug.Log("Setting Parent"); curProp.SetValue(toMergeCopy, referencingObj); } if (curProp.FieldType.IsArray && curProp.FieldType.GetElementType().IsSubclassOf(typeof(AdvancedScriptableObject))) { #region Handle Arrays Array array = curProp.GetValue(toMerge) as Array; Array newArray = curProp.GetValue(toMergeCopy) as Array; for (int x = 0; x < array.Length; x++) { if (array.GetValue(x) != null) { if (!BShareAsset(assetToMergeWith, (AdvancedScriptableObject)array.GetValue(x))) { newArray.SetValue(MergeAll(assetToMergeWith, toMergeCopy, (AdvancedScriptableObject)array.GetValue(x), bInstanceMerging), x); } } } curProp.SetValue(toMergeCopy, newArray); #endregion } else { //Skips over irrelevant types if (!BRelevantType(curProp)) { continue; } //Ensures value is not null var val = (AdvancedScriptableObject)curProp.GetValue(toMerge); if (val != null) { if (!BShareAsset(assetToMergeWith, val)) { curProp.SetValue(toMergeCopy, MergeAll(assetToMergeWith, toMergeCopy, val, bInstanceMerging)); } } } } #endregion if (!bInstanceMerging) { AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(toMerge)); } else { ASOManager.Me.RemoveReference(assetToMergeWith, toMerge); } //update assetdatabase AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); return(toMergeCopy); }
List <AdvancedScriptableObject> GetParentChain(List <AdvancedScriptableObject> chain, AdvancedScriptableObject curObj) { if (curObj.ParentAsset != null) { chain.Add(curObj.ParentAsset); //continue chain return(GetParentChain(chain, curObj.ParentAsset)); } else { return(chain); } }
/// <summary> /// Run through all properties recursively searching for aso references and /// adds the references /// </summary> /// <param name="toUpdate"></param> public static void AddReferences(AdvancedScriptableObject toUpdate) { //Debug.Log("-----------Add References Begin---------"); //Debug.Log(string.Format("Adding references for {0}", toUpdate.name)); //Examines the object and retrieves properties var properties = toUpdate.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); #region Iterate Through Properties //Iterates through it's properties for (int i = 0; i < properties.Length; i++) { var curProp = properties[i]; //Debug.Log(curProp.Name); if (curProp.FieldType.IsArray && curProp.FieldType.GetElementType().IsSubclassOf(typeof(AdvancedScriptableObject))) { #region Handle Arrays Array array = curProp.GetValue(toUpdate) as Array; if (array != null && array.Length > 0) { for (int x = 0; x < array.Length; x++) { if (array.GetValue(x) != null) { AdvancedScriptableObject propASO = (AdvancedScriptableObject)array.GetValue(x); if (!BShareAsset(toUpdate, propASO)) { ASOManager.Me.AddReferenceData(toUpdate, propASO, curProp.Name, x); } else { //recursively enter to update child references. AddReferences(propASO); } } } } #endregion } else { if (curProp.Name == "_protoParent") { AdvancedScriptableObject parentASO = (AdvancedScriptableObject)curProp.GetValue(toUpdate); if (parentASO != null) { if (!parentASO.ProtoChildren.Contains(toUpdate)) { parentASO.ProtoChildren.Add(toUpdate); } } continue; } //Skips over irrelevant types if (!BRelevantType(curProp)) { continue; } //Ensures value is not null var val = (UnityEngine.Object)curProp.GetValue(toUpdate); if (val != null) { AdvancedScriptableObject propASO = (AdvancedScriptableObject)val; if (!BShareAsset(toUpdate, propASO)) { ASOManager.Me.AddReferenceData(toUpdate, propASO, curProp.Name); } else { AddReferences(propASO); } } } } #endregion AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); //Debug.Log("---------Add References End---------"); }
public override void OnInspectorGUI() { //If in isolated editing mode if (Selection.activeObject == serializedObject.targetObject) { if (_aso.ParentAsset != null) { var parentChain = GetParentChain(new List <AdvancedScriptableObject>(), _aso); EditorGUILayout.BeginVertical(EditorStyles.helpBox); EditorGUILayout.LabelField("Object Hierarchy"); EditorGUILayout.BeginHorizontal(); for (int i = parentChain.Count - 1; i > -1; i--) { if (GUILayout.Button(parentChain[i].name)) { Selection.activeObject = parentChain[i]; } GUILayout.Label("<"); //Create new line every 4 buttons if (i % 4 == 0) { EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); } } EditorGUILayout.EndVertical(); EditorGUILayout.EndHorizontal(); } } EditorGUILayout.BeginVertical(EditorStyles.helpBox); EditorGUILayout.BeginHorizontal(); EditorGUI.BeginChangeCheck(); _parent = EditorGUILayout.ObjectField("Prototype Parent", _parent, typeof(AdvancedScriptableObject), true) as AdvancedScriptableObject; if (EditorGUI.EndChangeCheck()) { ParentChanged(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUI.BeginDisabledGroup(_parent == null); if (GUILayout.Button("Apply")) { if (EditorUtility.DisplayDialog("Apply values to prototype", "Overwrite values of parent?", "Confirm", "Cancel")) { var parSerObj = new SerializedObject(_parent); AdvancedScriptableObjectUtility.CleanObject(_parent); //parSerObj.ApplyModifiedProperties(); //parSerObj.Update(); AdvancedScriptableObjectUtility.CloneData(_parent, _aso, _parent); //parSerObj.Update(); //parSerObj.ApplyModifiedProperties(); //AdvancedScriptableObjectUtility.CopyDataUnity(parSerObj, serializedObject, parSerObj); } } if (GUILayout.Button("Restore")) { if (EditorUtility.DisplayDialog("Restore to prototype values", "Overwrite values of this object to match prototype?", "Confirm", "Cancel")) { AdvancedScriptableObjectUtility.CleanObject(_aso); serializedObject.ApplyModifiedProperties(); AdvancedScriptableObjectUtility.CloneData(_aso, _parent, _aso); serializedObject.ApplyModifiedProperties(); EditorUtility.SetDirty(serializedObject.targetObject); serializedObject.UpdateIfRequiredOrScript(); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } } if (GUILayout.Button("Break")) { BreakTieToParent(); } EditorGUI.EndDisabledGroup(); if (GUILayout.Button("Clean")) { if (EditorUtility.DisplayDialog("Clean", "Delete all values of this object?", "Confirm", "Cancel")) { AdvancedScriptableObjectUtility.CleanObject(_aso); serializedObject.ApplyModifiedProperties(); serializedObject.UpdateIfRequiredOrScript(); } } if (GUILayout.Button("Alter")) { SetToPrototype(); } EditorGUILayout.EndHorizontal(); if (_aso != null) { EditorGUILayout.BeginHorizontal(); EditorGUI.BeginDisabledGroup(_aso.ProtoChildren.Count < 1); EditorGUILayout.LabelField(string.Format("Children:{0}", _aso.ProtoChildren.Count)); if (GUILayout.Button("Goto")) { GoToChild(); } if (GUILayout.Button("Update")) { AdvancedScriptableObjectUtility.UpdateChildren(_aso); } EditorGUI.EndDisabledGroup(); EditorGUILayout.EndHorizontal(); //Referencing EditorGUILayout.BeginHorizontal(); EditorGUI.BeginDisabledGroup(_relevantRefData == null || _relevantRefData.Length == 0); EditorGUILayout.LabelField(string.Format("Externally Referenced by:{0}", _relevantRefData.Length)); if (GUILayout.Button("GoTo")) { GoToReferencerMenu(); } EditorGUI.EndDisabledGroup(); EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndVertical(); //DrawDefaultInspector(); DrawBaseExcept(); }
public ASOReferenceData(AdvancedScriptableObject referencingASO, AdvancedScriptableObject referencedASO, string referencingFieldName) { _referencingASO = referencingASO; _referencedASO = referencedASO; _referencingFieldName = referencingFieldName; }
public static void CloneData(AdvancedScriptableObject destObj, AdvancedScriptableObject from, AdvancedScriptableObject to) { //Debug.Log("--------BEGIN COPYING----------"); //Examines the object and retrieves properties var properties = from.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); //Iterates through it's properties setting any non merged SObj data for (int i = 0; i < properties.Length; i++) { var curProp = properties[i]; if (BIsIgnoredField(curProp)) { continue; } //Debug.Log(string.Format("CurProp:{0}", curProp.Name)); if (curProp.FieldType.IsArray) { #region ArrayHandling Type elType = curProp.FieldType.GetElementType(); //Debug.Log(string.Format("ArrayElementType:{0}", elType.Name)); if (elType.IsSubclassOf(typeof(AdvancedScriptableObject))) { #region ASO Array Array fromList = curProp.GetValue(from) as Array; Array toList = Array.CreateInstance(curProp.FieldType.GetElementType(), fromList.Length); //Copy each element for (int x = 0; x < fromList.Length; x++) { AdvancedScriptableObject element = (AdvancedScriptableObject)fromList.GetValue(x); if (element != null) { //if element is merged to the asset it's being copied from. if (BShareAsset(from, element)) { var newObj = ScriptableObject.CreateInstance(element.GetType()) as AdvancedScriptableObject; newObj.hideFlags = HideFlags.HideInHierarchy; newObj.name = element.name; SerializedObject serObj = new SerializedObject(newObj); serObj.FindProperty("_parentAsset").objectReferenceValue = to; serObj.ApplyModifiedProperties(); //Merge the object AssetDatabase.AddObjectToAsset(newObj, destObj); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(newObj)); EditorUtility.SetDirty(destObj); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); //copy the data from the other object CloneData(destObj, element, newObj); toList.SetValue(newObj, x); new SerializedObject(to).ApplyModifiedProperties(); Debug.Log(string.Format("New ARray val:{0}", toList.GetValue(x))); } else { toList.SetValue(fromList.GetValue(x), x); ASOManager.Me.AddReferenceData(to, element, curProp.Name, x); new SerializedObject(to).ApplyModifiedProperties(); } } } //Array newArray = Array.ConvertAll(toList, x => (elType)x); curProp.SetValue(to, toList); new SerializedObject(to).ApplyModifiedProperties(); EditorUtility.SetDirty(to); EditorUtility.SetDirty(destObj); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); #endregion } else { #region Other Arrays Array fromList = curProp.GetValue(from) as Array; Array toList = Array.CreateInstance(curProp.FieldType.GetElementType(), fromList.Length); Array.Copy(fromList, toList, fromList.Length); curProp.SetValue(to, toList); #endregion } #endregion } else { #region Standard Properties //Ensures value is not null var valFrom = curProp.GetValue(from); //Debug.Log(string.Format("Field:[{0}] Value:[{1}]", curProp.Name,valFrom)); if (valFrom != null) { if (!valFrom.GetType().IsSubclassOf((typeof(AdvancedScriptableObject))) && valFrom.GetType() != typeof(AdvancedScriptableObject)) { //Debug.Log("Setting as reference"); curProp.SetValue(to, valFrom); } else { //If from var doesn't share asset with from object then set as reference if (!BShareAsset(from, (AdvancedScriptableObject)valFrom)) { curProp.SetValue(to, valFrom); ASOManager.Me.AddReferenceData(to, (AdvancedScriptableObject)valFrom, curProp.Name); } else { //Create a new object var asoFromVal = (AdvancedScriptableObject)valFrom; Debug.Log(string.Format("Type of element:{0}", asoFromVal.GetType())); var newObj = ScriptableObject.CreateInstance(asoFromVal.GetType()) as AdvancedScriptableObject; newObj.hideFlags = HideFlags.HideInHierarchy; newObj.name = asoFromVal.name; SerializedObject serObj = new SerializedObject(newObj); serObj.FindProperty("_parentAsset").objectReferenceValue = to; serObj.ApplyModifiedProperties(); //Merge the object AssetDatabase.AddObjectToAsset(newObj, destObj); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(newObj)); curProp.SetValue(to, newObj); EditorUtility.SetDirty(destObj); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); //copy the data from the other object CloneData(destObj, (AdvancedScriptableObject)valFrom, newObj); } } } #endregion } } new SerializedObject(to).ApplyModifiedProperties(); EditorUtility.SetDirty(to); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); //Debug.Log("--------COPYING FINISHED----------"); }
static bool BShareAsset(AdvancedScriptableObject a, AdvancedScriptableObject b) { return(AssetDatabase.GetAssetPath(a) == AssetDatabase.GetAssetPath(b)); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(position, label, property); float widthUnit = position.width / 10; EditorGUI.LabelField(new Rect(position.x, position.y, widthUnit * 2, EditorGUIUtility.singleLineHeight), property.displayName); parentASO = (AdvancedScriptableObject)property.serializedObject.targetObject; if (property.objectReferenceValue != null) { propertyASO = (AdvancedScriptableObject)property.objectReferenceValue; if (!BIsMerged(property)) { GUI.color = Color.green; if (GUI.Button(new Rect(position.x + (widthUnit * 3), position.y, widthUnit, EditorGUIUtility.singleLineHeight), "><")) { MergeMenu(property); } GUI.color = Color.white; if (GUI.Button(new Rect(position.x + (widthUnit * 4), position.y, widthUnit, EditorGUIUtility.singleLineHeight), "Null")) { NullASOProperty(property); } GUI.color = Color.white; //Disabled for control over reference handling. GUI.enabled = false; EditorGUI.PropertyField(new Rect(position.x + (widthUnit * 5), position.y, widthUnit * 5, EditorGUIUtility.singleLineHeight), property, GUIContent.none, true); GUI.enabled = true; } else { GUI.color = Color.cyan; if (GUI.Button(new Rect(position.x + (widthUnit * 3), position.y, widthUnit, EditorGUIUtility.singleLineHeight), "<>")) { UnmergeMenu(property); } GUI.color = Color.red; if (GUI.Button(new Rect(position.x + (widthUnit * 4), position.y, widthUnit, EditorGUIUtility.singleLineHeight), "X")) { Delete(property); } GUI.color = Color.white; if (GUI.Button(new Rect(position.x + (widthUnit * 5), position.y, widthUnit * 4, EditorGUIUtility.singleLineHeight), property.objectReferenceValue.name)) { EnterChildASO(property); } if (GUI.Button(new Rect(position.x + (widthUnit * 9), position.y, widthUnit, EditorGUIUtility.singleLineHeight), "C")) { AdvancedScriptableObjectUtility.CopyASO(property); } } } else { if (GUI.Button(new Rect(position.x + (widthUnit * 2), position.y, widthUnit * 2, EditorGUIUtility.singleLineHeight), "Create")) { CreateNew(property); } EditorGUI.BeginChangeCheck(); EditorGUI.PropertyField(new Rect(position.x + (widthUnit * 4), position.y, widthUnit * 5, EditorGUIUtility.singleLineHeight), property, GUIContent.none, true); if (EditorGUI.EndChangeCheck()) { if (AdvancedScriptableObjectUtility.BWillCreateCircleReference(parentASO, propertyASO)) { AlterProperty(property, null); Debug.LogError("Object reference will create circular reference. These are not supported."); } else { AlterProperty(property, (AdvancedScriptableObject)property.objectReferenceValue); AddReferenceData(property); } } if (AdvancedScriptableObjectUtility.BCanPaste(property)) { if (GUI.Button(new Rect(position.x + (widthUnit * 9), position.y, widthUnit, EditorGUIUtility.singleLineHeight), "P")) { AdvancedScriptableObjectUtility.PasteASO(property); } } else { GUI.color = Color.grey; GUI.Button(new Rect(position.x + (widthUnit * 9), position.y, widthUnit, EditorGUIUtility.singleLineHeight), "P"); GUI.color = Color.white; } } EditorGUI.EndProperty(); }
public static AdvancedScriptableObject UnmergeAll(AdvancedScriptableObject unmergeFrom, AdvancedScriptableObject toUnmerge) { //Copies the object var toUnmergeCopy = ScriptableObject.Instantiate(toUnmerge); toUnmergeCopy.name = toUnmerge.name; //create the Object at the same file location but not added to object string fileLoc = AssetDatabase.GetAssetPath(unmergeFrom); string fileName = Path.GetFileName(fileLoc); string folderLoc = fileLoc.Remove(fileLoc.Length - fileName.Length); string uniquePath = AssetDatabase.GenerateUniqueAssetPath(folderLoc + toUnmergeCopy.name + ".asset"); AssetDatabase.CreateAsset(toUnmergeCopy, uniquePath); //Examines the object and retrieves properties var properties = toUnmerge.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); #region Iterate Through Properties //Iterates through it's properties for (int i = 0; i < properties.Length; i++) { var curProp = properties[i]; //Debug.Log(curProp.Name); if (curProp.Name == "_parentAsset") { //Debug.Log("Setting Parent"); curProp.SetValue(toUnmergeCopy, null); } if (curProp.FieldType.IsArray && curProp.FieldType.GetElementType().IsSubclassOf(typeof(AdvancedScriptableObject))) { #region Handle Arrays Array array = curProp.GetValue(toUnmerge) as Array; Array newArray = curProp.GetValue(toUnmergeCopy) as Array; for (int x = 0; x < array.Length; x++) { AdvancedScriptableObject arrayVal = (AdvancedScriptableObject)array.GetValue(x); if (arrayVal != null) { if (BShareAsset(unmergeFrom, arrayVal)) { AdvancedScriptableObject unmergedPropVal = UnmergeAll(unmergeFrom, arrayVal); newArray.SetValue(unmergedPropVal, x); //Set up reference cachedata ASOManager.Me.AddReferenceData(toUnmergeCopy, unmergedPropVal, curProp.Name, x); } } } curProp.SetValue(toUnmergeCopy, newArray); #endregion } else { //Skips over irrelevant types if (!BRelevantType(curProp)) { continue; } //Ensures value is not null var val = (AdvancedScriptableObject)curProp.GetValue(toUnmerge); if (val != null) { if (BShareAsset(unmergeFrom, val)) { //Remerge value along with children AdvancedScriptableObject unmergedPropVal = UnmergeAll(unmergeFrom, val); curProp.SetValue(toUnmergeCopy, unmergedPropVal); ASOManager.Me.AddReferenceData(toUnmergeCopy, unmergedPropVal, curProp.Name); } } } } #endregion MonoBehaviour.DestroyImmediate(toUnmerge, true); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(toUnmergeCopy)); //update assetdatabase AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); return(toUnmergeCopy); }
void AlterProperty(SerializedProperty property, AdvancedScriptableObject deltaASO) { propertyASO = deltaASO; property.objectReferenceValue = deltaASO; property.serializedObject.ApplyModifiedProperties(); }