Пример #1
0
        static string ModeParamTypeName(LunarPersistentListenerMode mode)
        {
            switch (mode)
            {
            case LunarPersistentListenerMode.Void:
                return("");

            case LunarPersistentListenerMode.Bool:
                return("bool");

            case LunarPersistentListenerMode.Float:
                return("float");

            case LunarPersistentListenerMode.Int:
                return("int");

            case LunarPersistentListenerMode.String:
                return("string");

            case LunarPersistentListenerMode.Object:
                return("UnityEngine.Object");
            }

            return("???");
        }
        void UpdateParamProperty(SerializedProperty serializedProperty, Type paramType)
        {
            SerializedProperty modeProperty         = serializedProperty.FindPropertyRelative(kPropMode);
            SerializedProperty argumentsProperty    = serializedProperty.FindPropertyRelative(kPropArguments);
            SerializedProperty typeAssemblyProperty = argumentsProperty.FindPropertyRelative(kPropObjectArgumentAssemblyTypeName);

            LunarPersistentListenerMode mode = LunarPersistentListenerMode.Void;

            if (paramType != null)
            {
                if (paramType.IsSubclassOf(typeof(UnityEngine.Object)))
                {
                    mode = LunarPersistentListenerMode.Object;
                }
                else if (paramType == typeof(int))
                {
                    mode = LunarPersistentListenerMode.Int;
                }
                else if (paramType == typeof(float))
                {
                    mode = LunarPersistentListenerMode.Float;
                }
                else if (paramType == typeof(string))
                {
                    mode = LunarPersistentListenerMode.String;
                }
                else if (paramType == typeof(bool))
                {
                    mode = LunarPersistentListenerMode.Bool;
                }
                else
                {
                    Log.e("Unexpected param type: {0}", paramType);
                }
            }
            modeProperty.enumValueIndex      = (int)mode;
            typeAssemblyProperty.stringValue = paramType != null ? paramType.AssemblyQualifiedName : null;
        }
        void DrawListElement(Rect rect, int index, bool isActive, bool isFocused)
        {
            SerializedProperty arrayElementAtIndex = list.serializedProperty.GetArrayElementAtIndex(index);

            rect.y += 1f;
            Rect[]             rowRects          = GetRowRects(rect);
            Rect               runtimeModeRect   = rowRects[0];
            Rect               targetRect        = rowRects[1];
            Rect               methodRect        = rowRects[2];
            Rect               argumentRect      = rowRects[3];
            SerializedProperty modeProperty      = arrayElementAtIndex.FindPropertyRelative(kPropMode);
            SerializedProperty targetProperty    = arrayElementAtIndex.FindPropertyRelative(kPropTarget);
            SerializedProperty methodProperty    = arrayElementAtIndex.FindPropertyRelative(kPropMethod);
            SerializedProperty argumentsProperty = arrayElementAtIndex.FindPropertyRelative(kPropArguments);

            Color backgroundColor = GUI.backgroundColor;

            GUI.backgroundColor = Color.white;

            var oldFlag = GUI.enabled;

            GUI.enabled = false;
            GUI.Box(runtimeModeRect, "Runtime Only", EditorStyles.popup);
            GUI.enabled = oldFlag;

            EditorGUI.BeginChangeCheck();
            GUI.Box(targetRect, GUIContent.none);
            EditorGUI.PropertyField(targetRect, targetProperty, GUIContent.none);
            if (EditorGUI.EndChangeCheck())
            {
                methodProperty.stringValue = null;
            }
            LunarPersistentListenerMode persistentListenerMode = (LunarPersistentListenerMode)modeProperty.enumValueIndex;

            if (targetProperty.objectReferenceValue == null || string.IsNullOrEmpty(methodProperty.stringValue))
            {
                persistentListenerMode = LunarPersistentListenerMode.Void;
            }
            SerializedProperty argumentProperty;

            switch (persistentListenerMode)
            {
            case LunarPersistentListenerMode.Object:
                argumentProperty = argumentsProperty.FindPropertyRelative("m_objectArgument");
                break;

            case LunarPersistentListenerMode.Int:
                argumentProperty = argumentsProperty.FindPropertyRelative("m_intArgument");
                break;

            case LunarPersistentListenerMode.Float:
                argumentProperty = argumentsProperty.FindPropertyRelative("m_floatArgument");
                break;

            case LunarPersistentListenerMode.String:
                argumentProperty = argumentsProperty.FindPropertyRelative("m_stringArgument");
                break;

            case LunarPersistentListenerMode.Bool:
                argumentProperty = argumentsProperty.FindPropertyRelative("m_boolArgument");
                break;

            default:
                argumentProperty = argumentsProperty.FindPropertyRelative("m_intArgument");
                break;
            }
            string argumentAssemblyTypeName = argumentsProperty.FindPropertyRelative(kPropObjectArgumentAssemblyTypeName).stringValue;
            Type   argumentType             = typeof(UnityEngine.Object);

            if (!string.IsNullOrEmpty(argumentAssemblyTypeName))
            {
                argumentType = (Type.GetType(argumentAssemblyTypeName, false) ?? typeof(UnityEngine.Object));
            }
            if (persistentListenerMode == LunarPersistentListenerMode.Object)
            {
                EditorGUI.BeginChangeCheck();
                UnityEngine.Object objectReferenceValue = EditorGUI.ObjectField(argumentRect, GUIContent.none, argumentProperty.objectReferenceValue, argumentType, true);
                if (EditorGUI.EndChangeCheck())
                {
                    argumentProperty.objectReferenceValue = objectReferenceValue;
                }
            }
            else if (persistentListenerMode != LunarPersistentListenerMode.Void)
            {
                EditorGUI.PropertyField(argumentRect, argumentProperty, GUIContent.none);
            }
            using (new DisabledScopeCompat(targetProperty.objectReferenceValue == null))
            {
                EditorGUI.BeginProperty(methodRect, GUIContent.none, methodProperty);
                GUIContent content;
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    if (targetProperty.objectReferenceValue == null || string.IsNullOrEmpty(methodProperty.stringValue))
                    {
                        stringBuilder.Append("No Function");
                    }
                    else if (!LunarConsoleActionCall.IsPersistantListenerValid(targetProperty.objectReferenceValue, methodProperty.stringValue, persistentListenerMode))
                    {
                        string             componentName = "UnknownComponent";
                        UnityEngine.Object target        = targetProperty.objectReferenceValue;
                        if (target != null)
                        {
                            componentName = target.GetType().Name;
                        }
                        stringBuilder.Append(string.Format("<Missing {0}.{1}>", componentName, methodProperty.stringValue));
                    }
                    else
                    {
                        stringBuilder.Append(targetProperty.objectReferenceValue.GetType().Name);
                        if (!string.IsNullOrEmpty(methodProperty.stringValue))
                        {
                            stringBuilder.Append(".");
                            if (methodProperty.stringValue.StartsWith("set_"))
                            {
                                stringBuilder.Append(methodProperty.stringValue.Substring(4));
                            }
                            else
                            {
                                stringBuilder.Append(methodProperty.stringValue);
                            }
                        }
                    }
                    content = new GUIContent(stringBuilder.ToString());
                }
                if (GUI.Button(methodRect, content, EditorStyles.popup))
                {
                    BuildPopupList(arrayElementAtIndex).DropDown(methodRect);
                }
                EditorGUI.EndProperty();
            }
            GUI.backgroundColor = backgroundColor;
        }
Пример #4
0
        public static bool IsPersistantListenerValid(UnityEngine.Object target, string methodName, LunarPersistentListenerMode mode)
        {
            if (target == null)
            {
                return(false);
            }

            List <MethodInfo> methods = ListActionMethods(target);

            foreach (var method in methods)
            {
                if (method.Name == methodName)
                {
                    var parameters = method.GetParameters();
                    if (mode == LunarPersistentListenerMode.Void)
                    {
                        if (parameters.Length == 0)
                        {
                            return(true);
                        }
                    }
                    else if (parameters.Length == 1)
                    {
                        var paramType = parameters[0].ParameterType;
                        if (mode == LunarPersistentListenerMode.Bool && paramType == typeof(bool))
                        {
                            return(true);
                        }
                        if (mode == LunarPersistentListenerMode.Float && paramType == typeof(float))
                        {
                            return(true);
                        }
                        if (mode == LunarPersistentListenerMode.Int && paramType == typeof(int))
                        {
                            return(true);
                        }
                        if (mode == LunarPersistentListenerMode.String && paramType == typeof(string))
                        {
                            return(true);
                        }
                        if (mode == LunarPersistentListenerMode.Object && paramType.IsSubclassOf(typeof(UnityEngine.Object)))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }