public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            var drawType = ComponentDataTypeAttribute.Type.WithData;

            if (this.GetAttr() is ComponentDataTypeAttribute attr)
            {
                drawType = attr.type;
            }

            var h    = 0f;
            var with = property.FindPropertyRelative("component");

            FilterDataTypesEditor.GetTypeFromManagedReferenceFullTypeName(with.managedReferenceFullTypename, out var type);
            if (type == null)
            {
                h += ComponentDataEditor.lineHeight;
            }
            else
            {
                h += ComponentDataEditor.lineHeight;
                if (drawType == ComponentDataTypeAttribute.Type.WithData)
                {
                    var initDepth = with.depth;
                    if (with.NextVisible(with.hasChildren) == true)
                    {
                        do
                        {
                            if (with.depth < initDepth)
                            {
                                break;
                            }
                            h += EditorGUI.GetPropertyHeight(with, true);
                        } while (with.NextVisible(false) == true);
                    }
                }
                else if (drawType == ComponentDataTypeAttribute.Type.NoData)
                {
                }
            }

            return(h + editButtonHeight);
        }
        public override void OnGUI(UnityEngine.Rect position, SerializedProperty property, UnityEngine.GUIContent label)
        {
            ComponentDataEditor.lastDraw.Clear();

            var drawType = ComponentDataTypeAttribute.Type.WithData;

            if (this.GetAttr() is ComponentDataTypeAttribute attr)
            {
                drawType = attr.type;
            }

            var name           = "component";
            var usedComponents = new System.Collections.Generic.HashSet <System.Type>();
            {
                var backStyle = new GUIStyle(EditorStyles.label);
                backStyle.normal.background = Texture2D.whiteTexture;

                var with = property.FindPropertyRelative(name);
                FilterDataTypesEditor.GetTypeFromManagedReferenceFullTypeName(with.managedReferenceFullTypename, out var type);

                position.height = ComponentDataEditor.lineHeight;
                if (type == null)
                {
                    //Debug.Log("Not found: " + with.managedReferenceFullTypename + ", " + with.managedReferenceFieldTypename);
                    EditorGUI.LabelField(position, "Component is not defined");

                    position.y += ComponentDataEditor.lineHeight;
                }
                else
                {
                    usedComponents.Add(type);

                    EditorGUI.LabelField(position, type.Name, EditorStyles.boldLabel);
                    position.y += ComponentDataEditor.lineHeight;

                    if (drawType == ComponentDataTypeAttribute.Type.WithData)
                    {
                        using (new GUILayoutExt.GUIBackgroundAlphaUsing(0.5f)) {
                            var isDirty = false;
                            {
                                //var componentName = GUILayoutExt.GetStringCamelCaseSpace(type.Name);
                                var initDepth = with.depth;
                                if (with.NextVisible(with.hasChildren) == true)
                                {
                                    do
                                    {
                                        if (with.depth < initDepth)
                                        {
                                            break;
                                        }

                                        ComponentDataEditor.lastDraw.Add(new ComponentDataProperty()
                                        {
                                            position = position,
                                            property = with.propertyPath,
                                            so       = with.serializedObject,
                                        });

                                        EditorGUI.BeginChangeCheck();
                                        EditorGUI.PropertyField(position, with, true);
                                        position.y += EditorGUI.GetPropertyHeight(with, true);
                                        if (EditorGUI.EndChangeCheck() == true)
                                        {
                                            isDirty = true;
                                        }
                                    } while (with.NextVisible(false) == true);
                                }
                            }

                            if (isDirty == true)
                            {
                                var obj = property.serializedObject;
                                if (obj.targetObject is IValidateEditor validateEditor)
                                {
                                    obj.ApplyModifiedProperties();
                                    obj.Update();
                                    validateEditor.OnValidateEditor();
                                    EditorUtility.SetDirty(obj.targetObject);
                                    obj.ApplyModifiedProperties();
                                    obj.Update();
                                }
                            }
                        }
                    }
                }
            }
            {
                var obj = property.serializedObject;
                GUILayoutExt.DrawAddComponentMenu(position, usedComponents, (addType, isUsed) => {
                    obj.Update();
                    var prop = obj.FindProperty(property.propertyPath);
                    var with = prop.FindPropertyRelative(name);
                    if (isUsed == true)
                    {
                        usedComponents.Remove(addType);
                        with.managedReferenceValue = null;
                    }
                    else
                    {
                        usedComponents.Add(addType);
                        with.managedReferenceValue = (IStructComponentBase)System.Activator.CreateInstance(addType);
                    }

                    if (obj.targetObject is IValidateEditor validateEditor)
                    {
                        obj.ApplyModifiedProperties();
                        obj.Update();
                        validateEditor.OnValidateEditor();
                        EditorUtility.SetDirty(obj.targetObject);
                        obj.ApplyModifiedProperties();
                        obj.Update();
                    }
                    obj.ApplyModifiedProperties();
                });
            }
        }
Example #3
0
        private Rect DrawArray(UnityEngine.Rect position, SerializedProperty property, string name)
        {
            var list           = new System.Collections.Generic.List <System.Type>();
            var usedComponents = new System.Collections.Generic.HashSet <System.Type>();

            {
                var backStyle = new GUIStyle(EditorStyles.label);
                backStyle.normal.background = Texture2D.whiteTexture;

                var with = property.FindPropertyRelative(name);
                for (int i = 0; i < with.arraySize; ++i)
                {
                    var registry = with.GetArrayElementAtIndex(i);
                    FilterDataTypesEditor.GetTypeFromManagedReferenceFullTypeName(registry.managedReferenceFullTypename, out var type);

                    if (type == null)
                    {
                        Debug.Log("Not found: " + registry.managedReferenceFullTypename + ", " + registry.managedReferenceFieldTypename);
                        continue;
                    }

                    list.Add(type);
                    usedComponents.Add(type);

                    using (new GUILayoutExt.GUIBackgroundAlphaUsing(i % 2 == 0 ? 0f : 0.05f)) {
                        {
                            var componentName = GUILayoutExt.GetStringCamelCaseSpace(type.Name);

                            EditorGUI.BeginDisabledGroup(true);
                            EditorGUI.Toggle(position, componentName, true, EditorStyles.toggle);
                            EditorGUI.EndDisabledGroup();
                        }
                    }

                    position.y += lineHeight;
                }
            }
            {
                var obj = property.serializedObject;
                GUILayoutExt.DrawAddComponentMenu(position, usedComponents, (addType, isUsed) => {
                    obj.Update();
                    var prop = obj.FindProperty(property.propertyPath);
                    var with = prop.FindPropertyRelative(name);
                    if (isUsed == true)
                    {
                        usedComponents.Remove(addType);
                        with.DeleteArrayElementAtIndex(list.IndexOf(addType));
                        list.Remove(addType);
                    }
                    else
                    {
                        usedComponents.Add(addType);
                        list.Add(addType);
                        ++with.arraySize;
                        var item = with.GetArrayElementAtIndex(with.arraySize - 1);
                        item.managedReferenceValue = (IComponentBase)System.Activator.CreateInstance(addType);
                    }
                    obj.ApplyModifiedProperties();
                }, showRuntime: true);
            }

            position.y += FilterDataTypesEditor.editButtonHeight;
            return(position);
        }