Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 KB | None | 0 0
  1. public class AssetReferenceDrawer<T> : OdinValueDrawer<T>, IDisposable
  2.     where T : AssetReference
  3. {
  4.     private SerializedProperty serializedProperty;
  5.     private UnityPropertyEmitter.Handle handle;
  6.  
  7.     protected override void Initialize()
  8.     {
  9.         if (this.Property.Tree.UnitySerializedObject != null)
  10.         {
  11.             this.serializedProperty = this.Property.Tree.UnitySerializedObject.FindProperty(this.Property.UnityPropertyPath);
  12.         }
  13.  
  14.         if (this.serializedProperty == null)
  15.         {
  16.             GameObject go = null;
  17.  
  18.             this.handle = UnityPropertyEmitter.CreateEmittedMonoBehaviourProperty(this.Property.Name, typeof(T), this.ValueEntry.ValueCount, ref go);
  19.             this.serializedProperty = this.handle.UnityProperty;
  20.         }
  21.        
  22.     }
  23.  
  24.     protected override void DrawPropertyLayout(GUIContent label)
  25.     {
  26.         if (this.serializedProperty == null)
  27.         {
  28.             // This won't work, hope for the best further down
  29.             SirenixEditorGUI.ErrorMessageBox("Failed to get property for AssetReference");
  30.             this.CallNextDrawer(label);
  31.             return;
  32.         }
  33.  
  34.         if (this.handle != null) // Emitted property preparation
  35.         {
  36.             for (int i = 0; i < this.ValueEntry.ValueCount; i++)
  37.             {
  38.                 var emitted = this.handle.Objects[i] as EmittedMonoBehaviour<T>;
  39.  
  40.                 if (emitted != null)
  41.                 {
  42.                     emitted.SetValue(this.ValueEntry.Values[i]);
  43.                 }
  44.             }
  45.  
  46.             this.serializedProperty.serializedObject.Update();
  47.             this.serializedProperty = this.serializedProperty.serializedObject.FindProperty(this.serializedProperty.propertyPath);
  48.         }
  49.  
  50.         if (label == null) label = GUIContent.none;
  51.  
  52.         if (this.handle == null) EditorGUI.BeginChangeCheck();
  53.         EditorGUILayout.PropertyField(this.serializedProperty, label);
  54.         if (this.handle == null && EditorGUI.EndChangeCheck())
  55.         {
  56.             this.ValueEntry.Values.ForceMarkDirty();
  57.         }
  58.  
  59.         if (this.handle != null) // Emitted property post-drawing stuff
  60.         {
  61.             this.serializedProperty.serializedObject.ApplyModifiedPropertiesWithoutUndo();
  62.  
  63.             for (int i = 0; i < this.ValueEntry.ValueCount; i++)
  64.             {
  65.                 var emitted = this.handle.Objects[i] as EmittedMonoBehaviour<T>;
  66.  
  67.                 if (emitted != null)
  68.                 {
  69.                     this.ValueEntry.Values[i] = emitted.GetValue();
  70.                 }
  71.             }
  72.         }
  73.     }
  74.  
  75.     public void Dispose()
  76.     {
  77.         if (this.handle != null)
  78.         {
  79.             this.handle.Dispose();
  80.             this.handle = null;
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement