FEditorInspector.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using System.Collections.Generic;
  5. using Flux;
  6. namespace FluxEditor
  7. {
  8. [Serializable]
  9. public abstract class FEditorInspector<FEDITOR,FOBJECT> where FEDITOR : FEditor where FOBJECT : FObject {
  10. public virtual string Title { get { return "Inspetor"; } }
  11. [SerializeField]
  12. protected List<FEDITOR> _editors = new List<FEDITOR>();
  13. public List<FEDITOR> Editors { get { return _editors; }}
  14. [SerializeField]
  15. protected List<FOBJECT> _objects = new List<FOBJECT>();
  16. public List<FOBJECT> Objects { get { return _objects; }}
  17. [SerializeField]
  18. protected Editor _inspector = null;
  19. [SerializeField]
  20. protected FMultiTypeInspector<FOBJECT> _multiTypeInspector = null;
  21. private bool _allSameType = true;
  22. public bool AllSameType { get { return _allSameType; } }
  23. private bool _isDirty = false;
  24. public bool IsDirty { get { return _isDirty; } }
  25. protected virtual FMultiTypeInspector<FOBJECT> CreateMultiTypeInspector(){ return null; }
  26. // public void SetEditors( List<FEDITOR> editors )
  27. // {
  28. // _editors = editors;
  29. // Refresh();
  30. // }
  31. public void Add( FEDITOR editor )
  32. {
  33. _editors.Add( editor );
  34. _isDirty = true;
  35. }
  36. public void Remove( FEDITOR editor )
  37. {
  38. _editors.Remove( editor );
  39. _isDirty = true;
  40. }
  41. public void Clear()
  42. {
  43. _editors.Clear();
  44. _isDirty = true;
  45. }
  46. public virtual void Refresh()
  47. {
  48. _objects.Clear();
  49. _allSameType = true;
  50. for( int i = 0; i != _editors.Count; ++i )
  51. {
  52. if( !_editors[i].IsSelected )
  53. _editors[i].OnSelect();
  54. FOBJECT obj = (FOBJECT)_editors[i].Obj;
  55. _objects.Add( obj );
  56. if( obj.GetType() != _editors[0].Obj.GetType() )
  57. _allSameType = false;
  58. }
  59. if( _inspector != null )
  60. {
  61. Editor.DestroyImmediate( _inspector );
  62. }
  63. if( _multiTypeInspector != null )
  64. ScriptableObject.DestroyImmediate( _multiTypeInspector );
  65. if( _objects.Count > 0 )
  66. {
  67. if( _allSameType )
  68. _inspector = Editor.CreateEditor( _objects.ToArray() );
  69. else
  70. _multiTypeInspector = CreateMultiTypeInspector();
  71. }
  72. _isDirty = false;
  73. }
  74. public virtual void OnInspectorGUI( float contentWidth )
  75. {
  76. if( IsDirty )
  77. Refresh();
  78. if( _objects.Count > 0 )
  79. {
  80. EditorGUILayout.BeginVertical(EditorStyles.textArea, GUILayout.Width(contentWidth));
  81. EditorGUILayout.LabelField(Title, EditorStyles.boldLabel);
  82. if( _inspector != null && _inspector.target != null )
  83. _inspector.OnInspectorGUI();
  84. else if( _multiTypeInspector != null )
  85. _multiTypeInspector.OnInspectorGUI();
  86. EditorGUILayout.EndVertical();
  87. }
  88. }
  89. }
  90. }