아카이빙/Unity3D

[Unity3D] 커스텀 에디터(4) - GUILayout

셩님 2017. 4. 11. 06:12

유니티 커스텀 에디터(4) - GUILayout

원래는 EditorWindow에 이어서 EditorStyles를 다루려 했었지만, 직접 예제를 다뤄보니 EditorStyles보다는 GUILayout을 먼저 익히고 넘어가는 게 좋을 것 같아 급 변경함.

본 포스팅에서는 EditorWindow에서 GUILayout를 활용하는 예제를 소개하려 한다. 예제는 Unity - Scripting API를 참조해서 작성했으며, 자주 쓰일 것 같은 레이아웃을 다룬다.

예제

using UnityEngine;
using UnityEditor;

public class GUILayoutWindow : EditorWindow {

    //포스팅을 위해 편의상 줄바꿈.
    string myString = "La La Land is a 2016 American musical romantic drama film" +
        "written and directed by Damien Chazelle, and starring Ryan Gosling and Emma Stone" +
        "as a musician and an aspiring actress who meet and fall in love in Los Angeles." +
        "The film's title refers both to the city of Los Angeles and to the idiom" +
        "for being out of touch with reality.";
  
    bool myBool = true;
    float myFloat = 1.23f;
    int toolbarIndex = 0;
    string[] toolbars = {"toolbar1", "toolbar2", "toolbar3"};
    public float sliderValue = 1.0F;

    [MenuItem("Window/GUI Layout Window")]
    static void Init()
    {
        GUILayoutWindow window =
            (GUILayoutWindow)EditorWindow.GetWindow(typeof(GUILayoutWindow));
        window.Show();
    }

    void OnGUI()
    {

        GUILayout.BeginArea(new Rect(0, 0, 400, 400));

        GUILayout.BeginHorizontal();
        if (GUILayout.RepeatButton ("Repeat\nButton"))  Debug.Log ("Repeat Button");
        if (GUILayout.Button ("Button"))                Debug.Log ("Button");
        GUILayout.FlexibleSpace();

        GUILayout.BeginVertical();
        GUILayout.Box("Value:" + Mathf.Round(sliderValue));
        sliderValue = GUILayout.HorizontalSlider(sliderValue, 0.0F, 10);
        GUILayout.EndVertical();
        GUILayout.EndHorizontal();

        GUILayout.Label ("Label");
        GUILayout.Space(30);

        myBool = GUILayout.Toggle (myBool, "Toggle");
        toolbarIndex = GUILayout.Toolbar(toolbarIndex, toolbars);
        myString = GUILayout.TextArea (myString);
        myString = GUILayout.TextField (myString);
        myString = GUILayout.PasswordField (myString, '#');

        myFloat = GUILayout.HorizontalSlider (myFloat, 0f, 5f);
        myFloat = GUILayout.VerticalSlider (myFloat, 0f, 5f);
        GUILayout.EndArea();
    }
}


결과



정리