MeshCreator.OnGUI C# (CSharp) Method

OnGUI() public method

public OnGUI ( ) : void
return void
    public void OnGUI()
    {
        #if UNITY_EDITOR
        GUILayout.Label("Sprite", EditorStyles.boldLabel);

        spriteRenderer = (SpriteRenderer)EditorGUILayout.ObjectField(spriteRenderer, typeof(SpriteRenderer), true);

        if(spriteRenderer == null) return;

        #region Auto mesh creation buttons
        GUI.enabled = !previewMode;

        simplify = EditorGUILayout.FloatField("Vertex Dist.", simplify);

        if(GUILayout.Button("Generate Polygon from Texture")) {
            if(Reset())
                LoadPolygonFromSprite();
        }

        EditorGUILayout.Separator();

        if(GUILayout.Button("Reset Points")) {
            Reset();
        }

        EditorGUILayout.Separator();

        subdivideValue = EditorGUILayout.IntPopup(subdivideValue, subdivideContents, subdivideContentValues);
        if(GUILayout.Button("Subdivide Mesh")) {
            Subdivide(subdivideValue);
        }

        EditorGUILayout.Separator();

        GUI.enabled = true;
        #endregion

        #region Custom mesh creation
        changedBaseSelectDistance = EditorGUILayout.Slider("Handle Size", baseSelectDistance, 0, 1);
        if(baseSelectDistance != changedBaseSelectDistance) {
            baseSelectDistance = changedBaseSelectDistance;
            EditorUtility.SetDirty(this);
            SceneView.currentDrawingSceneView.Repaint();
        }

        EditorGUILayout.Separator();

        GUILayout.Label("[Ctrl + Click] to Add Point", EditorStyles.whiteLabel);
        GUILayout.Label("[Shift + Click] to Remove Point or Segment", EditorStyles.whiteLabel);
        GUILayout.Label("[Right Click + Drag] to Add Edge", EditorStyles.whiteLabel);
        GUILayout.Label("[Alt + Click] to Mark / Demark Area as Hole", EditorStyles.whiteLabel);

        #endregion

        #region Preview Mode Button
        GUI.enabled = true;

        if(hideGizmos != EditorGUILayout.Toggle("Hide gizmos", hideGizmos)) {
            hideGizmos = !hideGizmos;
            EditorUtility.SetDirty(this);
            SceneView.currentDrawingSceneView.Repaint();
        }

        GUI.color = (previewMode) ? Color.green : Color.white;
        if(GUILayout.Button("Preview Mode")) {
            previewMode = !previewMode;
            if(previewMode) {
                GeneratePreviewObject();
            }
            else {
                DestroyPreviewObject();
            }

            EditorUtility.SetDirty(this);
        }
        GUI.color = Color.white;
        EditorGUILayout.Separator();
        #endregion

        #region Save mesh button
        meshName = EditorGUILayout.TextField("Mesh Name", meshName);

        if(GUILayout.Button("Save Mesh")) {
            previewMode = false;
            Mesh mesh = GetMesh();

            mesh.name = string.IsNullOrEmpty(meshName) ? spriteRenderer.name : meshName;

            // Check if the Mesh directory exists, if not, create it.
            DirectoryInfo meshDir = new DirectoryInfo("Assets/Meshes");
            if (Directory.Exists(meshDir.FullName) == false)
            {
                Directory.CreateDirectory(meshDir.FullName);
            }
            ScriptableObjectUtility.CreateAsset(mesh, "Meshes/" + mesh.name + ".Mesh");
        }
        #endregion

        #region Load Mesh Button

            EditorGUILayout.Separator();
            customLoadMesh = (Mesh)EditorGUILayout.ObjectField(customLoadMesh, typeof(Mesh), true);

            if (GUILayout.Button("Load Custom Mesh")) {
                if (spriteRenderer != null && customLoadMesh != null) {
                    LoadMesh(customLoadMesh);
                }
            }
        #endregion
        #endif
    }