FEditorCache.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using System.Collections.Generic;
  5. using Flux;
  6. namespace FluxEditor
  7. {
  8. /**
  9. * @brief Used to cache the editor classes that will be used to handle the individual elements
  10. * (e.g. FTimelineEditor, FEventEditor).
  11. */
  12. public class FEditorCache : ScriptableObject
  13. {
  14. // runtime dictionary used to hold the editor cache
  15. private Dictionary<int, FEditor> _editorHash;
  16. // used to save all the editor dictionary ids
  17. [SerializeField]
  18. private List<int> _editorHashKeys = new List<int>();
  19. // used to save all the editor dictionary values
  20. [SerializeField]
  21. private List<FEditor> _editorHashValues = new List<FEditor>();
  22. public void OnEnable()
  23. {
  24. hideFlags = HideFlags.DontSave;
  25. // Debug.LogWarning ("Creating EditorCache");
  26. }
  27. public T GetEditor<T>( FObject obj ) where T : FEditor
  28. {
  29. if( _editorHash == null )
  30. {
  31. Refresh();
  32. // editorHash = new Dictionary<int, CEditor>();
  33. //
  34. // int numEditors = editorHashKeys.Count;
  35. //
  36. // for( int i = 0; i != numEditors; ++i )
  37. // {
  38. // editorHash.Add( editorHashKeys[i], editorHashValues[i] );
  39. // }
  40. }
  41. if( obj == null )
  42. {
  43. Debug.Log ("obj is null" );
  44. }
  45. if( _editorHash.ContainsKey( obj.GetInstanceID() ) )
  46. {
  47. //T editor = editorHash[obj.GetInstanceID()];
  48. //editor.Init( editor
  49. // Debug.LogWarning ("Contains key! " + obj.GetInstanceID() + " selected? " + editorHash[obj.GetInstanceID()].IsSelected() + " id " + editorHash[obj.GetInstanceID()].GetInstanceID() );
  50. return (T)_editorHash[obj.GetInstanceID()];
  51. }
  52. Type[] allTypes = typeof( FEditor ).Assembly.GetTypes();
  53. Type editorType = typeof( T );
  54. Type bestEditorType = editorType;
  55. Type objType = obj.GetType();
  56. Type closestObjType = objType;
  57. foreach( Type type in allTypes )
  58. {
  59. if( !type.IsSubclassOf( editorType ) )
  60. continue;
  61. object[] attributes = type.GetCustomAttributes( false );
  62. foreach( object o in attributes )
  63. {
  64. if( !(o is FEditorAttribute) )
  65. continue;
  66. FEditorAttribute editorAttribute = (FEditorAttribute)o;
  67. if( editorAttribute.type == objType )
  68. {
  69. bestEditorType = type;
  70. break;
  71. }
  72. if( editorAttribute.type.IsAssignableFrom( objType ) && editorAttribute.type.IsSubclassOf( closestObjType ) )
  73. {
  74. bestEditorType = type;
  75. closestObjType = editorAttribute.type;
  76. }
  77. }
  78. }
  79. T editor = (T)Editor.CreateInstance( bestEditorType );
  80. // editor.Init( this, obj );
  81. _editorHash.Add( obj.GetInstanceID(), editor );
  82. _editorHashKeys.Add( obj.GetInstanceID() );
  83. _editorHashValues.Add( editor );
  84. #if FLUX_DEBUG
  85. Debug.LogWarning("Creating new.. " + obj.GetInstanceID() + " selected? " + _editorHash[obj.GetInstanceID()].IsSelected() + " id " + _editorHash[obj.GetInstanceID()].GetInstanceID() + " name : " + obj.name );
  86. #endif
  87. return editor;
  88. }
  89. public void Remove( FEditor editor )
  90. {
  91. if( _editorHash == null )
  92. Refresh();
  93. _editorHash.Remove( editor.Obj.GetInstanceID() );
  94. _editorHashKeys.Remove( editor.Obj.GetInstanceID() );
  95. _editorHashValues.Remove( editor );
  96. }
  97. public void Refresh()
  98. {
  99. if( _editorHash == null )
  100. _editorHash = new Dictionary<int, FEditor>();
  101. else
  102. _editorHash.Clear();
  103. for( int i = 0; i < _editorHashValues.Count; ++i )
  104. {
  105. FObject runtimeObj = _editorHashValues[i] == null ? null : _editorHashValues[i].Obj;
  106. // if( runtimeObj == null && !object.Equals(runtimeObj, null) )
  107. // {
  108. // runtimeObj = (FObject)EditorUtility.InstanceIDToObject( runtimeObj.GetInstanceID() );
  109. // _editorHashValues[i].Obj = runtimeObj;
  110. // }
  111. if( _editorHashValues[i] == null || object.Equals(runtimeObj, null) )
  112. {
  113. _editorHashKeys.RemoveAt( i );
  114. _editorHashValues.RemoveAt( i );
  115. --i;
  116. }
  117. else
  118. {
  119. if( runtimeObj == null ) // unity null, so it has an instance ID
  120. {
  121. _editorHashValues[i].RefreshRuntimeObject();
  122. }
  123. _editorHash.Add( _editorHashKeys[i], _editorHashValues[i] );
  124. }
  125. }
  126. }
  127. public void Clear()
  128. {
  129. if( _editorHash == null )
  130. _editorHash = new Dictionary<int, FEditor>();
  131. else
  132. _editorHash.Clear();
  133. for( int i = 0; i < _editorHashValues.Count; ++i )
  134. {
  135. if( _editorHashValues[i] != null )
  136. Editor.DestroyImmediate( _editorHashValues[i] );
  137. }
  138. _editorHashKeys.Clear();
  139. _editorHashValues.Clear();
  140. }
  141. }
  142. }