using System; using System.Collections.Generic; using System.Reflection; using Sirenix.OdinInspector.Editor; using TEngine; using UnityEditor; using UnityEngine; [CustomEditor(typeof(ComponentView))] public class ComponentViewEditor : OdinEditor { public override void OnInspectorGUI() { ComponentView componentView = (ComponentView)target; Entity component = componentView.Component; ComponentViewHelper.Draw(component); } } public static class ComponentViewHelper { private static readonly List typeDrawers = new List(); 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("RuntimeId: ", entity.RuntimeId); 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); } try { value = typeDrawer.DrawAndGetNewValue(type, fieldName, value, null); } catch (Exception e) { Debug.LogError(e); } fieldInfo.SetValue(entity, value); break; } } EditorGUILayout.EndVertical(); } catch (Exception e) { Debug.Log($"component view error: {entity.GetType().FullName} {e}"); } } }