Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.46 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4. using System.IO;
  5. using Sirenix.OdinInspector.Editor;
  6. using UnityEditor;
  7. using Sirenix.Utilities.Editor;
  8. using Sirenix.Utilities;
  9.  
  10. namespace Monster
  11. {
  12.     using Data;
  13.  
  14.     [CustomEditor(typeof(StaticGameData), true, isFallback = true), CanEditMultipleObjects]
  15.     public class StaticGameDataInspector : OdinEditor
  16.     {
  17.         private bool initialize = true;
  18.  
  19.         public override void OnInspectorGUI()
  20.         {
  21.             StaticGameData obj = target as StaticGameData;
  22.  
  23.             InspectorUtilities.BeginDrawPropertyTree(Tree, true);
  24.             InspectorProperty parent = Tree.GetPropertyAtPath("Parent");
  25.             bool newParent = initialize && obj.Parent != null;
  26.             Func<bool> hasParent = () => { return obj.ValidateParent((StaticGameData)parent.ValueEntry.WeakSmartValue) && parent.ValueEntry.WeakSmartValue != null; };
  27.  
  28.             EditorGUI.BeginChangeCheck();
  29.             parent.Draw();
  30.             if (EditorGUI.EndChangeCheck() && hasParent())
  31.                 newParent = true;
  32.  
  33.             foreach (var prop in Tree.EnumerateTree(false))
  34.             {
  35.                 if (prop.Name != "Parent" && prop.Name != "Overrides" && prop.Info.PropertyType != PropertyType.Method)
  36.                 {
  37.                     SirenixEditorGUI.BeginHorizontalToolbar();
  38.                     bool enable = true;
  39.                     InspectorProperty overrides = Tree.GetPropertyAtPath("Overrides.{\"" + prop.Name + "\"}");
  40.                     var overrideValue = (bool)overrides.ValueEntry.WeakSmartValue;
  41.  
  42.                     EditorGUI.BeginChangeCheck();
  43.                     overrideValue = SirenixEditorGUI.ToolbarToggle(overrideValue, (overrideValue) ? EditorIcons.LockUnloacked : EditorIcons.LockLocked);
  44.                     if (!overrideValue)
  45.                     {
  46.                         if (EditorGUI.EndChangeCheck() || newParent)
  47.                         {
  48.  
  49.                             if (hasParent())
  50.                             {
  51.                                 var val = obj.GetType().GetField(prop.DeepReflectionPath).GetMemberValue(parent.ValueEntry.WeakSmartValue);
  52.                                 if (val != null && IsAssignableToGenericType(val.GetType(), typeof(IEnumerable<>)))
  53.                                 {
  54.                                     using (var ms = new MemoryStream())
  55.                                     {
  56.                                         var formatter = new BinaryFormatter();
  57.                                         formatter.Serialize(ms, val);
  58.                                         ms.Position = 0;
  59.                                         val = formatter.Deserialize(ms);
  60.                                     }
  61.                                 }
  62.                                 prop.ValueEntry.WeakSmartValue = val;
  63.                             }
  64.                         }
  65.                         if (hasParent())
  66.                             enable = false;
  67.                     }
  68.                     else
  69.                         EditorGUI.EndChangeCheck();
  70.  
  71.                     overrides.ValueEntry.WeakSmartValue = overrideValue;
  72.  
  73.                     GUIHelper.PushGUIEnabled(enable);
  74.                     prop.Draw();
  75.                     GUIHelper.PopGUIEnabled();
  76.                     SirenixEditorGUI.EndHorizontalToolbar();
  77.                 }
  78.             }
  79.             InspectorUtilities.EndDrawPropertyTree(Tree);
  80.  
  81.  
  82.             initialize = false;
  83.             this.RepaintIfRequested();
  84.         }
  85.  
  86.         private void ForEachTarget(UnityEngine.Object[] targets, Action<StaticGameData> action)
  87.         {
  88.             StaticGameData[] sgd = (StaticGameData[])targets;
  89.             sgd.ForEach(action);
  90.         }
  91.  
  92.         private static bool IsAssignableToGenericType(Type givenType, Type genericType)
  93.         {
  94.             var interfaceTypes = givenType.GetInterfaces();
  95.  
  96.             foreach (var it in interfaceTypes)
  97.             {
  98.                 if (it.IsGenericType && it.GetGenericTypeDefinition() == genericType)
  99.                     return true;
  100.             }
  101.  
  102.             if (givenType.IsGenericType && givenType.GetGenericTypeDefinition() == genericType)
  103.                 return true;
  104.  
  105.             Type baseType = givenType.BaseType;
  106.             if (baseType == null)
  107.                 return false;
  108.  
  109.             return IsAssignableToGenericType(baseType, genericType);
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement