예제 #1
0
        protected void ShowConstructorSelector(SerializedProperty property)
        {
            // base type constraint
            Type baseType = SerializableClassConstructor.GetTypeFromName(
                property.FindPropertyRelative("baseTypeString").stringValue);

            var subclassTypes = ReflectionUtilities.GetAllSubTypes(baseType);

            List <MenuItem> menuItems = new List <MenuItem>();

            for (int i = 0; i < subclassTypes.Count; i++)
            {
                Type t = subclassTypes[i];

                ConstructorInfo[] constructors = t.GetConstructors();

                foreach (var constructor in constructors)
                {
                    Type[] parms = constructor.GetParameters().Select(x => x.ParameterType).ToArray();

                    // Skip methods with unsupported args
                    if (parms.Any(x => !SerializableArgument.IsSupported(x)))
                    {
                        continue;
                    }

                    string constructorPrettyName = GetConstructorShowString(constructor);
                    menuItems.Add(new MenuItem(t.Name, constructorPrettyName, () => SetConstructor(property, constructor)));
                }
            }

            // Construct and display context menu
            GenericMenu menu = new GenericMenu();

            for (int i = 0; i < menuItems.Count; i++)
            {
                menu.AddItem(menuItems[i].label, false, menuItems[i].action);
            }
            if (menu.GetItemCount() == 0)
            {
                menu.AddDisabledItem(new GUIContent("No supported constructor."));
            }
            menu.ShowAsContext();
        }
예제 #2
0
        protected void ShowSerializedArg(Rect position, SerializedProperty argProperty, string argName)
        {
            var valueProp = SerializableArgument.GetMatchProperty(argProperty);

            EditorGUI.PropertyField(position, valueProp, new GUIContent(argName));
        }
예제 #3
0
        private void SetConstructor(SerializedProperty property, ConstructorInfo constructorInfo)
        {
            SerializedProperty typeNameProp = property.FindPropertyRelative("typeName");

            typeNameProp.stringValue = SerializableClassConstructor.GetClassSerializedName(constructorInfo.DeclaringType);

            SerializedProperty argProp = property.FindPropertyRelative("serializedArguments");

            ParameterInfo[] parameters = constructorInfo.GetParameters();
            argProp.arraySize = parameters.Length;
            for (int i = 0; i < parameters.Length; i++)
            {
                argProp.GetArrayElementAtIndex(i).FindPropertyRelative("argType").enumValueIndex = (int)SerializableArgument.FromRealType(parameters[i].ParameterType);
            }
            property.serializedObject.ApplyModifiedProperties();
            property.serializedObject.Update();
        }