예제 #1
0
        private void LeaveEditMode()
        {
            Undo.RegisterCompleteObjectUndo(script, "Edit Off");

            //if possible, combine submeshes
            CheckCombine();

            //get all mesh filters
            MeshFilter[] meshFilters = script.GetComponentsInChildren <MeshFilter>();
            Undo.RecordObject(meshFilters[0], "Edit Off");

            //let the script combine them
            script.Combine();

            for (int i = 1; i < meshFilters.Length; i++)
            {
                Undo.DestroyObjectImmediate(meshFilters[i].gameObject);
            }

            placing = false;
        }
예제 #2
0
        /// <summary>
        /// Custom inspector override for buttons.
        /// </summary>
        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();
            EditorGUILayout.Space();

            //display current count of placed vertices in the active submesh
            EditorGUILayout.LabelField("Current Vertices: " + script.current.Count);

            //enter placement mode
            if (!placing && GUILayout.Button("编辑模式:关闭"))
            {
                Undo.RegisterCompleteObjectUndo(script, "Edit On");

                //if possible, create new submesh
                if (CheckSubMesh())
                {
                    GameObject newObj = script.CreateSubMesh();
                    Undo.RegisterCreatedObjectUndo(newObj, "Edit On");
                }

                //clear handle selections
                selected.Clear();
                placing = true;
            }

            GUI.color = Color.yellow;

            //leave placement mode and try to combine submeshes
            if (placing && GUILayout.Button("编辑模式:开启"))
            {
                Undo.RegisterCompleteObjectUndo(script, "Edit Off");

                //if possible, combine submeshes
                if (CheckCombine())
                {
                    //get all mesh filters
                    MeshFilter[] meshFilters = script.GetComponentsInChildren <MeshFilter>();
                    Undo.RecordObject(meshFilters[0], "Edit Off");

                    //let the script combine them
                    script.Combine();

                    for (int i = 1; i < meshFilters.Length; i++)
                    {
                        Undo.DestroyObjectImmediate(meshFilters[i].gameObject);
                    }
                }

                placing = false;
            }

            GUI.color = Color.white;

            //export mesh to asset and prefab
            if (GUILayout.Button("[此步不用操作]保存网格预设(不建议保存|保存场景即可)"))
            {
                //get gameobject and its mesh filter
                GameObject gObj = script.gameObject;
                Mesh       mesh = gObj.GetComponent <MeshFilter>().sharedMesh;

                if (!mesh)
                {
                    Debug.LogWarning("Could not save as prefab, asset does not have a Mesh.");
                    return;
                }

                if (AssetDatabase.Contains(mesh))
                {
                    Debug.Log("Mesh asset already exists. Cancelling.");
                    return;
                }

                //get the current unix timestamp for a unique naming scheme
                var    epochStart = new System.DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
                string timestamp  = (System.DateTime.UtcNow - epochStart).TotalSeconds.ToString("F0");
                string assetName  = "NavMesh_";

                //check that the folder does exist
                string dir = Path.GetDirectoryName(assetPath);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                    AssetDatabase.ImportAsset(dir);
                }

#if UNITY_5_3_OR_NEWER
                string sceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
#else
                string sceneName = Application.loadedLevelName;
