Unity
Custom Editor - SerializedProperty
빵원군
2017. 5. 3. 18:45
[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 () { } }
[PlayerInspector2.cs] - Editor폴더에 존재
using UnityEngine; using UnityEditor; [CustomEditor(typeof(Player))] [CanEditMultipleObjects] public class PlayerInspector2 : Editor { Player player; SerializedProperty playerName; SerializedProperty playerHealth; private void OnEnable() { player = (Player)target; playerName = serializedObject.FindProperty("playerName"); playerHealth = serializedObject.FindProperty("health"); } public override void OnInspectorGUI() { serializedObject.Update(); EditorGUILayout.BeginVertical(); EditorGUILayout.PropertyField(playerName); if(playerHealth.floatValue < 20) { GUI.color = Color.red; } if (playerHealth.floatValue > 80) { GUI.color = Color.green; } EditorGUILayout.PropertyField(playerHealth); GUI.color = Color.white; EditorGUILayout.EndVertical(); serializedObject.ApplyModifiedProperties(); } }