public HotKeyHandlerData(HotkeyAttribute attri, Delegate delegateInfo) { hotKey = attri.hotKey; eModifiers = attri.eModifiers; eType = attri.eType; priority = attri.priority; eSpace = attri.eSpace; actionDel = delegateInfo; }
internal static bool AssureValidity(MethodInfo method, HotkeyAttribute attr) { if (!method.IsGenericMethod && !method.IsGenericMethodDefinition && (method.ReturnType == null || method.ReturnType == typeof(void))) // Check if the method has the correct signature { ParameterInfo[] methodParams = method.GetParameters(); if (methodParams.Length == 1 && methodParams[0].ParameterType.IsAssignableFrom(typeof(EditorStates))) { return(true); } else { Debug.LogWarning("Method " + method.Name + " has incorrect signature for HotkeyAttribute!"); } } return(false); }
public static void SetupInputHandlers() { eventHandlers = new List <EventHandlerData> (); hotKeyHandlers = new List <HotKeyHandlerData> (); contextEntries = new List <ContextEntryData> (); contextFillers = new List <ContextFillerData> (); foreach (Assembly assem in AppDomain.CurrentDomain.GetAssemblies().Where(a => a.FullName.Contains("Assembly"))) { foreach (Type type in assem.GetTypes()) { foreach (MethodInfo method in type.GetMethods(BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)) { foreach (object attri in method.GetCustomAttributes(true)) { if (attri is EventHandlerAttribute && EventHandlerAttribute.AssureValidity(method, attri as EventHandlerAttribute)) { eventHandlers.Add(new EventHandlerData(attri as EventHandlerAttribute, Delegate.CreateDelegate(typeof(Action <EditorStates>), method))); } else if (attri is HotkeyAttribute && HotkeyAttribute.AssureValidity(method, attri as HotkeyAttribute)) { hotKeyHandlers.Add(new HotKeyHandlerData(attri as HotkeyAttribute, Delegate.CreateDelegate(typeof(Action <EditorStates>), method))); } else if (attri is ContextEntryAttribute && ContextEntryAttribute.AssureValidity(method, attri as ContextEntryAttribute)) { contextEntries.Add(new ContextEntryData(attri as ContextEntryAttribute, (object callBackObj) => { if (!(callBackObj is EditorStates)) { throw new UnityException(); } Delegate.CreateDelegate(typeof(Action <EditorStates>), method).DynamicInvoke(callBackObj as EditorStates); })); } else if (attri is ContextFillerAttribute && ContextFillerAttribute.AssureValidity(method, attri as ContextFillerAttribute)) { contextFillers.Add(new ContextFillerData(attri as ContextFillerAttribute, Delegate.CreateDelegate(typeof(Action <EditorStates, GenericMenu>), method))); } } } } } eventHandlers.Sort((handlerA, handlerB) => handlerA.priority.CompareTo(handlerB.priority)); hotKeyHandlers.Sort((handlerA, handlerB) => handlerA.priority.CompareTo(handlerB.priority)); }