如何使用脚本向 EventTrigger 添加函数

问题描述 投票:0回答:2

如何仅使用脚本添加事件触发器(例如,

PointerDown
)?具体来说,我有一个由脚本生成的对象。我已经添加了
EventTrigger
组件,但我不确定如何添加
PointerDown
事件。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class spawn : MonoBehaviour {

    //
    int buatrandom;
    int jumlahrandom = 16 ;

    int objek1 ;
    int objek2 ;
    int objek3 ;

    int checkobjek;
    int checkobjekawal;

    string simpan1;
    string simpan2;
    string simpan3;

    public GameObject target1;
    public GameObject target2;
    public GameObject target3;

    public int [] simpannomorobject ;
    public GameObject[] nomorasset;

    public float speed =10f;
    public float berat = 1.0f;

    // Use this for initialization
    void Start () {
        simpannomorobject = new int[3]; 
        for (int i = 0; i < 2; i++) {
            buatrandom = Random.Range (0, jumlahrandom);
            simpannomorobject [i] = buatrandom;
            if (i > 0){
                if (i < 3) {
                    buatobjek ();
                }
            }
        }
    }

    void buatobjek (){
        objek1 = simpannomorobject [0];
        objek2 = simpannomorobject [1];
        objek3 = simpannomorobject [2];

        simpan1 = objek1.ToString();
        simpan2 = objek2.ToString();
        simpan3 = objek3.ToString();

        target1 = Instantiate (nomorasset [objek1], new Vector3 (0, 0, 3.0f), Quaternion.identity);
        target2 = Instantiate (nomorasset [objek2], new Vector3 (4.0f, 0, 0), Quaternion.Euler(0,90,0));
        target3 = Instantiate (nomorasset [objek3], new Vector3 (-4.0f, 0, 0), Quaternion.Euler(0,-90,0));

        target1.AddComponent<BoxCollider> ();
        target2.AddComponent<BoxCollider> ();
        target3.AddComponent<BoxCollider> ();

        target1.AddComponent<EventTrigger> ();
        target2.AddComponent<EventTrigger> ();
        target3.AddComponent<EventTrigger> ();

        target1.tag = simpan1;
        target2.tag = simpan2;
        target3.tag = simpan3;

            
    }

    // Update is called once per frame
    void Update () {
        target1.transform.Rotate(0,100* Time.deltaTime,0);
        target2.transform.Rotate(0,100* Time.deltaTime,0);
        target3.transform.Rotate(0,100* Time.deltaTime,0);
    }



}
c# unity-game-engine google-cardboard google-vr
2个回答
2
投票
void buatobjek (){
    // ....
    EventTrigger eventTrigger1 = target1.AddComponent<EventTrigger> ();
    EventTrigger.Entry entry = new EventTrigger.Entry( );
    entry.eventID = EventTriggerType.PointerDown;
    entry.callback.AddListener( ( data ) => { OnPointerDownDelegate( (PointerEventData)data ); } );
    eventTrigger1.triggers.Add( entry );
}

public void OnPointerDownDelegate( PointerEventData data )
{
    Debug.Log( "OnPointerDownDelegate called." );
}

0
投票
EventTrigger eventTrigger = gameObject.GetComponent<EventTrigger>();
EventTrigger.Entry entry = new EventTrigger.Entry();
{
    entry.eventID = EventTriggerType.Select; // тип ивента
}
entry.callback.AddListener(data => deliteCounter.Metode(data, parametr)); // если нужно передать параметр
eventTrigger.triggers.Add(entry);
© www.soinside.com 2019 - 2024. All rights reserved.