U3D的事件系统用于3D物体,首先需要太场景中要有EventSystem(引物体在UI中有)这个物体
,然后摄像机上需要挂载组件
///此脚本挂载在要被点击的物体上,凡是挂载此脚本的物体都可以接收点击事件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class NewBehaviourScript : MonoBehaviour, IPointerClickHandler
{
public class My_UnityEvent : UnityEvent<string, PointerEventData> { }
//声明静态的字段
public static My_UnityEvent M_UnityEvent = new My_UnityEvent();
//实现点击接口
public void OnPointerClick(PointerEventData eventData)
{
//点击时,调用事件
M_UnityEvent.Invoke(gameObject.name, eventData);
}
}
///此脚本随便挂载在一个物体上。
sing System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class NewBehaviourScript1 : MonoBehaviour {