十二、Unity编辑器开发之各类字段及滑动条绘制

举报
CoderZ1010 发表于 2022/09/25 05:36:52 2022/09/25
【摘要】 1.各类字段: 为我们的Test类中添加以下字段: using UnityEngine; public class Test : MonoBehaviour{ public enum TestEnum { Enum1, Enum2, } public string stringV...

1.各类字段:

为我们的Test类中添加以下字段:


  
  1. using UnityEngine;
  2. public class Test : MonoBehaviour
  3. {
  4. public enum TestEnum
  5. {
  6. Enum1,
  7. Enum2,
  8. }
  9. public string stringValue = "str";
  10. public int intValue = 30;
  11. public float floatValue = 50f;
  12. public AudioClip clipValue;
  13. public string passwordValue = "1a2s3xd455";
  14. public TestEnum EnumValue = TestEnum.Enum1;
  15. }

在TestEditor类中来绘制这些字段:


  
  1. using UnityEngine;
  2. using UnityEditor;
  3. ​
  4. [CustomEditor(typeof(Test))]
  5. public class TestEditor : Editor
  6. {
  7. private Test Target;
  8. private void OnEnable()
  9. {
  10. Target = target as Test;
  11. }
  12. ​
  13. public override void OnInspectorGUI()
  14. {
  15. //绘制String类型字段
  16. string newStringValue = EditorGUILayout.TextField("String Value", Target.stringValue);
  17. if (newStringValue != Target.stringValue)
  18. {
  19. Target.stringValue = newStringValue;
  20. }
  21. //绘制Int类型字段
  22. int newIntValue = EditorGUILayout.IntField("Int Value", Target.intValue);
  23. if (newIntValue != Target.intValue)
  24. {
  25. Target.intValue = newIntValue;
  26. }
  27. //绘制Float类型字段
  28. float newFloatValue = EditorGUILayout.FloatField("Float Value", Target.floatValue);
  29. if (newFloatValue != Target.floatValue)
  30. {
  31. Target.floatValue = newFloatValue;
  32. }
  33. //绘制Object类型字段 通过as转化为其它类型
  34. AudioClip newClipValue = EditorGUILayout.ObjectField("AudioClip Value", Target.clipValue, typeof(AudioClip), false) as AudioClip;
  35. if (newClipValue != Target.clipValue)
  36. {
  37. Target.clipValue = newClipValue;
  38. }
  39. //绘制密文类型的String字段
  40. string newPasswordValue = EditorGUILayout.PasswordField("Password Value", Target.passwordValue);
  41. if(newPasswordValue != Target.passwordValue)
  42. {
  43. Target.passwordValue = newPasswordValue;
  44. }
  45. //绘制枚举类型字段
  46. Test.TestEnum newEnumValue = (Test.TestEnum)EditorGUILayout.EnumPopup("Enum Value", Target.EnumValue);
  47. if (newEnumValue != Target.EnumValue)
  48. {
  49. Target.EnumValue = newEnumValue;
  50. }
  51. }
  52. }

2.滑动条


  
  1. using UnityEngine;
  2. using UnityEditor;
  3. ​
  4. [CustomEditor(typeof(Test))]
  5. public class TestEditor : Editor
  6. {
  7. private Test Target;
  8. private void OnEnable()
  9. {
  10. Target = target as Test;
  11. }
  12. ​
  13. public override void OnInspectorGUI()
  14. {
  15. //绘制Int类型滑动条
  16. int newIntValue = EditorGUILayout.IntSlider("Int Value", Target.intValue, 0, 30);
  17. if (newIntValue != Target.intValue)
  18. {
  19. Target.intValue = newIntValue;
  20. }
  21. //绘制Float类型滑动条
  22. float newFloatValue = EditorGUILayout.Slider("Float Value", Target.floatValue, 0f, 100f);
  23. if (newFloatValue != Target.floatValue)
  24. {
  25. Target.floatValue = newFloatValue;
  26. }
  27. }
  28. }

文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。

原文链接:coderz.blog.csdn.net/article/details/116796830

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0)

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。