Esempio n. 1
0
        public static TreeTemplate CreateFromFunctions(List <TreeFunctionAsset> functionsToCopy, string path)
        {
            List <TreeFunctionAsset> treeFunctions = new List <TreeFunctionAsset>();

            for (int i = 0; i < functionsToCopy.Count; i++)
            {
                TreeFunctionAsset function = Instantiate(functionsToCopy[i]);
                treeFunctions.Add(function);
            }

            for (int i = 0; i < functionsToCopy.Count; i++)
            {
                int parentIndex          = functionsToCopy.IndexOf(functionsToCopy[i].parent);
                TreeFunctionAsset parent = parentIndex == -1 ? null : treeFunctions[parentIndex];
                treeFunctions[i].Init(parent, true);
            }

            TreeTemplate template = ScriptableObject.CreateInstance <TreeTemplate>();

            template.treeFunctions = treeFunctions;

            AssetDatabase.CreateAsset(template, path);

            template = AssetDatabase.LoadAssetAtPath <TreeTemplate>(path);

            for (int i = 0; i < treeFunctions.Count; i++)
            {
                AssetDatabase.AddObjectToAsset(treeFunctions[i], template);
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            return(template);
        }
Esempio n. 2
0
        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();
        }
Esempio n. 3
0
        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;
        }
 private void OnEnable()
 {
     template = (TreeTemplate)target;
 }