コード例 #1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var objectWhoUseProperty = property.GetObjectWhoUseTheProperty();
            var methodNameAttribute  = (MethodNameAttribute)fieldInfo.GetCustomAttribute(typeof(MethodNameAttribute), false);
            var methodName           = string.Empty;

            if (methodNameAttribute == null)
            {
                Debug.LogError("Method name attribute is not found. Mark it");
                GUI.Box(position, GUIContent.none);
                GUI.Label(position, "Method name attribute is not found. Mark it", GetHeaderGUIStyle(Color.red));
                return;
            }

            methodName = methodNameAttribute.MethodName;
            eventsTemplateDataProperty = property.FindPropertyRelative("EventsTemplateData");
            propertyObjectInstance     = property.GetObjectValueFromSerializedProperty <MethodToEventSubscribeContainer>();

            if (ObjectDoesntHaveMethod(objectWhoUseProperty, methodName))
            {
                Debug.LogError($"{methodName} event doesn't exsist on object, Please, use an existing eventName on object if you have it");
                GUI.Box(position, GUIContent.none);
                GUI.Label(position, $"''{methodName}'' event doesn't exsist on object", GetHeaderGUIStyle(Color.red));
                return;
            }

            InitializeRects(position);

            property.isExpanded = EditorGUI.Foldout(foldoutButtonRect, property.isExpanded, GUIContent.none, true);

            if (methodsTemplateDataReorderableList == null)
            {
                methodsTemplateDataReorderableList = BuildReorderableListFromProperty(eventsTemplateDataProperty, methodName);
            }

            EditorGUI.BeginProperty(position, label, property);

            var indent = EditorGUI.indentLevel;

            EditorGUI.indentLevel = 0;

            if (property.isExpanded)
            {
                methodsTemplateDataReorderableList.DoList(reordableListRect);
            }
            else
            {
                var headerGUIContent = new GUIContent(methodName);
                GUI.Box(foldinBoxRect, GUIContent.none);
                GUI.Label(foldoutLabelRect, headerGUIContent, GetHeaderGUIStyle(headerColor));
            }

            EditorGUI.indentLevel = indent;
            EditorGUI.EndProperty();
        }
コード例 #2
0
        private static void SubscribeMethod(object eventObject, MethodToEventSubscribeContainer methodToEventSubscriberField, MethodNameAttribute methodNameAttribute)
        {
            if (methodNameAttribute == null)
            {
                Debug.LogError("Method Name Attribute is not found");
                return;
            }

            var methodInfo = eventObject.GetType().GetMethod(methodNameAttribute.MethodName);

            if (methodInfo == null)
            {
                Debug.LogError($"ERROR! Method {methodNameAttribute.MethodName} doesn't exsist on a object");
                return;
            }

            if (methodToEventSubscriberField.EventsTemplateData.Count == 0)
            {
                Debug.Log($"Nothing to subscribe in {methodNameAttribute.MethodName} method");
            }

            foreach (var item in methodToEventSubscriberField.EventsTemplateData)
            {
                if (item.eventObject == null)
                {
                    continue;
                }

                if (item.IsGlobalEvent)
                {
                    var globalAction = (Action <EventParameter>)Delegate.CreateDelegate(
                        type: typeof(Action <EventParameter>),
                        target: eventObject,
                        method: methodInfo.Name);

                    GlobalEventsRouter.StartListeningGlobalEvent(item.GlobalEventName, globalAction);
                }
                else
                {
                    var eventInfo        = item.MonobehaviourReference.GetType().GetEvent(item.MonobehaviourEventName);
                    var newEventDelegate = (Action <EventParameter>)Delegate.CreateDelegate(
                        type: eventInfo.EventHandlerType,
                        target: eventObject,
                        method: methodInfo.Name);

                    //eventInfo.RemoveEventHandler(item.MonobehaviourReference, newEventDelegate);
                    eventInfo.AddEventHandler(item.MonobehaviourReference, newEventDelegate);
                }
            }
        }