瀏覽代碼

增加EntityRef的ENABLE_VIEW显示

tanghai 2 年之前
父節點
當前提交
a7f13bcfb5

+ 12 - 11
Unity/Assets/Scripts/Core/Entity/Entity.cs

@@ -28,7 +28,8 @@ namespace ET
     public partial class Entity: DisposeObject
     {
 #if ENABLE_VIEW && UNITY_EDITOR
-        private UnityEngine.GameObject viewGO;
+        [UnityEngine.HideInInspector]
+        public UnityEngine.GameObject ViewGO;
 #endif
 
         [BsonIgnore]
@@ -86,14 +87,14 @@ namespace ET
 #if ENABLE_VIEW && UNITY_EDITOR
                 if (value)
                 {
-                    this.viewGO = new UnityEngine.GameObject(this.ViewName);
-                    this.viewGO.AddComponent<ComponentView>().Component = this;
-                    this.viewGO.transform.SetParent(this.Parent == null? 
-                            UnityEngine.GameObject.Find("Global").transform : this.Parent.viewGO.transform);
+                    this.ViewGO = new UnityEngine.GameObject(this.ViewName);
+                    this.ViewGO.AddComponent<ComponentView>().Component = this;
+                    this.ViewGO.transform.SetParent(this.Parent == null? 
+                            UnityEngine.GameObject.Find("Global").transform : this.Parent.ViewGO.transform);
                 }
                 else
                 {
-                    UnityEngine.Object.Destroy(this.viewGO);
+                    UnityEngine.Object.Destroy(this.ViewGO);
                 }
 #endif
             }
@@ -229,16 +230,16 @@ namespace ET
                 }
 
 #if ENABLE_VIEW && UNITY_EDITOR
-                this.viewGO.GetComponent<ComponentView>().Component = this;
-                this.viewGO.transform.SetParent(this.Parent == null ?
-                        UnityEngine.GameObject.Find("Global").transform : this.Parent.viewGO.transform);
+                this.ViewGO.GetComponent<ComponentView>().Component = this;
+                this.ViewGO.transform.SetParent(this.Parent == null ?
+                        UnityEngine.GameObject.Find("Global").transform : this.Parent.ViewGO.transform);
                 foreach (var child in this.Children.Values)
                 {
-                    child.viewGO.transform.SetParent(this.viewGO.transform);
+                    child.ViewGO.transform.SetParent(this.ViewGO.transform);
                 }
                 foreach (var comp in this.Components.Values)
                 {
-                    comp.viewGO.transform.SetParent(this.viewGO.transform);
+                    comp.ViewGO.transform.SetParent(this.ViewGO.transform);
                 }
 #endif
             }

+ 10 - 1
Unity/Assets/Scripts/Editor/ComponentViewEditor/ComponentViewEditor.cs

@@ -78,7 +78,16 @@ namespace ET
                         {
                             fieldName = fieldName.Substring(1, fieldName.Length - 17);
                         }
-                        value = typeDrawer.DrawAndGetNewValue(type, fieldName, value, null);
+
+                        try
+                        {
+                            value = typeDrawer.DrawAndGetNewValue(type, fieldName, value, null);
+                        }
+                        catch (Exception e)
+                        {
+                            Debug.LogError(e);
+                        }
+                        
                         fieldInfo.SetValue(entity, value);
                         break;
                     }

+ 35 - 0
Unity/Assets/Scripts/Editor/ComponentViewEditor/TypeDrawer/EntityRefTypeDrawer.cs

@@ -0,0 +1,35 @@
+using System;
+using System.Reflection;
+using UnityEditor;
+using UnityEngine;
+
+namespace ET
+{
+    [TypeDrawer]
+    public class EntityRefTypeDrawer: ITypeDrawer
+    {
+        public bool HandlesType(Type type)
+        {
+            if (!type.IsGenericType)
+            {
+                return false;
+            }
+
+            if (type.GetGenericTypeDefinition() == typeof (EntityRef<>))
+            {
+                return true;
+            }
+
+            return false;
+        }
+
+        public object DrawAndGetNewValue(Type memberType, string memberName, object value, object target)
+        {
+            FieldInfo fieldInfo = memberType.GetField("entity", BindingFlags.NonPublic | BindingFlags.Instance);
+            Entity entity = (Entity)fieldInfo.GetValue(value);
+            GameObject go = entity?.ViewGO;
+            EditorGUILayout.ObjectField(memberName, go, memberType, true);
+            return value;
+        }
+    }
+}

+ 11 - 0
Unity/Assets/Scripts/Editor/ComponentViewEditor/TypeDrawer/EntityRefTypeDrawer.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: e13b5d8e25f279649895512cc6b21123
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: