読者です 読者をやめる 読者になる 読者になる

けいごのなんとか

Unityユーザーとしてのブログ。ギリギリ路線走ってます。

様々なEditorWindow④ - PreferenceWindow

様々なEditorWindow① - EditorWindow

様々なEditorWindow② - SearchableWIndow

様々なEditorWindow③ - ScriptableWizard

の続きです。

PreferenceWindow

UnityEditorの環境設定を行うWindowです。

f:id:anchan828:20130217010311p:plain

UnityEditorの見た目や動作に関する設定を行うWindowであるため、独自のアセットの設定Windowとして使用することはあまりオススメ出来ません。

Menuが増えるとスライダーが表示されます。 ( 見た目よろしくない... )

f:id:anchan828:20130217010350p:plain

サンプル

using UnityEngine;
using UnityEditor;

public class Hoge
{
    private const string HOGE_KEY = "HOGE_KEY";
    private static string hogeValue = string.Empty;
    private static bool isLoaded = false;

    [PreferenceItem("CustomMenu")]
    static void Menu ()
    {
        
        if (isLoaded == false) {
            hogeValue = EditorPrefs.GetString (HOGE_KEY, "");
            isLoaded = true;
            Debug.Log ("Loaded");
        }
        
        EditorGUI.BeginChangeCheck ();
        
        hogeValue = EditorGUILayout.TextField ("HogeKey", hogeValue);
            
        if (EditorGUI.EndChangeCheck ()) {    
            EditorPrefs.SetString (HOGE_KEY, hogeValue);
            Debug.Log ("Saved");
        }
    }
}

PreferenceWindowっぽいWindowを作っちゃおう

アセットの設定Windowとかに利用しちゃいましょう。

f:id:anchan828:20130217020327p:plain

using UnityEngine;
using UnityEditor;
using System;

public class OriginalPreferenceWindow : EditorWindow
{
    private GUIStyle preferencesSectionStyle = new GUIStyle ("PreferencesSection");
    private CustomSections section = CustomSections.Hoge;

    [MenuItem("Window/OriginalPreferenceWindow")]
    static void Open ()
    {
        OriginalPreferenceWindow window = GetWindow<OriginalPreferenceWindow> (true, "OriginalPreferenceWindow");
        SetWindowSize (window);
    }
    
    
    //========================
    //このソースコードそのまま使うならここをいじる
    
    //sections
    enum CustomSections
    {
        Hoge=0,
        Fuga,
        Foo
    }
    
    //選択したsectionに対するGUIの部分
    void DrawSections ()
    {
        switch (section) {
        case CustomSections.Hoge:
            GUILayout.Label ("Hoge!!");
            break;
        case CustomSections.Foo:
            GUILayout.Label ("Foo!!");
            break;
        case CustomSections.Fuga:
            GUILayout.Label ("Fuga!!");
            break;
        }
    }
    
    //========================
    //以下はレイアウトの設定なので基本触らない
    
    void OnGUI ()
    {
        GUILayout.BeginHorizontal ();
        {
            DrawSectionBox ();
            GUILayout.BeginVertical ();
            {
                DrawSections ();
            }
            GUILayout.EndVertical ();
        }
        GUILayout.EndHorizontal ();
    }

    void DrawSectionBox ()
    {
        GUILayout.BeginVertical (GUILayout.Width (120));
        {
            ///===============
            ///  SelectionBox
            ///===============
            
            GUILayout.Space (30);
            EditorGUIUtility.LookLikeControls (180f);
            GUI.DrawTexture (new Rect (0, 0, Screen.width * 0.24f, Screen.height), backgroundTexture); 
            GUI.Label (new Rect (0, 0, Screen.width * 0.24f, Screen.height), "", "PreferencesSectionBox");
        
            preferencesSectionStyle.normal.textColor = EditorGUIUtility.isProSkin ? new Color (0.7f, 0.7f, 0.7f, 1) : Color.black;
        
            
            {
                section = (CustomSections)GUILayout.SelectionGrid ( (int) section, Enum.GetNames (typeof(CustomSections)), 1, preferencesSectionStyle);
            }
            
            preferencesSectionStyle.onNormal.background = onNomalTexture;
        }
        GUILayout.EndVertical ();
    }
    
    
    //======================== 
    //以下はスタイルの設定なので基本触らない

    private static void SetWindowSize (EditorWindow window)
    {
        int left = EditorPrefs.GetInt ("UnityEditor.PreferencesWindowx", 96);
        int top = EditorPrefs.GetInt ("UnityEditor.PreferencesWindowy", 271);
        int width = EditorPrefs.GetInt ("UnityEditor.PreferencesWindoww", 500);
        int height = EditorPrefs.GetInt ("UnityEditor.PreferencesWindowh", 400);
        window.position = new Rect (left, top, width, height);
        window.minSize = new Vector2 (width, height);
        window.maxSize = window.minSize;
    }
    
    private static Texture2D _onNomalTexture;

    public static Texture2D onNomalTexture {
        get {
            _onNomalTexture = new Texture2D (1, 1);
            _onNomalTexture.SetPixel (0, 0, EditorGUIUtility.isProSkin ? new Color32 (66, 96, 147, 255) : new Color32 (72, 129, 227, 255));
            _onNomalTexture.Apply ();
            return _onNomalTexture;
        }
    }

    private static Texture2D _backgroundTexture;

    public static Texture2D backgroundTexture {
        get {
            if (_backgroundTexture == null) {
                _backgroundTexture = new Texture2D (1, 1);
                _backgroundTexture.SetPixel (0, 0, new Color32 (222, 222, 222, 255));
            }
            return    _backgroundTexture;
        }
    }
}