123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #if ENABLE_VIEW
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using UnityEditor;
- using UnityEngine;
- namespace ET
- {
- [CustomEditor(typeof (ComponentView))]
- public class ComponentViewEditor: Editor
- {
- public override void OnInspectorGUI()
- {
- ComponentView componentView = (ComponentView) target;
- Entity component = componentView.Component;
- ComponentViewHelper.Draw(component);
- }
- }
- public static class ComponentViewHelper
- {
- private static readonly List<ITypeDrawer> typeDrawers = new List<ITypeDrawer>();
- static ComponentViewHelper()
- {
- Assembly assembly = typeof (ComponentViewHelper).Assembly;
- foreach (Type type in assembly.GetTypes())
- {
- if (!type.IsDefined(typeof (TypeDrawerAttribute)))
- {
- continue;
- }
- ITypeDrawer iTypeDrawer = (ITypeDrawer) Activator.CreateInstance(type);
- typeDrawers.Add(iTypeDrawer);
- }
- }
-
- public static void Draw(Entity entity)
- {
- try
- {
- FieldInfo[] fields = entity.GetType()
- .GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
- EditorGUILayout.BeginVertical();
-
- EditorGUILayout.LongField("InstanceId: ", entity.InstanceId);
-
- EditorGUILayout.LongField("Id: ", entity.Id);
- foreach (FieldInfo fieldInfo in fields)
- {
- Type type = fieldInfo.FieldType;
- if (type.IsDefined(typeof (HideInInspector), false))
- {
- continue;
- }
- if (fieldInfo.IsDefined(typeof (HideInInspector), false))
- {
- continue;
- }
- object value = fieldInfo.GetValue(entity);
- foreach (ITypeDrawer typeDrawer in typeDrawers)
- {
- if (!typeDrawer.HandlesType(type))
- {
- continue;
- }
- string fieldName = fieldInfo.Name;
- if (fieldName.Length > 17 && fieldName.Contains("k__BackingField"))
- {
- fieldName = fieldName.Substring(1, fieldName.Length - 17);
- }
- value = typeDrawer.DrawAndGetNewValue(type, fieldName, value, null);
- fieldInfo.SetValue(entity, value);
- break;
- }
- }
- EditorGUILayout.EndVertical();
- }
- catch (Exception e)
- {
- UnityEngine.Debug.Log($"component view error: {entity.GetType().FullName} {e}");
- }
- }
- }
- }
- #endif
|