LoginSignup
15
15

More than 5 years have passed since last update.

uGUIのボタンにスクリプトからイベントを設定する方法

Last updated at Posted at 2015-03-18

uGUIのボタンなどにスクリプトでイベントを設定したくなったので方法をメモ。

// uGUIのボタンなどにイベントを設定するスクリプト例
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections.Generic;

public class HogeScript : MonoBehaviour {
    void Start() {
        var trigger = gameObject.AddComponent<EventTrigger>();
        trigger.delegates = new List<EventTrigger.Entry>();

        // PointerEnter(マウスオーバー)時のイベントを設定してみる
        var entry = new EventTrigger.Entry();
        entry.eventID = EventTriggerType.PointerEnter; // 他のイベントを設定したい場合はここを変える
        entry.callback.AddListener( (x) => { Debug.Log("Enter!"); });
        trigger.delegates.Add(entry);
    }
}

この方法で追加するとInspectorに一部表示されませんが動作します。

15
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
15