ruk·si

⬜️ Unity
Editor Window

Updated at 2016-10-16 00:43

How to create basic editor window for custom tools.

using System;
using UnityEngine;
using UnityEditor;

// Save this to any "Editor" directory

namespace Company.MyGame
{
    [Serializable]
    public class MyWindow : EditorWindow
    {
        private Vector2 scrollPosition;

        [MenuItem("Window/MyWindow")]
        public static void ShowWindow()
        {
            var window = EditorWindow.GetWindow<MyWindow>("MyWindow", true);
            window.titleContent = new GUIContent("MyWindow");
            window.titleContent.tooltip = "This is my window!";
        }

        private void OnGUI()
        {
            GUILayout.Label("MyWindow", EditorStyles.boldLabel);
            scrollPosition = GUILayout.BeginScrollView(scrollPosition);
            if (GUILayout.Button("Do Something", GUILayout.Width(70f))) {
                // TODO: do something to start the progress bar.
            }
            GUILayout.EndScrollView();

            UpdateProgressBar();
        }

        private void UpdateProgressBar()
        {
            if (false) {
                var current = 5;
                var max = 10;
                var value = Mathf.Clamp01(current / max);
                EditorUtility.DisplayProgressBar("Heading", "Body", value);
            } else {
                EditorUtility.ClearProgressBar();
            }
        }

    }
}