废话不多说,先上效果图:
,简单的来说也就是代码枚举几种类型,我选择A,就出现A的相关数据,选择B就出现B的相关数据,如果不做这种效果,inspector面板就会有很多东西,太乱。好了,下面开始教如何制作这个种效果的:
先新建一个C#脚本,脚本名称随便你,我这里用test1.cs,相关内容如下:
using UnityEngine; using System.Collections; public class test1 : MonoBehaviour { public enum type1 { a, b } public type1 m_type; public int a_int; public int b_int; }
这个脚本非常简单,就是枚举类型,public出2个公有变量,如果只是这样,我们inspector面板是这样的:
这肯定不能达到我们的要求,接下来在Project面板里新建一个文件夹,命名为EditZ喎?http://www.weiyer.com/Search.asp?KeyWord=/kf/ware/vc/" target="_blank" class="keylink">vcqOs1NrV4rj2zsS8/rzQz8LQwr2o0ru49r3Fsb6jrMP719bL5rHjo6zO0tXiwO/Tw05ld0JlaGF2aW91clNjcmlwdDEuY3OjrL3Fsb7E2sjdyOfPwqO6PC9wPg0KPHByZSBjbGFzcz0="brush:sql;">
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(test1))]//关联之前的脚本
public class NewBehaviourScript1 : Editor {
private SerializedObject test;//序列化
private SerializedProperty m_type,a_int, b_int;//定义类型,变量a,变量b
void OnEnable()
{
test = new SerializedObject(target);
m_type = test.FindProperty("m_type");//获取m_type
a_int = test.FindProperty("a_int");//获取a_int
b_int = test.FindProperty("b_int");//获取b_int
}
public override void OnInspectorGUI()
{
test.Update();//更新test
EditorGUILayout.PropertyField(m_type);
if (m_type.enumValueIndex == 0) {//当选择第一个枚举类型
EditorGUILayout.PropertyField(a_int);
}
else if (m_type.enumValueIndex == 1) {
EditorGUILayout.PropertyField(b_int);
}
//serializedObject.ApplyModifiedProperties();
test.ApplyModifiedProperties();//应用
}
}
脚本相对简单,就不解释了,这个脚本不需要挂载在物体上,现在点击刚刚的枚举类型,是不是可以出现想要的效果了!!!!O(∩_∩)O~