GameRes游资网

 找回密码
 立即注册
返回列表
查看: 271|回复: 0

扩展Unity Inspector

[复制链接]
发表于 2018-12-28 18:35:02 | 显示全部楼层 |阅读模式
游戏程序
平台类型: PC/兼容机 
程序设计: 客户端 
编程语言: C# 
引擎/SDK: Unity3D 
Unity Editor下,可以在不改变原有布局的情况下扩展Inspect的界面。
在继承了Editor的类中,有两种实现方式:
  1. <p class="MsoNormal"><span lang="EN-US">using UnityEditor;<o:p></o:p></span></p>

  2. <p class="MsoNormal"><span lang="EN-US">               <o:p></o:p></span></p>

  3. <p class="MsoNormal"><span lang="EN-US">[CustomEditor(typeof(CustomClass))]<o:p></o:p></span></p>

  4. <p class="MsoNormal"><span lang="EN-US">public class CustomClassEditor : Editor {<o:p></o:p></span></p>

  5. <p class="MsoNormal"><span lang="EN-US">   
  6. public override void OnInspectorGUI() {<o:p></o:p></span></p>

  7. <p class="MsoNormal"><span lang="EN-US">      
  8. // </span><span style="font-family:宋体;mso-ascii-font-family:Calibri;
  9. mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:
  10. minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">自定义代码</span><span lang="EN-US"><o:p></o:p></span></p>

  11. <p class="MsoNormal"><span lang="EN-US">      
  12. base.OnInspectorGUI();<o:p></o:p></span></p>

  13. <p class="MsoNormal"><span lang="EN-US">      
  14. // </span><span style="font-family:宋体;mso-ascii-font-family:Calibri;
  15. mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:
  16. minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">自定义代码</span><span lang="EN-US"><o:p></o:p></span></p>

  17. <p class="MsoNormal"><span lang="EN-US">    }<o:p></o:p></span></p>

  18. <p class="MsoNormal"><span lang="EN-US">}</span></p>
复制代码

  1. <p class="MsoNormal"><span lang="EN-US">using UnityEditor;<o:p></o:p></span></p>

  2. <p class="MsoNormal"><span lang="EN-US"> </span></p>

  3. <p class="MsoNormal"><span lang="EN-US">[CustomEditor(typeof(CustomClass))]<o:p></o:p></span></p>

  4. <p class="MsoNormal"><span lang="EN-US">public class CustomClassEditor : Editor {<o:p></o:p></span></p>

  5. <p class="MsoNormal"><span lang="EN-US">   
  6. public override void OnInspectorGUI() {<o:p></o:p></span></p>

  7. <p class="MsoNormal"><span lang="EN-US">      
  8. // </span><span style="font-family:宋体;mso-ascii-font-family:Calibri;
  9. mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:
  10. minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">自定义代码</span><span lang="EN-US"><o:p></o:p></span></p>

  11. <p class="MsoNormal"><span lang="EN-US">      
  12. DrawDefaultInspector();<o:p></o:p></span></p>

  13. <p class="MsoNormal"><span lang="EN-US">      
  14. // </span><span style="font-family:宋体;mso-ascii-font-family:Calibri;
  15. mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:
  16. minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">自定义代码</span><span lang="EN-US"><o:p></o:p></span></p>

  17. <p class="MsoNormal"><span lang="EN-US">    }<o:p></o:p></span></p>

  18. <p class="MsoNormal"><span lang="EN-US">}</span></p>
复制代码

当然,这两种方式对开发者自定义的组件的显示是没有问题的,因为自定义的组件本身的绘制方式就是利用的Editor类中的OnInspectorGUI方法。
Unity内置的一些组件,比如说AudioSource这个组件,有的开发者发现扩展了它以后,显示在Inspector中的样式跟默认的有很大区别。
代码:
  1. <p class="MsoNormal"><span lang="EN-US">using UnityEngine;<o:p></o:p></span></p>

  2. <p class="MsoNormal"><span lang="EN-US">using UnityEditor;<o:p></o:p></span></p>

  3. <p class="MsoNormal"><span lang="EN-US"> </span></p>

  4. <p class="MsoNormal"><span lang="EN-US">[CustomEditor(typeof(AudioSource))]<o:p></o:p></span></p>

  5. <p class="MsoNormal"><span lang="EN-US">public class CustomAudioSourceEditor :
  6. Editor {<o:p></o:p></span></p>

  7. <p class="MsoNormal"><span lang="EN-US">    public
  8. override void OnInspectorGUI() {<o:p></o:p></span></p>

  9. <p class="MsoNormal"><span lang="EN-US">      
  10. DrawDefaultInspector();<o:p></o:p></span></p>

  11. <p class="MsoNormal"><span lang="EN-US">      
  12. CreateEditor(targets, EditorGUILayout.HelpBox("</span><span style="font-family:宋体;mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;
  13. mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:
  14. Calibri;mso-hansi-theme-font:minor-latin">我最牛逼!</span><span lang="EN-US">",
  15. MessageType.Info);<o:p></o:p></span></p>

  16. <p class="MsoNormal"><span lang="EN-US">    }<o:p></o:p></span></p>

  17. <p class="MsoNormal"><span lang="EN-US">}</span></p>
复制代码

原始图片:
1.png
修改后图片:
2.png
造成这一问题的原因是Unity默认的大部分组件Unity官方代码也通过继承Editor的方式对其样式进行了自定义,因此我们需要得到AudioSourceEditor派生类,调用他的OnInspectorGUI方法。在这里我们通过反射进行调用CreateEditor(targets,typeof(Editor).Assembly.GetType("UnityEditor.AudioSourceInspector")).OnInspectorGUI();来解决样式不一致的问题。
完整代码:
  1. <p class="MsoNormal"><span lang="EN-US">using UnityEngine;<o:p></o:p></span></p>

  2. <p class="MsoNormal"><span lang="EN-US">using UnityEditor;<o:p></o:p></span></p>

  3. <p class="MsoNormal"><span lang="EN-US"> </span></p>

  4. <p class="MsoNormal"><span lang="EN-US">[CustomEditor(typeof(AudioSource))]<o:p></o:p></span></p>

  5. <p class="MsoNormal"><span lang="EN-US">public class CustomAudioSourceEditor :
  6. Editor {<o:p></o:p></span></p>

  7. <p class="MsoNormal"><span lang="EN-US">    public
  8. override void OnInspectorGUI() {<o:p></o:p></span></p>

  9. <p class="MsoNormal"><span lang="EN-US">      
  10. CreateEditor(targets, typeof(Editor).Assembly.GetType("UnityEditor.AudioSourceInspector")).OnInspectorGUI();<o:p></o:p></span></p>

  11. <p class="MsoNormal"><span lang="EN-US">      
  12. EditorGUILayout.HelpBox("</span><span style="font-family:宋体;
  13. mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:
  14. 宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;
  15. mso-hansi-theme-font:minor-latin">我最牛逼!</span><span lang="EN-US">",
  16. MessageType.Info);<o:p></o:p></span></p>

  17. <p class="MsoNormal"><span lang="EN-US">    }<o:p></o:p></span></p>

  18. <p class="MsoNormal"><span lang="EN-US">}</span></p>
复制代码

如图所示:
3.png
更多地Unity技术问题可关注“Paws3D学习中心”:https://www.paws3d.com/learn/
Unity交流qq群:935714213

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|作品发布|文章投稿|广告合作|关于本站|GameRes游资网 ( 闽ICP备05005107-1 )

GMT+8, 2019-3-24 18:52

快速回复 返回顶部 返回列表