Game Development/Unity

[Editor] 2D array, 2차 행렬을 inspector에서 보기 + 빌드 문제

아기 요다 2019. 2. 13. 16:41

Unity Editor에서는 public으로 선언한 변수에 대해서 inspector에서 값을 변경할 수 있게 해준다.

이러한 사유로 SciptableObject를 상속받는 데이터를 만들어서 사용하려고 한다.

그러나 1D array까지 지원해주지만, 기본적으로 2D array에 대해서는 지원해주지 않는다.

 

이에 관해 Custom inspector를 만들어 해결할 수 있다.

 

(Warning)

I didn't test those things. i just type this for remember what i did.

(주의) 아래의 내용들을 테스트 안해봤다. 기억하려고 적어놓은 거.

 

크게 Editor를 상속받는 방법과 PropertyDrawer를 사용하는 방법이 있는 것 같다.

(결과를 먼저 알고 싶다면, 내용을 생략하고 바로 밑으로 건너 뛰자.

If you guys wanna know how to i solved this issue. just skip main content. and go to the bottom of this post.)

 

 

(1) PropertyDrawer는 어떻게 사용하는지 잘모르니 아래의 주소를 참고하자.

 

:: https://catlikecoding.com/unity/tutorials/editor/custom-data/

 

 

 

(2) Inherit Editor

 

대충 알기 때문에 개요만 작성하도록 하겠다.

 

스크립트 생성하고 해당 클래스에 아래와 같은 형태로 작성하자.

 

[CustomEditor(typeof(ScriptableObjectData))]
public class className : Editor
{}

 

또한 위와 같은 스크립트가 존재한다면 ScriptableObjectData라는 스크립트도 존재해야 되지 않겠는가.

 

[CreateAssetMenu(fileName = "NewScriptableObjectData", menuName = "Data/ScriptabelObjectData", order = 1)]
public class ScriptableObjectData : ScriptableObject
{
  // we don't care about data type. you can change 'bool type' to 'int type'.

  public bool a;
  [Range(0,5)]
  public int arraySize;
  public bool[,] arr = new int[arraySize, arraySize];
}

 

이제 ScriptableObjectData는 custom inspector의 형태를 띄기 때문에, inspector에서 변수가 표시되지 않을지도 모른다.

 

예전처럼 inspector에서 변수를 보기 위해서는 일련의 과정을 거쳐야 한다.

 

[CustomEditor(typeof(ScriptableObjectData))]
public class className : Editor
{
  private SerializedProperty a;

  void OnEnable()
  {
  	a = serializedObject.FindProperty("a");
  }

  public override void OnInspectorGUI()
  {
  	EditorGUILayout.PropertyField(a);
  }
}
 
위와 같은 형태를 띄면, custom inspector 이전의 inspector처럼 보일 것 이다.
 
그 외에 자세한 내용은 아래의 github 링크의 코드를 참고했다.

 

2D array Inspector를 개발해주신 형님의 github link.

:: https://github.com/Eldoir/2DArrayEditor

 
(2.5) 또한 [Range(min, max)]를 통해 슬라이더가 생성되는데 이를 1이 아닌 2단위로 증가시키고 싶었다.
이에 아래의 링크를 찾았으나, 짝수 단위의 2 증가만 가능하여 코드를 일부 수정했다.

 

::https://forum.unity.com/threads/range-attribute.451848/
 
value = (value / rangeAttribute.step) * rangeAttribute.step + 1; // inside of if statement
 
어짜피 2씩 증가시킬거라 괜찮은 방법이라고 생각된다.
 
 
 
(3) 그런데 그런데 그런데
안드로이드에서 빌드가 안되더라 이 말이다. 으아아

 

what is the problem... why i can't build my game..
 
error CS0246: The type or namespace name `Editor' could not be found. Are you missing an assembly reference?
 
'Assets/Editor/.' 경로가 틀린 것도 아닌데 안되서 다른 방법을 이용하기로 했다.
 
::https://wergia.tistory.com/61
 
그래서 문제가 해결됐다는 이야기... 휴

 

 

<Result Ref>

 

What is PropertyDrawer

:: https://catlikecoding.com/unity/tutorials/editor/custom-data/

 

 

2D array Inspector를 개발해주신 형님의 github link.

:: https://github.com/Eldoir/2DArrayEditor

 

 

PropertyDrawer change slider increase step, 1 to 2

::https://forum.unity.com/threads/range-attribute.451848/
 

 

Android build issue

::https://wergia.tistory.com/61