Unity中的图像的onClick事件

是否可以在Unity中为图像(canvas的一个组件)添加“onClick”function?

var obj = new GameObject(); Image NewImage = obj.AddComponent(); NewImage.sprite = Resources.Load(a + "/" + obj.name) as Sprite; obj.SetActive(true); obj.AddComponent(); 

如何为“onClick”事件添加操作?

假设ClickAction是您的脚本,您可以通过以下方式实现OnClickfunction:

 using UnityEngine.EventSystems; public class ClickAction : MonoBehaviour, IPointerClickHandler { public void OnPointerClick(PointerEventData eventData) { // OnClick code goes here ... } } 

命名空间UnityEngine.EventSystems为您提供IPointerClickHandler接口。 单击图像时, OnPointerClick的代码将运行。