赞
踩
其实是canvas内部有一个Ray Cast 的射线投射,也就是说,canvas负责发射射线,Image负责接收射线
在canvas的inspector上有一个Graphic Raycaster 组件,负责发射,Image的inspector 的image组件上的Raycast Target可以设置是否接收射线
接口 | 事件 | 说明 |
---|---|---|
IPointerEnterHandler | OnPointerEnter | Called when a pointer enters the object |
IPointerExitHandler | OnPointerExit | Called when a pointer exits the object |
IPointerDownHandler | OnPointerDown | Called when a pointer is pressed on the object |
IPointerUpHandler | OnPointerUp | Called when a pointer is released (called on the GameObject that the pointer is clicking) |
IPointerClickHandler | OnPointerClick | Called when a pointer is pressed and released on the same object |
IInitializePotentialDragHandler | OnInitializePotentialDrag | Called when a drag target is found, can be used to initialise values |
IBeginDragHandler | OnBeginDrag | Called on the drag object when dragging is about to begin |
IDragHandler | OnDrag | Called on the drag object when a drag is happening |
IEndDragHandler | OnEndDrag | Called on the drag object when a drag finishes |
IDropHandler | OnDrop | Called on the object where a drag finishes |
IUpdateSelectedHandler | OnUpdateSelected | Called on the selected object each tick |
ISelectHandler | OnSelect | Called when the object becomes the selected object |
IDeselectHandler | OnDeselect | Called on the selected object becomes deselected |
IMoveHandler | OnMove | Called when a move event occurs (left, right, up, down, ect) |
ISubmitHandler | OnSubmit | Called when the submit button is pressed |
ICancelHandler | OnCancel | Called when the cancel button is pressed |
第一步,实现 IPointerDownHandler 接口
第二步,实现接口的方法
用代码实现一个点击图片放大,弹起图片缩小的效果
### k可能不会有代码提示,请引入: UnityEngine.EventSystems;
public class Test : MonoBehaviour, IPointerDownHandler,IPointerUpHandler
{
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("鼠标按下事件");
transform.localScale = new Vector3(1.1f,1.1f,1.1f);
}
public void OnPointerUp(PointerEventData eventData)
{
transform.localScale = new Vector3(1f, 1f, 1f);
Debug.Log("鼠标弹起事件");
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。