본문 바로가기

Unity/Unity 스파르타

14주차 - 에디터 작업, 오브젝트 컨텐츠 추가 하기

728x90

 

1. 에디터 사이즈 설정

2. 구역 설정

3. 오브젝트 컨텐츠 생성하기

4.  마무리   OnGUI() 로 레이아웃 그려주기


1. 에디터 사이즈 설정

[MenuItem("Window/Create Map Tool")] 

 - 프로젝트 Widow 메뉴에 에디터 메뉴를 추가할 수 있습니다

툴의 최소 사이즈와 최대 사이즈를 설정해주고 마지막에 Show()를 통해 창을 Unity 편집기 인터페이스에 표시해 줍니다.

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class CreateMap_Tool : EditorWindow
{
	[MenuItem("Window/Create Map Tool")]
 	   public static void ShowWindow()
 	   {
  	      CreateMap_Tool ct = (CreateMap_Tool)GetWindow(typeof(CreateMap_Tool));
  	      ct.minSize = new Vector2(350, 500);
          ct.maxSize = new Vector2(350, 700);
          ct.Show();
	   }
}

 

2. 구역 설정

툴 레이아웃의 영역 설정은 Rect로 설정해 줍니다.

x,y : 구역의 시작점

width,height : 구역의 크기 설정

GUI.DrawTexture(objectSection, objectSectionTexture); : 텍스쳐를 통해 구역의 배경화면을 설정 할 수 있습니다

	Rect objectSection;

	private void DrawLayouts()
    {
        objectSection.x = 0;
        objectSection.y = 100;
        objectSection.width = Screen.width;
        objectSection.height = 200;
        GUI.DrawTexture(objectSection, objectSectionTexture);

    }

 

3. 오브젝트 컨텐츠 생성하기

  1. 오브젝트 컨텐츠들을 놓을 구역 설정을 해줍니다
    1. . GUILayout.BeginArea(objectSection); 구역 설정
  2. 리소스에 있는 오브젝트들을 불러옵니다
  3. 오브젝트들의 프리뷰를 텍스쳐로 가져와 GUIContent 리스트를 만듭니다
list = new(Resources.LoadAll<GameObject>(path));
 foreach (GameObject obj in list)
            {
                Texture2D texture = AssetPreview.GetAssetPreview(obj);
                contentsList.Add(new GUIContent(texture));
            }

 4. GUIContent 를 모아논 리스트를 사용해 레이아웃에 그려줍니다.

 foreach (GUIContent content in contentsList)
            {
                if (curWidth == 0)
                {
                    GUILayout.BeginHorizontal(GUILayout.Width(Screen.width));
                }

                if (GUILayout.Button(content, _GUIStyle_Cell))
                {
                    Debug.Log($"{index}, {list[index]}");
                }
                GUILayout.Label(list[index].name, _GUIStyle_Text);

                if (curWidth > screenWidth - 10)
                {
                    curWidth = 0;
                    index++;
                    GUILayout.EndHorizontal();
                    continue;
                }
                curWidth += _GUIStyle_Cell.fixedWidth;
                index++;

            }

            GUILayout.EndArea();

 

*구역설정후 마지막에 구역을 끝내줘야함

GUILayout.BeginScrollView();
GUILayout.BeginArea()

 GUILayout.EndArea();
 GUILayout.EndScrollView();


GUIStyle guiStyle = new GUIStyle(); 
GUIStyle 을 통해 GUI의 스타일을 조정할 수 있다 
마진, 패딩, 글자색, 크기 등등

728x90