コード例 #1
0
        static void ExportNavOBJ()
        {
            GameObject[] gameObjects = Selection.gameObjects;

            if (gameObjects.Length < 1)
            {
                return;
            }
            GameObject active = Selection.activeGameObject;

            if (active == null)
            {
                active = gameObjects[0];
            }

            List <MeshFilter>          meshRenderers     = new List <MeshFilter>();
            List <SkinnedMeshRenderer> skinMeshRenderers = new List <SkinnedMeshRenderer>();

            foreach (GameObject gameObject in gameObjects)
            {
                Renderer[] renderers = gameObject.GetComponentsInChildren <Renderer>();
                foreach (Renderer renderer in renderers)
                {
                    if (renderer is MeshRenderer)
                    {
                        GameObject         go       = renderer.gameObject;
                        SerializedObject   so       = new SerializedObject(go);
                        SerializedProperty property = so.FindProperty("m_StaticEditorFlags");
                        if ((property.intValue & 8) != 0)
                        {
                            MeshFilter f = renderer.gameObject.GetComponent <MeshFilter>();
                            if (f)
                            {
                                meshRenderers.Add(f);
                            }
                        }
                    }
                }
            }

            NavObjectData v = new NavObjectData();

            v.export(meshRenderers.ToArray(), skinMeshRenderers.ToArray());

            string prefix = Application.dataPath + "/Exports/";

            if (Directory.Exists(prefix) == false)
            {
                Directory.CreateDirectory(prefix);
            }
            v.save(prefix + active.name + ".obj");

            EditorUtility.OpenWithDefaultApp(prefix);
        }
コード例 #2
0
        static void ExportNavMesh()
        {
            NavMeshTriangulation triangulatedNavMesh = NavMesh.CalculateTriangulation();

            Mesh mesh = new Mesh();

            mesh.name      = "ExportedNavMesh";
            mesh.vertices  = triangulatedNavMesh.vertices;
            mesh.triangles = triangulatedNavMesh.indices;

            Matrix4x4 matrix4X4 = new Matrix4x4();

            matrix4X4.SetTRS(Vector3.zero, Quaternion.identity, new Vector3(-1, 1, 1));

            NavObjectData v = new NavObjectData();

            v.exportMesh(mesh, matrix4X4);

            string prefix = Application.dataPath + "/Exports/";

            if (Directory.Exists(prefix) == false)
            {
                Directory.CreateDirectory(prefix);
            }
            string path = prefix + "navMesh.obj";

            v.save(path);


            AssetDatabase.Refresh();
            UnityEngine.Object ob = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(path);
            if (ob)
            {
                Selection.activeObject = ob;
                EditorGUIUtility.PingObject(ob);
            }

            EditorUtility.OpenWithDefaultApp(prefix);
        }