#endif

                //create the mesh asset at the path specified, with unique name
                AssetDatabase.CreateAsset(mesh, assetPath + sceneName + "/" + assetName + timestamp + ".asset");
                AssetDatabase.SaveAssets();

                //create the prefab of this gameobject at the path specified, with the same name
                PrefabUtility.CreatePrefab(assetPath + assetName + timestamp + ".prefab", gObj,
                                           ReplacePrefabOptions.ConnectToPrefab);
                //rename instance to prefab name
                gObj.name = assetName + timestamp;
            }

            if (placing)
            {
                EditorGUILayout.Separator();
                EditorGUILayout.LabelField("Hint:", EditorStyles.boldLabel);
                EditorGUILayout.TextArea("Click on colliders to place vertices.\n" +
                                         "CTRL + click to start a new submesh.");
            }

            //================================================
            if (GUILayout.Button("将选中瞄点同步高度(以第一个选中的高度为准)"))
            {
                //don't continue without selected vertices
                if (selected.Count == 0)
                {
                    return;
                }

                Undo.RegisterCompleteObjectUndo(script, "Handle Moved");
                Mesh myMesh = script.gameObject.GetComponent <MeshFilter>().sharedMesh;
                if (myMesh)
                {
                    Undo.RecordObject(myMesh, "Handle Moved");
                }

                float standPosY = script.list[selected[0]].y;
                for (int i = 0; i < selected.Count; i++)
                {
                    Vector3 curPos = script.list[selected[i]];
                    script.list[selected[i]] = new Vector3(curPos.x, standPosY, curPos.z);
                }
                script.UpdateMesh(ConvertAllPoints());

                Debug.Log("将选中瞄点同步高度(以第一个选中的高度为准)==>" + standPosY);
            }
        }
예제 #3
0
        /// <summary>
        /// Custom inspector override for buttons.
        /// </summary>
        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();
            EditorGUILayout.Space();

            //display current count of placed vertices in the active submesh
            EditorGUILayout.LabelField("Current Vertices: " + script.current.Count);

            if (GUILayout.Button("Triangulate"))
            {
                script.RegenerateMesh(ConvertAllPoints());
            }

            //enter placement mode
            if (!placing && GUILayout.Button("Edit Mode: Off"))
            {
                Undo.RegisterCompleteObjectUndo(script, "Edit On");

                //if possible, create new submesh
                if (CheckSubMesh())
                {
                    GameObject newObj = script.CreateSubMesh();
                    Undo.RegisterCreatedObjectUndo(newObj, "Edit On");
                }

                //clear handle selections
                selected.Clear();
                placing = true;
            }

            GUI.color = Color.yellow;

            //leave placement mode and try to combine submeshes
            if (placing && GUILayout.Button("Edit Mode: On"))
            {
                Undo.RegisterCompleteObjectUndo(script, "Edit Off");

                //if possible, combine submeshes
                if (CheckCombine())
                {
                    //get all mesh filters
                    MeshFilter[] meshFilters = script.GetComponentsInChildren <MeshFilter>();
                    Undo.RecordObject(meshFilters[0], "Edit Off");

                    //let the script combine them
                    script.Combine();

                    for (int i = 1; i < meshFilters.Length; i++)
                    {
                        Undo.DestroyObjectImmediate(meshFilters[i].gameObject);
                    }
                }



                placing = false;
            }

            GUI.color = Color.white;

            //export mesh to asset and prefab
            if (GUILayout.Button("Save as Prefab"))
            {
                //get gameobject and its mesh filter
                GameObject gObj = script.gameObject;
                Mesh       mesh = gObj.GetComponent <MeshFilter>().sharedMesh;

                if (!mesh)
                {
                    Debug.LogWarning("Could not save as prefab, asset does not have a Mesh.");
                    return;
                }

                if (AssetDatabase.Contains(mesh))
                {
                    Debug.Log("Mesh asset already exists. Cancelling.");
                    return;
                }

                //get the current unix timestamp for a unique naming scheme
                var    epochStart = new System.DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
                string timestamp  = (System.DateTime.UtcNow - epochStart).TotalSeconds.ToString("F0");
                string assetName  = "NavMesh_";

                //check that the folder does exist
                string dir = Path.GetDirectoryName(assetPath);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                    AssetDatabase.ImportAsset(dir);
                }

                //create the mesh asset at the path specified, with unique name
                AssetDatabase.CreateAsset(mesh, assetPath + assetName + timestamp + ".asset");
                AssetDatabase.SaveAssets();

                //create the prefab of this gameobject at the path specified, with the same name
                PrefabUtility.CreatePrefab(assetPath + assetName + timestamp + ".prefab", gObj,
                                           ReplacePrefabOptions.ConnectToPrefab);
                //rename instance to prefab name
                gObj.name = assetName + timestamp;
            }
        }