十二、Unity编辑器开发之各类字段及滑动条绘制
【摘要】
1.各类字段:
为我们的Test类中添加以下字段:
using UnityEngine; public class Test : MonoBehaviour{ public enum TestEnum { Enum1, Enum2, } public string stringV...
1.各类字段:
为我们的Test类中添加以下字段:
-
using UnityEngine;
-
-
public class Test : MonoBehaviour
-
{
-
public enum TestEnum
-
{
-
Enum1,
-
Enum2,
-
}
-
-
public string stringValue = "str";
-
public int intValue = 30;
-
public float floatValue = 50f;
-
public AudioClip clipValue;
-
public string passwordValue = "1a2s3xd455";
-
public TestEnum EnumValue = TestEnum.Enum1;
-
}
在TestEditor类中来绘制这些字段:
-
using UnityEngine;
-
using UnityEditor;
-
-
[CustomEditor(typeof(Test))]
-
public class TestEditor : Editor
-
{
-
private Test Target;
-
private void OnEnable()
-
{
-
Target = target as Test;
-
}
-
-
public override void OnInspectorGUI()
-
{
-
//绘制String类型字段
-
string newStringValue = EditorGUILayout.TextField("String Value", Target.stringValue);
-
if (newStringValue != Target.stringValue)
-
{
-
Target.stringValue = newStringValue;
-
}
-
//绘制Int类型字段
-
int newIntValue = EditorGUILayout.IntField("Int Value", Target.intValue);
-
if (newIntValue != Target.intValue)
-
{
-
Target.intValue = newIntValue;
-
}
-
//绘制Float类型字段
-
float newFloatValue = EditorGUILayout.FloatField("Float Value", Target.floatValue);
-
if (newFloatValue != Target.floatValue)
-
{
-
Target.floatValue = newFloatValue;
-
}
-
//绘制Object类型字段 通过as转化为其它类型
-
AudioClip newClipValue = EditorGUILayout.ObjectField("AudioClip Value", Target.clipValue, typeof(AudioClip), false) as AudioClip;
-
if (newClipValue != Target.clipValue)
-
{
-
Target.clipValue = newClipValue;
-
}
-
//绘制密文类型的String字段
-
string newPasswordValue = EditorGUILayout.PasswordField("Password Value", Target.passwordValue);
-
if(newPasswordValue != Target.passwordValue)
-
{
-
Target.passwordValue = newPasswordValue;
-
}
-
//绘制枚举类型字段
-
Test.TestEnum newEnumValue = (Test.TestEnum)EditorGUILayout.EnumPopup("Enum Value", Target.EnumValue);
-
if (newEnumValue != Target.EnumValue)
-
{
-
Target.EnumValue = newEnumValue;
-
}
-
}
-
}
2.滑动条
-
using UnityEngine;
-
using UnityEditor;
-
-
[CustomEditor(typeof(Test))]
-
public class TestEditor : Editor
-
{
-
private Test Target;
-
private void OnEnable()
-
{
-
Target = target as Test;
-
}
-
-
public override void OnInspectorGUI()
-
{
-
//绘制Int类型滑动条
-
int newIntValue = EditorGUILayout.IntSlider("Int Value", Target.intValue, 0, 30);
-
if (newIntValue != Target.intValue)
-
{
-
Target.intValue = newIntValue;
-
}
-
//绘制Float类型滑动条
-
float newFloatValue = EditorGUILayout.Slider("Float Value", Target.floatValue, 0f, 100f);
-
if (newFloatValue != Target.floatValue)
-
{
-
Target.floatValue = newFloatValue;
-
}
-
}
-
}
文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。
原文链接:coderz.blog.csdn.net/article/details/116796830
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)