VioletaBabel

71. Unity 팁 3 본문

BCA/4. Unity
71. Unity 팁 3
Beabletoet 2018. 11. 21. 11:58

 - 툴 만드는 법

크게 두 가지이다.

우선 Editor라는 폴더 안에 있지 않으면 인스펙터 변형에 작동이 되지 않음. 에디터 폴더의 위치나 갯수는 상관 X

1. 인스펙터를 변형

Enemy를 만들어 그 오브젝트에 이 스크립트를 넣어준다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Enemy.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Enemy : MonoBehaviour
{
    public MonsterType monsterType;
    public int HP;
    public float Damage;
    public string Tag;
    public bool CanRun;
}
 
public enum MonsterType
{
    Slime,
    Ent,
    Goblin
}
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//EnemyEditor.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor; // 이걸 쓰게되면 에디터라는 폴더에 들어가야 함
 
[CustomEditor(typeof(Enemy))] // 제어할 스크립트를 적어준다.
public class EnemyEditor : Editor // 에디터 상속
{
    public Enemy selected; // 내가 하이에라키에서 선택한 에너미
 
    private void OnEnable()
    {
        if (AssetDatabase.Contains(target)) // 타겟은 선택한 것.
        {
            selected = null;
        }
        else
        {
            selected = (Enemy)target; //타겟은 오브젝트 형식이기에 형변환해서 넣어준다.
        }
    }
 
    public override void OnInspectorGUI()
    { // 본래 이게 유니티 인스펙터에 보이게 뿌려주던 아이. 기존에 실행되던 게 안되고 이제 새로 만드는 셈. 
        //base.OnInspectorGUI();
 
        if (selected == null)
            return;
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("****** 몬스터 정보 입력 툴 ******");
        EditorGUILayout.LabelField(selected.name);
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();
 
        Color tmpColor = Color.white;
        switch(selected.monsterType)
        {
            case MonsterType.Slime: tmpColor = Color.yellow; break;
            case MonsterType.Ent: tmpColor = Color.cyan; break;
            case MonsterType.Goblin: tmpColor = Color.green; break;
        }
        GUI.color = tmpColor; // 색을 입힌다
 
        selected.monsterType = (MonsterType)EditorGUILayout.EnumPopup("몬스터 종류", selected.monsterType); // enum 타입
 
        GUI.color = Color.white;
 
        selected.HP = EditorGUILayout.IntField("몬스터 체력", selected.HP); // 값이 보임과 동시에 바꾸면 바꿔도 주는 줄
        if (selected.HP < 0)
            selected.HP = 0//음수를 넣으면 0으로 잡아준다
 
        EditorGUILayout.Space();
        selected.HP = EditorGUILayout.IntField("몬스터 체력", selected.HP);
        EditorGUILayout.Space();
        selected.Damage = EditorGUILayout.FloatField("몬스터 공격력", selected.Damage);
        EditorGUILayout.Space();
        selected.Tag = EditorGUILayout.TextField("설명", selected.Tag);
        EditorGUILayout.Space();
        if(GUILayout.Button("Resize"))
        {
            selected.transform.localScale = Vector3.one * Random.Range(0.5f, 1f);
        }
    }
}
cs

에디터 폴더에 스크립트를 넣는다.



2. 윈도우를 띄움

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//TestEditor.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class TestEditor : EditorWindow
{
    [MenuItem("테스트 툴/테스트")]
    static void Init()
    {
        TestEditor window = (TestEditor)EditorWindow.GetWindow(typeof(TestEditor));
    }
 
    private void OnGUI()
    {
        if(GUILayout.Button("동그라미 선택"))
        {
            SphereCollider[] colliders = FindObjectsOfType(typeof(SphereCollider)) as SphereCollider[];
            List<GameObject> listGo = new List<GameObject>();
            if(colliders != null)
            {
                for (int i = 0; i < colliders.Length; ++i)
                    listGo.Add(colliders[i].gameObject);
            }
            Selection.objects = listGo.ToArray();
        }
        else if (GUILayout.Button("네모 선택"))
        {
            BoxCollider[] colliders = FindObjectsOfType(typeof(BoxCollider)) as BoxCollider[];
            List<GameObject> listGo = new List<GameObject>();
            if (colliders != null)
            {
                for (int i = 0; i < colliders.Length; ++i)
                    listGo.Add(colliders[i].gameObject);
            }
            Selection.objects = listGo.ToArray();
        }
        else if (GUILayout.Button("콜라이더 전체 선택"))
        {
            Collider[] colliders = FindObjectsOfType(typeof(Collider)) as Collider[];
            List<GameObject> listGo = new List<GameObject>();
            if (colliders != null)
            {
                for (int i = 0; i < colliders.Length; ++i)
                    listGo.Add(colliders[i].gameObject);
            }
            Selection.objects = listGo.ToArray();
        }
    }
}
 
cs




 - 깃허브를 써야하는 이유

1. 협업

2. 버전 관리 => 혼자여도 깃허브를 써야하는 이유


 - Spine (뼈대 애니메이션)

http://ko.esotericsoftware.com/spine-in-depth

여기껄 제일 많이들 씀

https://github.com/EsotericSoftware/spine-runtimes <- 위 Spine 3.6버전임

이걸 다운로드받은 후 압축을 풀고 유니티에 Spine폴더를 만든 후 Spine-CSharp 폴더랑 Spine-Unity 폴더를 드래그해서 넣는다.

Spine-Unity에 예제가 있음

유료임 ㅇㅇ...


Spine의 구성 (한 이미지에는 이 6개가 세트로 따라옴)

 아틀라스 데이터 (아틀라스는 여러 텍스쳐를 하나의 규격에 넣는 것. 그리고 그 좌표값을 정리한 데이터임)

 아틀라스 이미지 파일

 Json 파일 (애니메이션이나 스킨 및 기타 정보를 저장)

 아틀라스 에셋

 스파인데이터 에셋

 머테리얼


Spine 스크립트로 제어하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//SpineFileController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine.Unity;
public class SpineFileController : MonoBehaviour
{
    public SkeletonAnimation skeletonAnimation;
 
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        { 
            skeletonAnimation.AnimationState.SetAnimation(0"Idle"true);
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            skeletonAnimation.AnimationState.SetAnimation(0"Attack"false);
            skeletonAnimation.AnimationState.AddAnimation(0"Idle"true0);
        }
    }
}
 
cs


'BCA > 4. Unity' 카테고리의 다른 글

72. Model View Controller 2 - PostMessage, DelayMessage  (0) 2018.11.21
70. Unity 팁 2  (0) 2018.11.21
69. Unity 팁 1  (0) 2018.11.21
67. Model View Controller 1 - SendMessage  (0) 2018.11.20
66. A*를 응용한 이동  (0) 2018.08.14
Comments