Custom Inspector

Unity 2017. 5. 3. 09:18


[Player.cs]
using UnityEngine;

public class Player : MonoBehaviour {

    public int id;
    
    public string playerName;
    public string backStory;    
    public float health;
    public float damage;
    
    public float weapon1Damage, weapon2Damage;

    public string shoesName;
    public int shoesSize;
    public string shoesType;   

    // Use this for initialization
    void Start () {
        health = 50;		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}


[PlayerInspector.cs] - Asset/Editor 에 존재해야 한다.
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Player))]
public class PlayerInspector : Editor {

    Player player;
    bool showWeapons;
    private void OnEnable()
    {
        player = (Player)target;
    }

    public override void OnInspectorGUI()
    {
        //base.OnInspectorGUI();
        EditorGUILayout.BeginVertical();
        EditorGUILayout.LabelField("Player id " + player.id);
        player.playerName = EditorGUILayout.TextField("Player Name ", player.playerName);
        

        EditorGUILayout.LabelField("Back Story");
        player.backStory = EditorGUILayout.TextArea(player.backStory,GUILayout.MinHeight(70));

        if (player.health < 20)
            GUI.color = Color.red;
        if (player.health > 80)
            GUI.color = Color.green;

        Rect progressRect = GUILayoutUtility.GetRect(20,20);
        EditorGUI.ProgressBar(progressRect, player.health / 100.0f, "Health " + player.health + " / 100");

        GUI.color = Color.white;

        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();

        player.damage = EditorGUILayout.Slider("Damage ", player.damage, 10, 20);

        if(player.damage < 12)
        {
            EditorGUILayout.HelpBox("The Damage is too low", MessageType.Warning);
        }
        if (player.damage > 18)
        {
            EditorGUILayout.HelpBox("Damage is too damn high", MessageType.Warning);
        }

        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();

        showWeapons = EditorGUILayout.Foldout(showWeapons, "Weapons");
        if(showWeapons)
        {
            
            player.weapon1Damage = EditorGUILayout.FloatField("Weapon 1 Damage", player.weapon1Damage);
            EditorGUI.indentLevel += 2;
            player.weapon2Damage = EditorGUILayout.FloatField("Weapon 2 Damage", player.weapon2Damage);
            EditorGUI.indentLevel -= 2;
        }

        EditorGUILayout.LabelField("Shoes");
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Name ", GUILayout.MaxWidth(50));
        player.shoesName = EditorGUILayout.TextField(player.shoesName);
        EditorGUILayout.LabelField("Size ", GUILayout.MaxWidth(50));
        player.shoesSize = EditorGUILayout.IntField(player.shoesSize);
        EditorGUILayout.LabelField("Type ", GUILayout.MaxWidth(50));
        player.shoesType = EditorGUILayout.TextField(player.shoesType);

        EditorGUILayout.EndHorizontal();

        EditorGUILayout.EndVertical();

        
        player.health += 2;
        if (player.health > 100)
            player.health = 0;
        
    }

}





'Unity' 카테고리의 다른 글

FSM 엔진  (0) 2017.06.11
Custom Editor - SerializedProperty  (0) 2017.05.03
EditorWindow 사용예  (0) 2017.05.02
Custom Editor Window - GUI  (0) 2017.05.02
Editor Scripting Tip [Built-in Attribute]  (0) 2017.05.02
Posted by 빵원군
,

EditorWindow 사용예

Unity 2017. 5. 2. 22:43

지뢰찾기 RPG개발시 EditorWindow 사용예이다.

해당 게임씬이고 플레이시에만 보이도록 설정하고 스텟정보를 보이도록 했다.

게임플레이 도중 아이템 착용이나 스킬 적용후 스텟이 변화되는 부분을 실시간으로 볼수 있다.


using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;

public class MRDebugWindow : EditorWindow {

    [MenuItem("디버그메뉴/스텟정보")]
    static void Init()
    {
        MRDebugWindow window = (MRDebugWindow)EditorWindow.GetWindow(typeof(MRDebugWindow));
        window.titleContent.text = "영웅/몬스터 스텟정보";
        window.Show();
    }

