internal void ClearArrayElement()
 {
     if (ValueType.IsArrayOrListType())
     {
         if (ValueType.IsArray)
         {
             Value = DrawerUtility.CreateInstance(ValueType);
         }
         else
         {
             ((IList)Value).Clear();
         }
     }
 }
        internal void AddArrayElement()
        {
            if (ValueType.IsArrayOrListType())
            {
                object element = DrawerUtility.CreateInstance(ValueType.GetElementTypeInArrayOrList());
                if (ValueType.IsArray)
                {
                    Array array = (Array)Value;
                    ArrayUtility.Add(ref array, element);

                    Value = array;
                }
                else
                {
                    ((IList)Value).Add(element);
                }
            }
        }
Exemplo n.º 3
0
        private void InitField()
        {
            Type[] allTypes = DrawerUtility.GetAllBaseTypes(drawerObject.GetType());
            if (allTypes != null)
            {
                foreach (var type in allTypes)
                {
                    TypeDrawerProperty typeDrawerProperty = new TypeDrawerProperty()
                    {
                        type = type,
                    };

                    FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
                    foreach (var field in fields)
                    {
                        DrawerProperty drawerProperty = new DrawerProperty(drawerObject, field);
                        typeDrawerProperty.drawerProperties.Add(drawerProperty);
                    }

                    typeDrawerProperties.Add(typeDrawerProperty);
                }
            }
        }
        internal void OnGUILayout()
        {
            foreach (var drawer in layoutDrawers)
            {
                LayoutAttribute attr = drawer.GetAttr <LayoutAttribute>();
                if (attr.Occasion == LayoutOccasion.Before)
                {
                    drawer.OnGUILayout();
                }
            }

            bool isVisible = IsVisible();

            if (isVisible)
            {
                foreach (var drawer in controlDrawers)
                {
                    drawer.OnStartGUILayout();
                }

                foreach (var drawer in decoratorDrawers)
                {
                    drawer.OnGUILayout();
                }

                foreach (var drawer in verificationDrawers)
                {
                    drawer.OnGUILayout();
                }

                string label = FieldName;
                if (labelDrawer != null)
                {
                    label = labelDrawer.GetLabel();
                }
                if (!string.IsNullOrEmpty(label))
                {
                    label = UnityEditor.ObjectNames.NicifyVariableName(label);
                }
                if (contentDrawer != null)
                {
                    contentDrawer.OnGUILayout(label);
                }
                else if (drawerObject != null)
                {
                    if (!IsArrayElement)
                    {
                        UnityEditor.EditorGUILayout.LabelField(label);
                        UnityEditor.EditorGUI.indentLevel++;
                        {
                            drawerObject.OnGUILayout();
                        }
                        UnityEditor.EditorGUI.indentLevel--;
                    }
                    else
                    {
                        UnityEditor.EditorGUILayout.BeginHorizontal();
                        {
                            UnityEditor.EditorGUILayout.LabelField(label, UnityEngine.GUILayout.Width(25));
                            UnityEditor.EditorGUILayout.BeginVertical();
                            {
                                drawerObject.OnGUILayout();
                            }
                            UnityEditor.EditorGUILayout.EndVertical();
                        }
                        UnityEditor.EditorGUILayout.EndHorizontal();
                    }
                }
                else if (drawerObject == null)
                {
                    if (!DrawerUtility.IsTypeSupported(ValueType))
                    {
                        EGUI.BeginGUIColor(UnityEngine.Color.red);
                        {
                            UnityEditor.EditorGUILayout.LabelField(string.IsNullOrEmpty(label) ? "" : label, $"The type isn't supported.type = {ValueType}");
                        }
                        EGUI.EndGUIColor();
                    }
                    else if (Value == null)
                    {
                        UnityEditor.EditorGUILayout.BeginHorizontal();
                        {
                            UnityEditor.EditorGUILayout.PrefixLabel(label);
                            if (UnityEngine.GUILayout.Button("Create"))
                            {
                                Value = DrawerUtility.CreateInstance(ValueType);

                                if (DrawerUtility.IsTypeSupported(ValueType))
                                {
                                    if (ValueType.IsStructOrClassType() && Value != null)
                                    {
                                        drawerObject = new DrawerObject(Value);
                                    }
                                }
                            }
                        }
                        UnityEditor.EditorGUILayout.EndHorizontal();
                    }
                    else
                    {
                        EGUI.BeginGUIColor(UnityEngine.Color.red);
                        {
                            UnityEditor.EditorGUILayout.LabelField(string.IsNullOrEmpty(label) ? "" : label, "Unknown Drawer");
                        }
                        EGUI.EndGUIColor();
                    }
                }

                foreach (var drawer in controlDrawers)
                {
                    drawer.OnEndGUILayout();
                }
            }

            foreach (var drawer in layoutDrawers)
            {
                LayoutAttribute attr = drawer.GetAttr <LayoutAttribute>();
                if (attr.Occasion == LayoutOccasion.After)
                {
                    drawer.OnGUILayout();
                }
            }
        }
        internal void Init()
        {
            if (!IsArrayElement)
            {
                var drawerAttrs = Field.GetCustomAttributes <DrawerAttribute>();
                foreach (var attr in drawerAttrs)
                {
                    var drawer = DrawerUtility.CreateAttrDrawer(this, attr);
                    if (drawer == null)
                    {
                        Debug.LogWarning("DrawerProperty::Init->drawer not found.attr = " + attr.GetType().Name);
                    }
                    else
                    {
                        if (drawer.GetType().IsSubclassOf(typeof(DecoratorDrawer)))
                        {
                            decoratorDrawers.Add(drawer as DecoratorDrawer);
                        }
                        else if (drawer.GetType().IsSubclassOf(typeof(LayoutDrawer)))
                        {
                            layoutDrawers.Add(drawer as LayoutDrawer);
                        }
                        else if (drawer.GetType().IsSubclassOf(typeof(ListenerDrawer)))
                        {
                            listenerDrawers.Add(drawer as ListenerDrawer);
                        }
                        else if (drawer.GetType().IsSubclassOf(typeof(PropertyControlDrawer)))
                        {
                            controlDrawers.Add(drawer as PropertyControlDrawer);
                        }
                        else if (drawer.GetType().IsSubclassOf(typeof(VisibleDrawer)))
                        {
                            visibleDrawers.Add(drawer as VisibleDrawer);
                        }
                        else if (drawer.GetType().IsSubclassOf(typeof(VerificationDrawer)))
                        {
                            verificationDrawers.Add(drawer as VerificationDrawer);
                        }
                        else if (drawer.GetType().IsSubclassOf(typeof(PropertyLabelDrawer)))
                        {
                            if (labelDrawer != null)
                            {
                                Debug.LogWarning("DrawerProperty::Init->labelDrawer has been found.attr = " + attr.GetType().Name);
                            }
                            else
                            {
                                labelDrawer = drawer as PropertyLabelDrawer;
                            }
                        }
                        else if (drawer.GetType().IsSubclassOf(typeof(PropertyContentDrawer)))
                        {
                            if (contentDrawer != null)
                            {
                                Debug.LogWarning("DrawerProperty::Init->contentDrawer has been found.attr = " + attr.GetType().Name);
                            }
                            else
                            {
                                contentDrawer = drawer as PropertyContentDrawer;
                            }
                        }
                    }
                }
            }

            if (contentDrawer == null)
            {
                contentDrawer = DrawerUtility.CreateCustomTypeDrawer(this);
                if (contentDrawer == null)
                {
                    if (DrawerUtility.IsTypeSupported(ValueType))
                    {
                        if (ValueType.IsStructOrClassType() && Value != null)
                        {
                            drawerObject = new DrawerObject(Value);
                        }
                    }
                }
            }
        }