Exemplo n.º 1
0
        public string GetFieldTypeName(bool getLowestType)
        {
            var fieldType = GetFieldType(getLowestType);

            if (fieldType.IsGenericType)
            {
                if (isInArray)
                {
                    var arr = (Array)parentValue;
                    var v   = arr.GetValue(arrayIndex);

                    // Couldn't determine type, going with array's type.
                    if (v == null || getLowestType)
                    {
                        return(ReflectionDrawerUtility.GetGenericTypeNiceName(fieldType));
                    }

                    if (v.GetType().IsGenericType)
                    {
                        return(ReflectionDrawerUtility.GetGenericTypeNiceName(v.GetType()));
                    }

                    return(v.GetType().Name);
                }

                return(ReflectionDrawerUtility.GetGenericTypeNiceName(fieldType));
            }

            return(fieldType.Name);
        }
Exemplo n.º 2
0
        public void Update()
        {
            children.Clear();
            var fieldType = GetFieldType(false);

            if (typeof(UnityEngine.Object).IsAssignableFrom(fieldType) && ReflectionDrawerUtility.TryGetCustomDrawerType(fieldType, fieldInfo == null) == null)
            {
                return;
            }

            if (value == null)
            {
                // Check if we can create a new object of this type
                // TODO: Move to utility, can be reused - Utility.CanCreateNewObjectOfType
                if ((fieldType.IsClass && fieldType.IsAbstract == false) ||
                    fieldType.IsValueType)
                {
                    if (fieldType.GetConstructors().Any(o => o.GetParameters().Length == 0))
                    {
                        value = Activator.CreateInstance(fieldType);
                        NotifyValueChanged(value);
                    }
                }
            }

            var list = new List <FieldInfo>();

            ReflectionUtility.GetAllSerializableFieldsInherited(GetFieldType(false), list);

            if (value != null)
            {
                foreach (var field in list)
                {
                    if (field.FieldType == value.GetType() ||
                        field.Name.StartsWith("m_"))
                    {
//                        DevdogLogger.LogWarning("Same type : " + value.GetType());
                        continue;
                    }

                    var child = ReflectionDrawerUtility.BuildEditorHierarchy(field, value);
                    children.Add(child);
                }

                GUI.changed = true;
            }
        }
Exemplo n.º 3
0
        public void Update()
        {
            children.Clear();

            if (value == null)
            {
                value = Array.CreateInstance(fieldInfo.FieldType.GetElementType(), 0);
                NotifyValueChanged(value);
            }

            var arrValue = (Array)value;

            for (int i = 0; i < arrValue.Length; i++)
            {
                var child = ReflectionDrawerUtility.BuildEditorHierarchy(fieldInfo, arrValue, i);
                children.Add(child);
            }

            GUI.changed = true;
        }
Exemplo n.º 4
0
        protected virtual void DrawTypeSelector(Rect rect)
        {
            var t = GetFieldType(true);

            if (t.IsValueType == false)
            {
                var r = rect;
                r.width -= EditorGUIUtility.labelWidth + 15;
                r.x     += EditorGUIUtility.labelWidth;
                r.x     += 15; // Indentation

                if (required && value == null)
                {
                    GUI.color = Color.red;
                }

                GUI.Button(r, new GUIContent(value == null ? "(empty)" : GetFieldTypeName(false)), "MiniPopup");
                if (r.Contains(Event.current.mousePosition) && Event.current.type == EventType.Used)
                {
                    var implementableTypes = ReflectionDrawerUtility.GetDerivedTypesFrom(GetFieldType(true), onlyDerivedTypesAttribute != null ? onlyDerivedTypesAttribute.type : null);

                    var n = new GenericMenu();
                    for (int i = 0; i < implementableTypes.types.Length; i++)
                    {
                        int index = i;
                        n.AddItem(implementableTypes.content[i], false, (obj) =>
                        {
                            ChangeImplementation(implementableTypes.types[index]);
                            Update();
                        }, implementableTypes.types[i]);
                    }

                    n.ShowAsContext();
                    Event.current.Use();
                }

                GUI.color = Color.white;
            }
        }
Exemplo n.º 5
0
        private void DrawArrayAddButton(Rect rect)
        {
            rect.height = EditorGUIUtility.singleLineHeight;
            if (GUI.Button(rect, "Add type", "DropDownButton"))
            {
                var derivedTypeInfo = ReflectionDrawerUtility.GetDerivedTypesFrom(fieldInfo.FieldType.GetElementType(), onlyDerivedTypesAttribute != null ? onlyDerivedTypesAttribute.type : null);

                var arrFieldType = fieldInfo.FieldType.GetElementType();
                if (onlyDerivedTypesAttribute != null)
                {
                    arrFieldType = onlyDerivedTypesAttribute.type;
                }

                rect.width -= 25;
                if (rect.Contains(Event.current.mousePosition) && arrFieldType.IsAbstract == false && arrFieldType.IsInterface == false && ReflectionDrawerUtility.IsGenericTypeWithoutGenericArguments(arrFieldType) == false)
                {
                    // Clicked left side
                    AddElement(arrFieldType);
                    Update();
                }
                else
                {
                    // Clicked right side (pick implementation)
                    var n = new GenericMenu();
                    for (int i = 0; i < derivedTypeInfo.types.Length; i++)
                    {
                        n.AddItem(derivedTypeInfo.content[i], false, (obj) =>
                        {
                            AddElement((Type)obj);
                            Update();
                        }, derivedTypeInfo.types[i]);
                    }

                    n.ShowAsContext();
                    Event.current.Use();
                }
            }
        }