Unity 利用 UnityAction 对多个按钮添加监听

当有许多个按钮需要添加监听的时候,我们可以先使用UnityAction把这些方法监听起来,再分别传给按钮。



public UnityEngine.Events.UnityAction[] allEvents = new UnityEngine.Events.UnityAction[3];



void Start () {
    //一些初始化
    mAS = this.GetComponent ();

    mVideoPlayer = this.GetComponent ();

    mVideoPlayer.clip = mVClips [num];

    timeCtrl.onValueChanged.AddListener (ChangeTime);

 
    //监听方法
    allEvents [0] = () => {

        chooseVideo = -1;

    };
    //监听方法
    allEvents [1] = () => {

        videoIsPlaying = !videoIsPlaying;

    };
    //监听方法
    allEvents [2] = () => {

        chooseVideo = 1;

    };



    for (int i = 0; i < btns.Length; i++) {
        //给按钮等添加监听
        btns [i].onClick.AddListener (allEvents [i]);

    }

}


你可能感兴趣的:(Unity)