private void DisplayTemplates() { EditorGUIUtility.labelWidth = 60; EditorGUILayout.BeginVertical(EditorStyles.helpBox); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(serializedObject.FindProperty("template"), new GUIContent("Template"), true); serializedObject.ApplyModifiedProperties(); string SaveButton = "Save Template"; if (tree.template != null) { SaveButton = "Override"; } else { SaveButton = "Save"; } EditorGUI.BeginDisabledGroup(tree.template == null); if (GUILayout.Button("Load Template")) { serializedObject.FindProperty("selectedFunctionIndex").intValue = 0; serializedObject.FindProperty("template").objectReferenceValue = tree.template; serializedObject.ApplyModifiedProperties(); tree.FromTemplate(); UpdateTree(); } EditorGUI.EndDisabledGroup(); if (GUILayout.Button(SaveButton + " Template")) { if (SaveButton == "Override") { string templatePath = AssetDatabase.GetAssetPath(tree.template); AssetDatabase.DeleteAsset(templatePath); tree.template = TreeTemplate.CreateFromFunctions(tree.treeFunctionsAssets, templatePath); serializedObject.FindProperty("template").objectReferenceValue = tree.template; serializedObject.ApplyModifiedProperties(); } if (SaveButton == "Save") { string templatePath = EditorUtility.SaveFilePanelInProject("Save Template", tree.name + ".asset", "asset", ""); if (templatePath.Length > 0) { tree.template = TreeTemplate.CreateFromFunctions(tree.treeFunctionsAssets, templatePath); serializedObject.FindProperty("template").objectReferenceValue = tree.template; serializedObject.ApplyModifiedPropertiesWithoutUndo(); } } } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); }
public static void OnCreatePrefab(GameObject instance) { if (instance.GetComponent <MtreeComponent>() == null) { return; } #if UNITY_2018_3_OR_NEWER string prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(instance); #else string prefabPath = AssetDatabase.GetAssetPath(PrefabUtility.GetPrefabParent(instance)); #endif MtreeComponent[] trees = (MtreeComponent[])GameObject.FindObjectsOfType(typeof(MtreeComponent)); MtreeComponent originTree = null; foreach (MtreeComponent tree in trees) { #if UNITY_2018_3_OR_NEWER bool isInstance = PrefabUtility.GetPrefabInstanceStatus(tree) == PrefabInstanceStatus.Connected; string parentPrefabPath = AssetDatabase.GetAssetPath(PrefabUtility.GetCorrespondingObjectFromSource(tree)); #else bool isInstance = PrefabUtility.GetPrefabType(tree) == PrefabType.PrefabInstance; string parentPrefabPath = AssetDatabase.GetAssetPath(PrefabUtility.GetPrefabParent(tree)); #endif if (isInstance && parentPrefabPath == prefabPath) { #if UNITY_2018_3_OR_NEWER PrefabUtility.UnpackPrefabInstance(tree.gameObject, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction); #else PrefabUtility.DisconnectPrefabInstance(tree); #endif originTree = tree; } } #if UNITY_2018_3_OR_NEWER // Unity 2017 crashes when deleting prefab at this stage. This part is therefore also done in the MtreeComponent editor AssetDatabase.DeleteAsset(prefabPath); AssetDatabase.Refresh(); #endif if (originTree == null) { return; } string templatePath = AssetDatabase.GenerateUniqueAssetPath(Path.GetDirectoryName(prefabPath) + "/" + originTree.name + ".asset"); TreeTemplate template = TreeTemplate.CreateFromFunctions(originTree.treeFunctionsAssets, templatePath); originTree.template = template; }