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
是您的脚本,您可以通过以下方式实现OnClick
function:
using UnityEngine.EventSystems; public class ClickAction : MonoBehaviour, IPointerClickHandler { public void OnPointerClick(PointerEventData eventData) { // OnClick code goes here ... } }
命名空间UnityEngine.EventSystems
为您提供IPointerClickHandler
接口。 单击图像时, OnPointerClick
的代码将运行。