Unity

Unity 이벤트 트리거 스크립트로 추가

기억해조 2022. 10. 28. 17:38

<스크립트>

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class MyBtnScript : MonoBehaviour
{
    public Image img;
    private EventTrigger eventTrigger;

    private Material btnMat;

    private Color onColor = new Color(118/255f, 5 / 255f, 0 / 255f);
    private Color defaultColor = Color.white;
    private const float emissionIntensity = 25f;


    private void Awake()
    {
        CreateBtnMat();

        img = GetComponent<Image>();
        eventTrigger = GetComponent<EventTrigger>();

        //이벤트 트리거 - PointEnter
        EventTrigger.Entry enterentry = new EventTrigger.Entry();
        enterentry.eventID = EventTriggerType.PointerEnter;
        enterentry.callback.AddListener((data) => { MouseOver(); });

        //이벤트 트리거 - PointExit
        EventTrigger.Entry exitentry = new EventTrigger.Entry();
        exitentry.eventID = EventTriggerType.PointerExit;
        exitentry.callback.AddListener((data) => { MouseExit(); });

        //저장
        eventTrigger.triggers.Add(enterentry);
        eventTrigger.triggers.Add(exitentry);

        //
    }

    //실행할 함수 
    public void MouseOver()
    {
        if (img.material != null)
        {
            img.material.SetVector("_EmissionColor", onColor  * emissionIntensity);
        }

    }

    //실행할 함수 
    public void MouseExit()
    {
        if (img.material != null)
        {
            img.material.SetVector("_EmissionColor", defaultColor * emissionIntensity);
        }
    }

    //버튼 머터리얼 생성
    public void CreateBtnMat()
    {
        Material mat = Instantiate(UIManager.Inst.btnActiveMat);
        btnMat = mat;
    }

    //머터리얼 넣고/빼고
    public void SetBtnMat(bool isActive)
    {
        switch (isActive)
        {
            case true:
                img.material = btnMat;
                break;
            case false:
                img.material = null;
                break;
        }
    }
}

 

출처(훨씬자세히 써있음)

 

https://daru-daru.tistory.com/55

 

[Unity 3D] 이벤트 트리거 (Event Trigger)

유니티에서는 이벤트 트리거라고 하는 이벤트 딜리게이트를 제공한다. 유니티에서 아무 UI를 생성하게 되면 이벤트 시스템이 생성된다.    이벤트 시스템은 Canvas 내에 있는 이미지들에 대한 Del

daru-daru.tistory.com