经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
Unity检视面板的继承方法研究 (二)
来源:cnblogs  作者:tiancaiKG  时间:2019/8/28 8:37:59  对本文有异议

  之前做了普通对象的可继承的检视面板类, 现在想要实现对Unity自带的检视面板的继承的话, 要怎样写呢?

  万变不离其宗,  仍然是围绕UnityEditor.Editor.CreateEditor 这个函数来实现:

  1. /// <summary>
  2. /// decorate Unity's built-in inspector Editor.
  3. /// </summary>
  4. public class DecoratorEditor<T> : UnityEditor.Editor
  5. {
  6. protected T _target;
  7. protected UnityEditor.Editor _nativeEditor;
  8. public void OnEnable()
  9. {
  10. _target = (T)System.Convert.ChangeType(target, typeof(T));
  11. Type targetEditorType = null;
  12. foreach(Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
  13. {
  14. var tempT = assembly.GetType("UnityEditor." + typeof(T).Name + "Inspector");
  15. if(tempT != null)
  16. {
  17. targetEditorType = tempT;
  18. break;
  19. }
  20. }
  21. if(targetEditorType != null)
  22. {
  23. _nativeEditor = UnityEditor.Editor.CreateEditor(serializedObject.targetObject, targetEditorType);
  24. }
  25. else
  26. {
  27. _nativeEditor = UnityEditor.Editor.CreateEditor(serializedObject.targetObject);
  28. }
  29. }
  30. public override void OnInspectorGUI()
  31. {
  32. if(_nativeEditor != null)
  33. {
  34. _nativeEditor.OnInspectorGUI();
  35. }
  36. }
  37. }

  这里对于内置Unity Inspector的类型查找几乎是在走钢丝, 也只能碰运气了, 好在我只用来修改了一下TextureImporter的检视面板, 因为Unity2019里面没有显示spritePackingTag这个设置了, 可是这个序列化仍然存在.

  1. #if UNITY_2019_1_OR_NEWER
  2. [CustomEditor(typeof(TextureImporter))]
  3. public class TextureImporterCustomEditor : DecoratorEditor<TextureImporter>
  4. {
  5. public override void OnInspectorGUI()
  6. {
  7. if(_target.textureType == TextureImporterType.Sprite)
  8. {
  9. _target.spritePackingTag = EditorGUILayout.TextField("Packing Tag", _target.spritePackingTag);
  10. }
  11. base.OnInspectorGUI();
  12. }
  13. }
  14. #endif

  这样我的设置又回来了.

原文链接:http://www.cnblogs.com/tiancaiwrk/p/11418387.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号