現在のイベントのUGUIに追加したボタンは3つのメソッドをまとめたもの。
1は、直接イベントを追加するには、プラス記号をクリックし、この方法は、最も一般的に使用されています。
二つのパラメータが存在する場合、このメソッドは、引数を持つ関数を追加、しかし、ほとんどのパラメータ数にすることができ、Unityは表示されません。
まず、このスクリプトは、キャンバスの上にマウントするために、スクリプトButtonClickFIrstを書きます。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtonClickFIrst : MonoBehaviour
{
public void a()
{
Debug.Log("ccccccc");
}
}
その後、ボタンのキャンバスとドラッグイベントをクリックしてください。
そして、クリックされたときに実行する機能を選択
図2は、コードに追加のaddListenerによってイベントを追加します
あなたは、イベントを追加することができますボタンにこのスクリプトを入れて、
もちろん、あなたものaddListenerによって、他のイベントを追加することができます。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ButtonClick : MonoBehaviour
{
// Start is called before the first frame update
void b()
{
Debug.Log("aaaaaaaaaaaa");
}
void addFun()
{
Button a = transform.GetComponent<Button>();
a.onClick.AddListener(b);
}
void Start()
{
addFun();
}
// Update is called once per frame
void Update()
{
}
}
イベントを追加するには3、インターフェイス
インタフェースの継承IPointerClickHandlerは、イベントを追加します。
このインターフェイスは、インターフェイスのクリックイベントで、インターフェースが直接継承することができ、他のイベントがたくさんあります。直接戻るには、インターフェイスを追加することができます。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ButtonClick : MonoBehaviour,IPointerClickHandler
{
// Start is called before the first frame update
void b()
{
Debug.Log("aaaaaaaaaaaa");
}
void addFun()
{
Button a = transform.GetComponent<Button>();
a.onClick.AddListener(b);
}
void Start()
{
addFun();
}
// Update is called once per frame
void Update()
{
}
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("clickCount:::"+eventData.clickCount);
}
}