using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestCube : MonoBehaviour
{
public string monsterName = “”;
public void PrintNmae()
{
Debug.Log(monsterName);//输出名字
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public enum MonsterName
{
建民水怪,
精英怪,
小怪,
红名Boss
}
[CustomEditor(typeof(TestCube))]//将脚本TestCube变成一个可以自定义的脚本
public class TestEditor : Editor
{
MonsterName Enermy = MonsterName.建民水怪;//枚举类型变量的初始值
public override void OnInspectorGUI()//重写Inspector面板
{
base.OnInspectorGUI();
TestCube script = target as TestCube;//target指要重写的这个对象
EditorGUILayout.BeginHorizontal();
GUILayout.Label(“monster”);//创建一个文本,显示文字
//创建一个枚举的下拉菜单
Enermy = (MonsterName)EditorGUILayout.EnumPopup(Enermy, GUILayout.Width(100),

本文展示了如何在Unity中通过编写自定义编辑器脚本`TestEditor`,修改Inspector面板以显示一个枚举类型的下拉菜单。通过`OnInspectorGUI`方法重写面板,并使用`EnumPopup`创建枚举选择,将选择的值绑定到`TestCube`脚本的`monsterName`属性。此外,添加了一个按钮,点击后调用`PrintNmae`方法输出选定的怪物名字。
5296

被折叠的 条评论
为什么被折叠?



