1.添加输入框和标签
LevelScript:
using UnityEngine; using System.Collections; public class LevelScript : MonoBehaviour { public int experience; public int Level { get { return experience / 750; } } }
LevelScriptEditor
注意:在
OnInspectorGUI
事件中加入以下代码会 附加上默认的 属性:
DrawDefaultInspector();
sing UnityEngine; using System.Collections; using UnityEditor; [CustomEditor(typeof(LevelScript))] public class LevelScriptEditor : Editor { public override void OnInspectorGUI() { LevelScript myTarget = (LevelScript)target; myTarget.experience = EditorGUILayout.IntField("Experience", myTarget.experience); EditorGUILayout.LabelField("Level", myTarget.Level.ToString()); } }
效果:
2.添加按钮
ObjectBuilderScript
using UnityEngine; using System.Collections; public class ObjectBuilderScript : MonoBehaviour { public GameObject obj; public Vector3 spawnPoint; public void BuildObject() { Instantiate(obj, spawnPoint, Quaternion.identity); } }
ObjectBuilderEditor
using UnityEngine; using System.Collections; using UnityEditor; [CustomEditor(typeof(ObjectBuilderScript))] public class ObjectBuilderEditor : Editor { public override void OnInspectorGUI() { DrawDefaultInspector(); ObjectBuilderScript myScript = (ObjectBuilderScript)target; if(GUILayout.Button("Build Object")) { myScript.BuildObject(); } } }
时间: 2024-11-05 02:47:29