public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var objectWhoUseProperty = property.GetObjectWhoUseTheProperty();
            var eventNameAttribute   = (EventNameAttribute)fieldInfo.GetCustomAttribute(typeof(EventNameAttribute), false);
            var eventName            = string.Empty;

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

            eventName = eventNameAttribute.EventName;
            methodsTemplateDataProperty = property.FindPropertyRelative("MethodsTemplateData");
            propertyObjectInstance      = property.GetObjectValueFromSerializedProperty <EventToMethodSubscribeСontainer>();

            if (ObjectDoesntHaveEvent(objectWhoUseProperty, eventName))
            {
                Debug.LogError($"{eventName} 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, $"''{eventName}'' 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(methodsTemplateDataProperty, eventName);
            }

            EditorGUI.BeginProperty(position, label, property);


            var indent = EditorGUI.indentLevel;

            EditorGUI.indentLevel = 0;

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

            EditorGUI.indentLevel = indent;
            EditorGUI.EndProperty();
        }
Example #2
0
        private static void SubscribeEvent(object eventObject, EventToMethodSubscribeСontainer eventToMethodSubscriberField, EventNameAttribute eventNameAttribute)
        {
            if (eventNameAttribute == null)
            {
                Debug.LogError("Event Name Attribute is not found");
                return;
            }

            var eventInfo = eventObject.GetType().GetEvent(eventNameAttribute.EventName);

            if (eventInfo == null)
            {
                Debug.LogError($"ERROR! Event {eventNameAttribute.EventName} doesn't exsist on a object");
                return;
            }

            foreach (var item in eventToMethodSubscriberField.MethodsTemplateData)
            {
                if (item.eventObject == null)
                {
                    continue;
                }

                if (item.IsGlobalEvent)
                {
                    Action <EventParameter> globalEventAction = null;

                    globalEventAction += eventParameter => GlobalEventsRouter.RaiseGlobalEvent(item.GlobalEventName, eventParameter);
                    eventInfo.AddEventHandler(eventObject, globalEventAction);
                }
                else
                {
                    if (item.MonobehaviourReference == null)
                    {
                        Debug.LogError($"Monobehaviour reference is null. Can't subscribe it");
                        continue;
                    }


                    var newEventDelegate = (Action <EventParameter>)Delegate.CreateDelegate(
                        type: typeof(Action <EventParameter>),
                        target: item.MonobehaviourReference,
                        method: item.MonobehaviourMethodName);

                    eventInfo.RemoveEventHandler(eventObject, newEventDelegate);
                    eventInfo.AddEventHandler(eventObject, newEventDelegate);
                }
            }
        }