Ejemplo n.º 1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            int index = _stateInfoList.IndexOf(stateInfo);

            if (index < 0)
            {
                stateInfo = _stateInfoList[index = 0];
                field.SetValue(property.serializedObject.targetObject, stateInfo);
            }

            if (_stateInfoList.Count < 0)
            {
                EditorGUI.LabelField(position, new GUIContent(string.Format("There is no classes that implement {0}.", (typeof(IState)).Name)));
                return;
            }

            Rect rect = position;

            rect.height = EditorGUIUtility.singleLineHeight;
            GUI.Label(rect, new GUIContent(property.displayName));
            rect.y += EditorGUIUtility.singleLineHeight;
            index   = EditorGUI.Popup(rect, index, constructorNames.ToArray());
            for (int i = 0; i < stateInfo.Parameters.Length; i++)
            {
                rect.y += EditorGUIUtility.singleLineHeight;
                Type       parametersType = stateInfo.Parameters[i].Type;
                GUIContent parameterLabel = new GUIContent(stateInfo.Parameters[i].ParameterName);
                switch (parametersType.Name)
                {
                case "Int32":
                    stateInfo.Parameters[i].IntValue = EditorGUI.IntField(rect, parameterLabel, stateInfo.Parameters[i].IntValue);
                    break;

                case "Single":
                    stateInfo.Parameters[i].FloatValue = EditorGUI.FloatField(rect, parameterLabel, stateInfo.Parameters[i].FloatValue);
                    break;

                case "Boolean":
                    stateInfo.Parameters[i].BoolValue = EditorGUI.Toggle(rect, parameterLabel, stateInfo.Parameters[i].BoolValue);
                    break;

                case "String":
                    stateInfo.Parameters[i].StringValue = EditorGUI.TextField(rect, parameterLabel, stateInfo.Parameters[i].StringValue);
                    break;

                default:
                    stateInfo.Parameters[i].ObjectValue = EditorGUI.ObjectField(rect, parameterLabel, stateInfo.Parameters[i].ObjectValue, parametersType, true);
                    break;
                }
            }

            if (stateInfo != _stateInfoList[index])
            {
                stateInfo = _stateInfoList[index];
                field.SetValue(property.serializedObject.targetObject, stateInfo);
            }

            property.serializedObject.ApplyModifiedProperties();
            property.serializedObject.UpdateIfRequiredOrScript();
        }
Ejemplo n.º 2
0
        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            if (stateInfo == null)
            {
                field     = property.serializedObject.targetObject.GetType().GetField(property.propertyPath, BindingFlags.NonPublic | BindingFlags.Instance);
                stateInfo = field.GetValue(property.serializedObject.targetObject) as StateConstructor;
            }

            if (constructorNames.Count == 0)
            {
                foreach (var type in GetTypes())
                {
                    foreach (var constructor in type.GetConstructors())
                    {
                        _stateInfoList.Add(new StateConstructor(constructor));
                        constructorNames.Add(_stateInfoList[_stateInfoList.Count - 1].Name);
                    }
                }

                if (string.IsNullOrEmpty(stateInfo.Type.FullName) && string.IsNullOrEmpty(stateInfo.Type.AssemblFullName))
                {
                    stateInfo = _stateInfoList[0];
                }
            }

            return(EditorGUIUtility.singleLineHeight * (stateInfo.Parameters.Length + 2));
        }