목록

EventTrigger

UGUI의 버튼기능에서 EventTrigger 기능만 가진 컴포넌트를 만들어 다른 일반 object에도 붙이면 작업 할 때 여러모로 편할거 같아서

object컴포넌트형식의 Event에 따라 반응하도록 모듈화 script 기록


—주의사항

  • 아직 작성단계..

  • InputManager.cs와 커플링

    • gameFlow.Touch부분처럼 InputManager.cs전역적으로 쓰이는 싱글톤 클래스

<ReactEvent.cs>

namespace FrameWork
{
#if UNITY_EDITOR
    [CustomEditor(typeof(ReactEvent))]
    public class ReactEventOverrideEditor : Editor
    {
        ReactEvent reactEvent { get { return target as ReactEvent; } }

        public override void OnInspectorGUI()
        {
            EditorGUILayout.HelpBox("Add and use required event functions", MessageType.Info);

            // BeginChangeCheck와 EndChangeCheck로 둘러싸인 GUI에 어떤 변경을 실행했을때, EndChangeCheck가 true를 반환.
            EditorGUI.BeginChangeCheck();

            // update : 내부 캐쉬에서 최신 데이터를 얻습니다. 항상 최신 정보를 다루기 위해서 SerializedObject에 접근하기 전에 호출해주세요.
            serializedObject.Update();

            ////////////////////////////////////////////////////////////////////

            EditorGUILayout.PropertyField(serializedObject.FindProperty("m_enumEvent"), new GUIContent("Add New Event Type"));

            if ((reactEvent.enumEvent & ReactEvent.EventFunc.Click) == ReactEvent.EventFunc.Click)
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("m_eventClick"));
            }

            if ((reactEvent.enumEvent & ReactEvent.EventFunc.Drag) == ReactEvent.EventFunc.Drag)
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("m_eventDrag"));
            }

            if ((reactEvent.enumEvent & ReactEvent.EventFunc.DoubleClick) == ReactEvent.EventFunc.DoubleClick)
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("m_eventDoubleClick"));
            }

            ////////////////////////////////////////////////////////////////////

            // Update로 항상 최신 정보를 받으면, ApplyModifiedProperties로 변경점을 적용합니다.
            serializedObject.ApplyModifiedProperties();

            if (EditorGUI.EndChangeCheck())
            {
                SceneView.RepaintAll();
            }
        }
    }
#endif

    public class ReactEvent : MonoBehaviour
    {
        public enum EventFunc
        {
            Click = (1 << 0),
            Drag = (1 << 1),
            DoubleClick = (1 << 2),
        }

        [EnumFlag][SerializeField] private EventFunc m_enumEvent;
        [SerializeField] private UnityEngine.Events.UnityEvent m_eventClick;
        [SerializeField] private UnityEngine.Events.UnityEvent m_eventDrag;
        [SerializeField] private UnityEngine.Events.UnityEvent m_eventDoubleClick;

        public EventFunc enumEvent { get => m_enumEvent; }
        public UnityEvent eventClick { get => m_eventClick; }
        public UnityEvent eventDrag { get => m_eventDrag; }
        public UnityEvent eventDoubleClick { get => m_eventDoubleClick; }

        private void OnEnable()
        {
            gameFlow.Touch.onOneFingerTouchBegan += OnTouchBegan;
        }

        private void OnDisable()
        {
            gameFlow.Touch.onOneFingerTouchBegan -= OnTouchBegan;
        }

        public void OnTouchBegan(Touch _touchInfo)
        {
            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(_touchInfo.position), Vector2.zero);

            if(hit.collider.gameObject.GetInstanceID() == gameObject.GetInstanceID())
            {
                if (m_eventClick != null)
                    m_eventClick.Invoke();
            }
        }
    }
}