EntityBehaviourInspector.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using UnityEditor;
  2. using UnityEngine;
  3. using XGame.Framework.Map;
  4. namespace XGame.Editor.Map
  5. {
  6. [CustomEditor(typeof(EntityBehaviour))]
  7. public class EntityBehaviourInspector : UnityEditor.Editor
  8. {
  9. private static readonly string[] EntityTypes = new string[] {"Entity", "MapUI" };
  10. private EntityBehaviour _behaviour;
  11. private int _selectedIdx = 0;
  12. private void OnEnable()
  13. {
  14. _behaviour = serializedObject.targetObject as EntityBehaviour;
  15. }
  16. private void OnDisable()
  17. {
  18. _behaviour = null;
  19. }
  20. public override void OnInspectorGUI()
  21. {
  22. base.OnInspectorGUI();
  23. serializedObject.Update();
  24. // 运行时不显示代码生成相关
  25. if (Application.isPlaying) return;
  26. GUILayout.Space(20);
  27. EditorGUILayout.BeginVertical("Box");
  28. DrawCodeTitle();
  29. DrawGenerateBtn();
  30. EditorGUILayout.EndVertical();
  31. }
  32. private void DrawCodeTitle()
  33. {
  34. var titleStyle = new GUIStyle("Label")
  35. {
  36. fontSize = 25,
  37. alignment = TextAnchor.MiddleCenter,
  38. fontStyle = FontStyle.BoldAndItalic
  39. };
  40. GUILayout.Box("以下为代码生成区域", titleStyle);
  41. GUILayout.Space(10);
  42. var labelContent = new GUIContent("先选择预制类型:", "MapUI只生成View和VM脚本,Entity额外生成AI控制脚本");
  43. _selectedIdx = EditorGUILayout.Popup(labelContent, _selectedIdx, EntityTypes);
  44. GUILayout.Space(10);
  45. }
  46. private void DrawGenerateBtn()
  47. {
  48. GUILayout.FlexibleSpace();
  49. GUILayout.BeginVertical();
  50. GUIStyle style = new GUIStyle("Button")
  51. {
  52. alignment = TextAnchor.MiddleCenter,
  53. fontStyle = FontStyle.BoldAndItalic,
  54. fontSize = 15,
  55. };
  56. GUI.color = Color.green;
  57. if (GUILayout.Button("Generator", style, GUILayout.Height(40)))
  58. {
  59. (new EntityViewCodeGen()).Gen(_behaviour, _selectedIdx);
  60. }
  61. GUI.color = Color.white;
  62. GUILayout.EndVertical();
  63. }
  64. }
  65. }