コード例 #3
0
        private void SceneInfo()
        {
            EditorGUI.BeginChangeCheck();
            GameObject root = sceneCFG.gameObject;

            showTransforms = EditorGUILayout.ToggleLeft("show", root.hideFlags == 0);
            if (GUI.changed)
            {
                if (showTransforms)
                {
                    root.hideFlags = 0;
                }
                else
                {
                    root.hideFlags = HideFlags.HideInHierarchy;
                }
                EditorApplication.DirtyHierarchyWindowSorting();
            }

            EditorGUI.BeginChangeCheck();
            EditorGUILayout.BeginVertical("box");
            int size = 1000;

            EditorGUILayout.IntSlider("X", (int)sceneCFG.rect.x, -size, size);
            EditorGUILayout.IntSlider("Y", (int)sceneCFG.rect.y, -size, size);
            int x      = EditorGUILayout.IntSlider("CenterX", (int)sceneCFG.rect.center.x, -size, size);
            int y      = EditorGUILayout.IntSlider("CenterY", (int)sceneCFG.rect.center.y, -size, size);
            int width  = EditorGUILayout.IntSlider("Width", (int)sceneCFG.rect.width, 0, 2000);
            int height = EditorGUILayout.IntSlider("Height", (int)sceneCFG.rect.height, 0, 2000);

            EditorGUILayout.EndVertical();
            Rect rect = new Rect(0, 0, width, height);

            rect.center = new Vector2(x, y);

            if (EditorGUI.EndChangeCheck())
            {
                Undo.RegisterCompleteObjectUndo(sceneCFG, "position");
                sceneCFG.rect = rect;
            }

            SceneView sceneView = SceneView.lastActiveSceneView;

            if (sceneView == null)
            {
                sceneView = SceneView.currentDrawingSceneView;
            }
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.ObjectField(sceneCFG.cutview, typeof(Texture), false);
            sceneCFG.preview = (Texture)EditorGUILayout.ObjectField("preview", sceneCFG.preview, typeof(Texture), false);
            EditorGUILayout.EndHorizontal();

            GUILayout.FlexibleSpace();
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("GetPreview"))
            {
                int   textureSize = (int)(rect.height * 2);
                float aspect      = rect.width / rect.height;
                int   textWidth   = Mathf.RoundToInt(aspect * textureSize);
                if (renderTexture == null || renderTexture.height != textureSize || renderTexture.width != textWidth)
                {
                    renderTexture           = new RenderTexture(textWidth, textureSize, 24);
                    renderTexture.hideFlags = HideFlags.HideAndDontSave;
                }
                else
                {
                    renderTexture.DiscardContents();
                }
                Camera camera = sceneView.camera;

                Vector3 v = new Vector3(rect.x + rect.width / 2f, 0, rect.y + rect.height / 2f);
                sceneView.orthographic     = true;
                sceneView.isRotationLocked = true;
                sceneView.LookAt(v, Quaternion.Euler(90, 0, 0));
                sceneView.Repaint();
                EditorApplication.delayCall += () =>
                {
                    RenderTexture oldRenderTexture    = camera.targetTexture;
                    float         oldAspect           = camera.aspect;
                    float         oldOrthographicSize = camera.orthographicSize;

                    bool oldFog = RenderSettings.fog;
                    Unsupported.SetRenderSettingsUseFogNoDirty(false);

                    camera.orthographicSize = rect.height / 2f;
                    camera.aspect           = aspect;
                    camera.targetTexture    = renderTexture;
                    camera.Render();

                    camera.orthographicSize = oldOrthographicSize;
                    camera.aspect           = oldAspect;
                    camera.targetTexture    = null;
                    Unsupported.SetRenderSettingsUseFogNoDirty(oldFog);

                    string name = "preview";
                    SaveRenderTextureToPNG(renderTexture, aspect, name);
                    AssetDatabase.Refresh();

                    string path = getSceneAssetPrefix() + "/" + name + ".png";
                    sceneCFG.cutview = sceneCFG.preview = AssetDatabase.LoadAssetAtPath <Texture2D>(path);
                    sceneView.Repaint();
                };
            }
            if (GUILayout.Button("GetHeightmap"))
            {
                int w = (int)(rect.width * 10);
                int h = (int)(rect.height * 10);

                Texture2D texture2D = new Texture2D(w, h, TextureFormat.ARGB32, false);
                texture2D.hideFlags = HideFlags.HideAndDontSave;
                Color[] colors = new Color[w * h];

                NavMeshTriangulation tmpNavMeshTriangulation = NavMesh.CalculateTriangulation();
                Vector3[]            vertices = tmpNavMeshTriangulation.vertices;
                int[]     indices             = tmpNavMeshTriangulation.indices;
                int       len       = indices.Length / 3;
                HeightMap heightMap = new HeightMap(w, h, TextureFormat.ARGB32, false);

                for (int i = 0; i < len; i++)
                {
                    int     index = indices[i * 3 + 0];
                    Vector3 v0    = Trans(vertices[index]);
                    index = indices[i * 3 + 1];
                    Vector3 v1 = Trans(vertices[index]);
                    index = indices[i * 3 + 2];
                    Vector3 v2 = Trans(vertices[index]);
                    heightMap.DrawTriangle(v0, v1, v2, Color.red);

                    if (i % 500 == 0)
                    {
                        ShowProgress(i, len);
                    }
                }
                EditorUtility.ClearProgressBar();
                texture2D = heightMap.EndDraw();

                string pngName = "heightMap";
                saveTexture(texture2D, pngName);

                Texture2D.DestroyImmediate(texture2D);
                AssetDatabase.Refresh();
            }
            if (GUILayout.Button("ExportNav"))
            {
                Renderer[]                 renderers         = GameObject.FindObjectsOfType <Renderer>();
                List <MeshFilter>          meshRenderers     = new List <MeshFilter>();
                List <SkinnedMeshRenderer> skinMeshRenderers = new List <SkinnedMeshRenderer>();
                foreach (Renderer renderer in renderers)
                {
                    if (renderer is MeshRenderer)
                    {
                        GameObject         go       = renderer.gameObject;
                        SerializedObject   so       = new SerializedObject(go);
                        SerializedProperty property = so.FindProperty("m_StaticEditorFlags");
                        if ((property.intValue & 8) != 0)
                        {
                            MeshFilter f = renderer.gameObject.GetComponent <MeshFilter>();
                            if (f)
                            {
                                meshRenderers.Add(f);
                            }
                        }
                    }
                }

                if (meshRenderers.Count == 0)
                {
                    List <GameObject> list = new List <GameObject>();
                    SceneManager.GetActiveScene().GetRootGameObjects(list);
                    List <Renderer> rList = new List <Renderer>();
                    foreach (GameObject go in list)
                    {
                        Renderer[] t = go.GetComponentsInChildren <Renderer>(true);
                        rList.AddRange(t);
                    }

                    foreach (Renderer renderer in rList)
                    {
                        if (renderer is MeshRenderer)
                        {
                            GameObject         go       = renderer.gameObject;
                            SerializedObject   so       = new SerializedObject(go);
                            SerializedProperty property = so.FindProperty("m_StaticEditorFlags");
                            if ((property.intValue & 8) != 0)
                            {
                                MeshFilter f = renderer.gameObject.GetComponent <MeshFilter>();
                                if (f)
                                {
                                    meshRenderers.Add(f);
                                }
                            }
                        }
                    }
                }


                NavObjectData v = new NavObjectData();
                v.export(meshRenderers.ToArray(), skinMeshRenderers.ToArray());

                FileInfo fileInfo = new FileInfo(Application.dataPath);
                string   prefix   = Path.Combine(fileInfo.Directory.FullName, "ReleaseResource/NAV/");
                if (Directory.Exists(prefix) == false)
                {
                    Directory.CreateDirectory(prefix);
                }
                v.save(prefix + "Meshes/RootScene.obj");

                System.Diagnostics.Process.Start(prefix + "Recast.bat");
                System.Diagnostics.Process.Start(prefix);
            }

            if (GUILayout.Button("CopyRect"))
            {
                IntRectange intRectange = new IntRectange();
                intRectange.CopyFrom(sceneCFG.rect);
                string v = intRectange.x + "," + intRectange.y + "," + intRectange.width + "," + intRectange.height;
                EditorGUIUtility.systemCopyBuffer = v;
                ShowNotification(new GUIContent("已复制"));
            }

            GUILayout.EndHorizontal();
            if (EditorGUI.EndChangeCheck())
            {
                sceneView.Repaint();
            }
        }