カスタムボタンを利用する
カスタムボタンの使い方
参考 URL
使い方
①ボタンのUI Imageを作成
②スクリプトを作成
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class CustomButton : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler { public System.Action onClickCallback; // タップ public void OnPointerClick(PointerEventData eventData) { onClickCallback.Invoke(); } // タップダウン public void OnPointerDown(PointerEventData eventData) { } // タップアップ public void OnPointerUp(PointerEventData eventData) { } }
onClickCallbackはActionデリゲート。このデリゲートにボタンを押した処理を
格納する。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class LeftButton : MonoBehaviour { private CustomButton _bt; private void Start() { _bt = GetComponent<CustomButton>(); //_bt.onClickCallback = () => //{ // PushButtonAction(); //}; } private void Update() { _bt.onClickCallback = () => { PushButtonActioon(); }; } public void PushButtonAction() { Debug.Log("Push"); } }
この2つのスクリプトを、ボタンにしたいImageに張り付ける。
Imageをクリックすると、 PushButtonAction()関数が呼ばれる。