Skip to content

Commit

Permalink
04 : Property Drawer 구현하기 (1)
Browse files Browse the repository at this point in the history
https://doublsb.tistory.com/83

- EmotionDrawer 날코딩
  • Loading branch information
DoublSB committed Apr 10, 2020
1 parent 9b6f6d7 commit 77cd6ac
Showing 1 changed file with 56 additions and 4 deletions.
60 changes: 56 additions & 4 deletions Assets/DialogAsset/Script/EmotionDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,72 @@ namespace Doublsb.Dialog
[CustomPropertyDrawer(typeof(Emotion))]
public class EmotionDrawer : PropertyDrawer
{
private int ArraySize = 0;
private string EmotionName = "Input the emotion name";

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
SerializedProperty _emotion = property.FindPropertyRelative("_emotion");
SerializedProperty _sprite = property.FindPropertyRelative("_sprite");

ArraySize = _emotion.arraySize;

EditorGUI.BeginProperty(position, label, property);

// {

Rect NewRect = new Rect(position.position, new Vector2(position.width, 16));
EditorGUI.LabelField(position, "Emotion");

EditorGUI.indentLevel++;

Rect NewRect = new Rect(position.position, new Vector2(position.width / 3, 16));

for (int i = 0; i < _emotion.arraySize; i++)
{
NewRect = new Rect(NewRect.position + new Vector2(0, 20), NewRect.size);
EditorGUI.PropertyField(NewRect, _emotion.GetArrayElementAtIndex(i));
NewRect = new Rect(NewRect.position + new Vector2(0, 18), NewRect.size);
EditorGUI.PropertyField(NewRect, _emotion.GetArrayElementAtIndex(i), GUIContent.none);
}

NewRect = new Rect(position.position + new Vector2(NewRect.width, 0), new Vector2(NewRect.width, 16));

for (int i = 0; i < _sprite.arraySize; i++)
{
NewRect = new Rect(NewRect.position + new Vector2(0, 18), NewRect.size);
EditorGUI.PropertyField(NewRect, _sprite.GetArrayElementAtIndex(i), GUIContent.none);
}

NewRect = new Rect(position.position + new Vector2(NewRect.width * 2 + 10, 0), new Vector2(30, 16));

for (int i = 0; i < _sprite.arraySize; i++)
{
NewRect = new Rect(NewRect.position + new Vector2(0, 18), NewRect.size);
if(GUI.Button(NewRect, "-"))
{
int j = i;
_emotion.DeleteArrayElementAtIndex(j);

if (_sprite.GetArrayElementAtIndex(j).objectReferenceValue != null)
_sprite.DeleteArrayElementAtIndex(j);

_sprite.DeleteArrayElementAtIndex(j);
}
}

NewRect = new Rect(position.position + new Vector2(0, 18 * _emotion.arraySize + 30), new Vector2(position.width / 3 * 2, 16));

EmotionName = EditorGUI.TextField(NewRect, EmotionName);

if(GUI.Button(new Rect(NewRect.position + new Vector2(NewRect.width + 10, 0), new Vector2(70, 16)), "create"))
{
_emotion.InsertArrayElementAtIndex(_emotion.arraySize);
_emotion.GetArrayElementAtIndex(_emotion.arraySize - 1).stringValue = EmotionName;

_sprite.InsertArrayElementAtIndex(_sprite.arraySize);

if(_sprite.GetArrayElementAtIndex(_sprite.arraySize - 1).objectReferenceValue != null)
_sprite.DeleteArrayElementAtIndex(_sprite.arraySize - 1);

EmotionName = "";
}

// }
Expand All @@ -34,7 +86,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return property.isExpanded ? 16 * 10 : 16;
return 16 * (ArraySize + 4);
}
}
}
Expand Down

0 comments on commit 77cd6ac

Please sign in to comment.