這里用IPointerClickHandler舉例(api中這樣解釋):
也就是在我們的被點(diǎn)擊的類中實(shí)現(xiàn)了接口后,在實(shí)現(xiàn)方法OnPointerClick,在這個(gè)方法里操作點(diǎn)擊后的處理。
?
?
?
到這里,需要我們開發(fā)的只有一個(gè)腳本,Demo里的EventClick.cs?
using UnityEngine;
using UnityEngine.EventSystems;
public class EventClick : MonoBehaviour,IPointerClickHandler
{
? ? public void OnPointerClick(PointerEventData eventData)
? ? {
? ? ? ? print("點(diǎn)擊了::"+this.name);
? ? }
}
?
?
?
?
?
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class ClickEventTrigger : MonoBehaviour?
{
public void OnClick()
? ? {
? ? ? ? print("MyOnClick 點(diǎn)擊了::"+this.name);
? ? }
}
?
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class ClickEventTrigger : MonoBehaviour {
// Use this for initialization
void Start () {
? ? ? ? //獲取或添加EventTrigger組件
? ? ? ? EventTrigger trigger = transform.GetComponent();
? ? ? ? if (trigger == null)
? ? ? ? {
? ? ? ? ? ? trigger = transform.gameObject.AddComponent();
? ? ? ? }
? ? ? ? //初始化EventTrigger.Entry的數(shù)組 如果這里初始化了事件觸發(fā)數(shù)組,那么在ide靜態(tài)添加的事件會(huì)丟失
? ? ? ? //trigger.triggers = new List();
? ? ? ? //創(chuàng)建各種 EventTrigger.Entry的類型
? ? ? ? EventTrigger.Entry entry = new EventTrigger.Entry();
? ? ? ? entry.eventID = EventTriggerType.PointerEnter;//設(shè)置Entry的eventID類型 即EventTriggerType的各種枚舉(比如鼠標(biāo)點(diǎn)擊,滑動(dòng),拖動(dòng)等)
? ? ? ? UnityAction callback = new UnityAction(OnPointerEnter);? //注冊(cè)代理
? ? ? ? entry.callback.AddListener(callback);//添加代理事件到EventTrigger.Entry
? ? ? ? EventTrigger.Entry entry2 = new EventTrigger.Entry();
? ? ? ? entry2.eventID = EventTriggerType.PointerDown;
? ? ? ? UnityAction callback1 = new UnityAction(OnPointerDown);
? ? ? ? entry2.callback.AddListener(callback1);
? ? ? ? //在EventTrigger.Entry的數(shù)組添加EventTrigger.Entry
? ? ? ? trigger.triggers.Add(entry);
? ? ? ? trigger.triggers.Add(entry2);
? ? }
? ? private void OnPointerDown(BaseEventData arg0)
? ? {
? ? ? ? Debug.Log("OnPointerDown");
? ? }
? ? private void OnPointerEnter(BaseEventData arg0)
? ? {
? ? ? ? Debug.Log("OnPointerEnter");
? ? }
}
?
?
?
?
文章來源:http://www.zghlxwxcb.cn/news/detail-480700.html
?
文章來源地址http://www.zghlxwxcb.cn/news/detail-480700.html
?
using UnityEngine;
public class ClickRayCastHitControl : MonoBehaviour {
? ? Ray ray;
? ? RaycastHit hit;
? ? GameObject obj;
? ? // Use this for initialization
? ? void Start () {
}
? ? // Update is called once per frame
? ??
? ? void Update()
? ? {
? ? ? ? if (Input.GetMouseButtonDown(0))
? ? ? ? {
? ? ? ? ? ? Debug.Log("點(diǎn)擊鼠標(biāo)左鍵");
? ? ? ? ? ? ray = Camera.main.ScreenPointToRay(Input.mousePosition);
? ? ? ? ? ? if (Physics.Raycast(ray, out hit))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Debug.Log(hit.collider.gameObject.name);
? ? ? ? ? ? ? ? obj = hit.collider.gameObject;
? ? ? ? ? ? ? ? //通過名字
? ? ? ? ? ? ? ? if (obj.name.Equals("BeiJiChuan"))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Debug.Log("點(diǎn)中" + obj.name);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //通過標(biāo)簽
? ? ? ? ? ? ? ? if (obj.tag == "ClicObj")
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Debug.Log("點(diǎn)中" + obj.name);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
到了這里,關(guān)于Unity3D-場(chǎng)景中3D物體添加點(diǎn)擊事件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!