Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.IO;
- using Sirenix.OdinInspector.Editor;
- using UnityEditor;
- using Sirenix.Utilities.Editor;
- using Sirenix.Utilities;
- namespace Monster
- {
- using Data;
- [CustomEditor(typeof(StaticGameData), true, isFallback = true), CanEditMultipleObjects]
- public class StaticGameDataInspector : OdinEditor
- {
- private bool initialize = true;
- public override void OnInspectorGUI()
- {
- StaticGameData obj = target as StaticGameData;
- InspectorUtilities.BeginDrawPropertyTree(Tree, true);
- InspectorProperty parent = Tree.GetPropertyAtPath("Parent");
- bool newParent = initialize && obj.Parent != null;
- Func<bool> hasParent = () => { return obj.ValidateParent((StaticGameData)parent.ValueEntry.WeakSmartValue) && parent.ValueEntry.WeakSmartValue != null; };
- EditorGUI.BeginChangeCheck();
- parent.Draw();
- if (EditorGUI.EndChangeCheck() && hasParent())
- newParent = true;
- foreach (var prop in Tree.EnumerateTree(false))
- {
- if (prop.Name != "Parent" && prop.Name != "Overrides" && prop.Info.PropertyType != PropertyType.Method)
- {
- SirenixEditorGUI.BeginHorizontalToolbar();
- bool enable = true;
- InspectorProperty overrides = Tree.GetPropertyAtPath("Overrides.{\"" + prop.Name + "\"}");
- var overrideValue = (bool)overrides.ValueEntry.WeakSmartValue;
- EditorGUI.BeginChangeCheck();
- overrideValue = SirenixEditorGUI.ToolbarToggle(overrideValue, (overrideValue) ? EditorIcons.LockUnloacked : EditorIcons.LockLocked);
- if (!overrideValue)
- {
- if (EditorGUI.EndChangeCheck() || newParent)
- {
- if (hasParent())
- {
- var val = obj.GetType().GetField(prop.DeepReflectionPath).GetMemberValue(parent.ValueEntry.WeakSmartValue);
- if (val != null && IsAssignableToGenericType(val.GetType(), typeof(IEnumerable<>)))
- {
- using (var ms = new MemoryStream())
- {
- var formatter = new BinaryFormatter();
- formatter.Serialize(ms, val);
- ms.Position = 0;
- val = formatter.Deserialize(ms);
- }
- }
- prop.ValueEntry.WeakSmartValue = val;
- }
- }
- if (hasParent())
- enable = false;
- }
- else
- EditorGUI.EndChangeCheck();
- overrides.ValueEntry.WeakSmartValue = overrideValue;
- GUIHelper.PushGUIEnabled(enable);
- prop.Draw();
- GUIHelper.PopGUIEnabled();
- SirenixEditorGUI.EndHorizontalToolbar();
- }
- }
- InspectorUtilities.EndDrawPropertyTree(Tree);
- initialize = false;
- this.RepaintIfRequested();
- }
- private void ForEachTarget(UnityEngine.Object[] targets, Action<StaticGameData> action)
- {
- StaticGameData[] sgd = (StaticGameData[])targets;
- sgd.ForEach(action);
- }
- private static bool IsAssignableToGenericType(Type givenType, Type genericType)
- {
- var interfaceTypes = givenType.GetInterfaces();
- foreach (var it in interfaceTypes)
- {
- if (it.IsGenericType && it.GetGenericTypeDefinition() == genericType)
- return true;
- }
- if (givenType.IsGenericType && givenType.GetGenericTypeDefinition() == genericType)
- return true;
- Type baseType = givenType.BaseType;
- if (baseType == null)
- return false;
- return IsAssignableToGenericType(baseType, genericType);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement