NGUI-EventLisiener和EventTrigger

public class L14 : MonoBehaviour
{
    public UISprite A;
    public UISprite B;
    // Start is called before the first frame update
    void Start()
    {
        //控件自带事件的局限性
        //常用控件只提供了一些常用的事件监听方式

        //NGUI事件响应函数
        //只要给对象添加了碰撞器,NGUI就提供了一些利用反射调用的函数
        //OnHover(bool)-经过
        //OnPress(bool)-按下
        //OnClick-点击
        //OnDoubleClick-双击
        //OnDragStart-开始拖曳
        //OnDrag(Vector2)-拖曳中
        //OnDragEnd-拖曳结束
        //OnDragOver(GameObject)-GameObject拖曳时经过某物
        //OnDragOut(GameObject)-GameObject拖曳时离开了某物(这个GameObject传入的是拖曳物体本身,上同)

        //更加方便的UIEventListener和UIEventTrigger
        //封装了所有特殊响应函数,可以通关它们管理和添加
        //1.UIEventListener 适合代码添加
        UIEventListener listener=UIEventListener.Get(A.gameObject);
        listener.onPress += (obj, isPressed) =>
        {
            print("IsPressed");
        };
        listener.onHover += OnHover;

        //2.UIEventTrigger 适合Inspector面板关联脚本添加
        //通过拖曳脚本在Inspector关联
        //需要自行处理逻辑和判断
    }
    void OnHover(GameObject obj, bool hover)
    {
        if (hover)
        {
            print("经过");
        }
    }
}
posted @ 2025-03-24 10:01  cannedmint  阅读(17)  评论(0)    收藏  举报