예제 #1
0
 internal static bool AssureValidity(MethodInfo method, HotkeyAttribute attr)
 {
     if (!method.IsGenericMethod && !method.IsGenericMethodDefinition && (method.ReturnType == null || method.ReturnType == typeof(void)))
     {
         ParameterInfo[] methodParams = method.GetParameters();
         if (methodParams.Length == 1 && methodParams[0].ParameterType.IsAssignableFrom(typeof(EditorInputInfo)))
         {
             return(true);
         }
         else
         {
             Debug.LogWarning("Method " + method.Name + " has incorrect signature for HotkeyAttribute!");
         }
     }
     return(false);
 }
예제 #2
0
    public static void SetupInput(List <Type> setType)
    {
        eventHandlers  = new List <KeyValuePair <EventHandlerAttribute, Delegate> >();
        hotkeyHandlers = new List <KeyValuePair <HotkeyAttribute, Delegate> >();
        contextEntries = new List <KeyValuePair <ContextEntryAttribute, MenuFunctionData> >();

        IEnumerable <Assembly> scriptAssemblies = AppDomain.CurrentDomain.GetAssemblies().Where((Assembly assembly) => assembly.FullName.Contains("Assembly"));

        foreach (Assembly assembly in scriptAssemblies)
        {
            foreach (Type type in assembly.GetTypes())
            {
                bool getType = false;
                foreach (var t in setType)
                {
                    if (type == t || type == typeof(EditorState))
                    {
                        getType = true;
                        break;
                    }
                }

                if (!getType)
                {
                    continue;
                }

                foreach (MethodInfo method in type.GetMethods(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static))
                {
                    Delegate actionDelegate = null;
                    foreach (object attr in method.GetCustomAttributes(true))
                    {
                        Type attrType = attr.GetType();
                        if (attrType == typeof(EventHandlerAttribute))
                        {
                            if (EventHandlerAttribute.AssureValidity(method, attr as EventHandlerAttribute))
                            {
                                if (actionDelegate == null)
                                {
                                    actionDelegate = Delegate.CreateDelegate(typeof(Action <EditorInputInfo>), method);
                                }

                                eventHandlers.Add(new KeyValuePair <EventHandlerAttribute, Delegate>(attr as EventHandlerAttribute, actionDelegate));
                            }
                        }
                        else if (attrType == typeof(HotkeyAttribute))
                        {
                            if (HotkeyAttribute.AssureValidity(method, attr as HotkeyAttribute))
                            {
                                if (actionDelegate == null)
                                {
                                    actionDelegate = Delegate.CreateDelegate(typeof(Action <EditorInputInfo>), method);
                                }
                                hotkeyHandlers.Add(new KeyValuePair <HotkeyAttribute, Delegate>(attr as HotkeyAttribute, actionDelegate));
                            }
                        }
                        else if (attrType == typeof(ContextEntryAttribute))
                        {
                            if (ContextEntryAttribute.AssureValidity(method, attr as ContextEntryAttribute))
                            {
                                if (actionDelegate == null)
                                {
                                    actionDelegate = Delegate.CreateDelegate(typeof(Action <EditorInputInfo>), method);
                                }

                                MenuFunctionData menuFunction = (object callbackObj) =>
                                {
                                    if (!(callbackObj is EditorInputInfo))
                                    {
                                        throw new UnityException("Callback Object passed by context is not of type EditorMenuCallback!");
                                    }
                                    actionDelegate.DynamicInvoke(callbackObj as EditorInputInfo);
                                };
                                contextEntries.Add(new KeyValuePair <ContextEntryAttribute, MenuFunctionData>(attr as ContextEntryAttribute, menuFunction));
                            }
                        }
                    }
                }
            }
        }

        eventHandlers.Sort((handlerA, handlerB) => handlerA.Key.priority.CompareTo(handlerB.Key.priority));
        hotkeyHandlers.Sort((handlerA, handlerB) => handlerA.Key.priority.CompareTo(handlerB.Key.priority));
    }