    private void OnGUI()
    {
        if(EditorApplication.isPlaying && EditorSceneManager.GetActiveScene().name == "ingame") //플레이시만 보이도록
        {
            MRHero hero = MRGameManager.Instance.GetHero();

            GUILayout.BeginVertical();
            GUILayout.Space(10);
            GUILayout.Label("[영웅]");
            GUILayout.Label("위치 : " + hero.Position.x + "," + hero.Position.y);
            GUILayout.BeginHorizontal();
            GUILayout.Label("힘 : " + (int)hero.Status.strength);
            GUILayout.Label("지능 : " + (int)hero.Status.intelligence);
            GUILayout.Label("민첩 : " + (int)hero.Status.agility);
            GUILayout.Label("건강 : " + (int)hero.Status.vitality);
            GUILayout.Label("행운 : " + (int)hero.Status.luck);
            GUILayout.EndHorizontal();

            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            GUILayout.Label("무기 : " + hero.equipItems.weapon.name);
            GUILayout.Label("방어 : " + hero.equipItems.armor.name);
            GUILayout.Label("악세사리 : " + hero.equipItems.accessories.name);
            GUILayout.Label("머리 : " + hero.equipItems.head.name);
            GUILayout.EndHorizontal();

            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            GUILayout.Label("생명력 : " + (int)hero.CurrentHealthPoint + " / " + (int)hero.Status.healthPoint, GUILayout.Width(150));
            GUILayout.Label("공격력 : " + (int)hero.Status.attack, GUILayout.Width(100));
            GUILayout.Label("마력 : " + (int)hero.Status.magicAttack, GUILayout.Width(100));
            GUILayout.Label("방어력 : " + (int)hero.Status.defence, GUILayout.Width(100));
            GUILayout.Label("저항력 : " + (int)hero.Status.resistance, GUILayout.Width(100));
            GUILayout.Label("명중률 : " + hero.Status.hitRate, GUILayout.Width(100));
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            GUILayout.Label("회피율 : " + hero.Status.dodgeRate, GUILayout.Width(150));
            GUILayout.Label("치명타확률 : " + hero.Status.criticalRate, GUILayout.Width(100));
            GUILayout.Label("공격속도 : " + hero.Status.attackSpeed, GUILayout.Width(100));
            GUILayout.Label("자연회복량 : " + (int)hero.Status.natureRestoration, GUILayout.Width(100));
            GUILayout.Label("만복도 : " + (int)hero.CurrentHungerPoint + "/" + (int)hero.Status.hungerPoint, GUILayout.Width(150));
            GUILayout.EndHorizontal();

            GUILayout.Space(20);

            if (hero.ActionTarget != null)
            {
                GUILayout.Label("[선택된 몬스터]");
                if (hero.ActionTarget.GetType().ToString() == "MRMonster")
                {
                    MRMonster mon = (MRMonster)hero.ActionTarget;

                    GUILayout.Label("이름 : " + mon.Name);
                    GUILayout.Label("위치 : " + mon.Position.x + "," + mon.Position.y);

                    GUILayout.BeginHorizontal();
                    GUILayout.Label("힘 : " + (int)mon.Status.strength);
                    GUILayout.Label("지능 : " + (int)mon.Status.intelligence);
                    GUILayout.Label("민첩 : " + (int)mon.Status.agility);
                    GUILayout.Label("건강 : " + (int)mon.Status.vitality);
                    GUILayout.Label("행운 : " + (int)mon.Status.luck);
                    GUILayout.EndHorizontal();

                    GUILayout.Space(10);
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("생명력 : " + (int)mon.CurrentHealthPoint + " / " + (int)mon.Status.healthPoint, GUILayout.Width(150));
                    GUILayout.Label("공격력 : " + (int)mon.Status.attack, GUILayout.Width(100));
                    GUILayout.Label("마력 : " + (int)mon.Status.magicAttack, GUILayout.Width(100));
                    GUILayout.Label("방어력 : " + (int)mon.Status.defence, GUILayout.Width(100));
                    GUILayout.Label("저항력 : " + (int)mon.Status.resistance, GUILayout.Width(100));
                    GUILayout.Label("명중률 : " + mon.Status.hitRate, GUILayout.Width(100));
                    GUILayout.EndHorizontal();
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("회피율 : " + mon.Status.dodgeRate, GUILayout.Width(150));
                    GUILayout.Label("치명타확률 : " + mon.Status.criticalRate, GUILayout.Width(100));
                    GUILayout.Label("공격속도 : " + mon.Status.attackSpeed, GUILayout.Width(100));
                    GUILayout.EndHorizontal();

                }
                else if (hero.ActionTarget.GetType().ToString() == "MRMine")
                {
                    MRMine mon = (MRMine)hero.ActionTarget;

                    GUILayout.Label("이름 : " + mon.Name);
                    GUILayout.Label("위치 : " + mon.Position.x + "," + mon.Position.y);

                    GUILayout.BeginHorizontal();
                    GUILayout.Label("힘 : " + (int)mon.Status.strength);
                    GUILayout.Label("지능 : " + (int)mon.Status.intelligence);
                    GUILayout.Label("민첩 : " + (int)mon.Status.agility);
                    GUILayout.Label("건강 : " + (int)mon.Status.vitality);
                    GUILayout.Label("행운 : " + (int)mon.Status.luck);
                    GUILayout.EndHorizontal();

                    GUILayout.Space(10);
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("생명력 : " + (int)mon.CurrentHealthPoint + " / " + (int)mon.Status.healthPoint, GUILayout.Width(150));
                    GUILayout.Label("공격력 : " + (int)mon.Status.attack, GUILayout.Width(100));
                    GUILayout.Label("마력 : " + (int)mon.Status.magicAttack, GUILayout.Width(100));
                    GUILayout.Label("방어력 : " + (int)mon.Status.defence, GUILayout.Width(100));
                    GUILayout.Label("저항력 : " + (int)mon.Status.resistance, GUILayout.Width(100));
                    GUILayout.Label("명중률 : " + mon.Status.hitRate, GUILayout.Width(100));
                    GUILayout.EndHorizontal();
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("회피율 : " + mon.Status.dodgeRate, GUILayout.Width(150));
                    GUILayout.Label("치명타확률 : " + mon.Status.criticalRate, GUILayout.Width(100));
                    GUILayout.Label("공격속도 : " + mon.Status.attackSpeed, GUILayout.Width(100));
                    GUILayout.EndHorizontal();
                }
            }
            GUILayout.EndVertical();
            

        }
    }

    private void OnInspectorUpdate()
    {
        Repaint();
    }

}



'Unity' 카테고리의 다른 글

Custom Editor - SerializedProperty  (0) 2017.05.03
Custom Inspector  (0) 2017.05.03
Custom Editor Window - GUI  (0) 2017.05.02
Editor Scripting Tip [Built-in Attribute]  (0) 2017.05.02
unity gitignore 파일  (0) 2017.03.07
Posted by 빵원군
,

Custom Editor Window - GUI

Unity 2017. 5. 2. 21:26
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;

public class BugReportWindow : EditorWindow {

    int normalTextSize;
    string windowTitle;
    Object source;
    int num = 0;

    [MenuItem("내가만든메뉴/내가만든윈도우")]
    static void Init()
    {
        BugReportWindow window = (BugReportWindow)EditorWindow.GetWindow(typeof(BugReportWindow));
        window.titleContent.text = "내가만든윈도우";        
        window.Show();
    }

    private void OnGUI()
    {
        GUILayout.BeginVertical();
        GUILayout.Space(10);
        GUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace();
        normalTextSize = GUI.skin.label.fontSize;
        GUI.skin.label.fontSize = 25;
        GUILayout.Label("연습중");
        GUI.skin.label.fontSize = normalTextSize;
        GUILayout.FlexibleSpace();
        GUILayout.EndHorizontal();
        GUILayout.EndVertical();

        GUILayout.Space(10);
        windowTitle = EditorGUILayout.TextField("제목을 적어주세요", windowTitle, GUILayout.Width(300));
        GUILayout.Label("씬 : " + EditorSceneManager.GetActiveScene().path);
        GUILayout.Label("시간 : " + System.DateTime.Now.ToString());
        source = EditorGUILayout.ObjectField("오브젝트", source, typeof(Object), true);


        GUILayout.BeginHorizontal();
        GUILayout.Label(num.ToString());
        if (GUILayout.Button("+"))
            num++;

        if (GUILayout.Button("-"))
            num--;

        GUILayout.EndHorizontal();
    }
}




'Unity' 카테고리의 다른 글

Custom Inspector  (0) 2017.05.03
EditorWindow 사용예  (0) 2017.05.02
Editor Scripting Tip [Built-in Attribute]  (0) 2017.05.02
unity gitignore 파일  (0) 2017.03.07
스크립트에서 게임의 실행을 일시정지  (0) 2017.03.03
Posted by 빵원군
,