当前位置:   article > 正文

【Unity笔记】UI Image如何自定义事件来做一个点击图片放大缩小的功能_u-image 点击放大

u-image 点击放大

其实是canvas内部有一个Ray Cast 的射线投射,也就是说,canvas负责发射射线,Image负责接收射线

在canvas的inspector上有一个Graphic Raycaster 组件,负责发射,Image的inspector 的image组件上的Raycast Target可以设置是否接收射线



  1. 第一步,添加一个无响应事件的控件,比如: Image

  2. 给上面的控件添加一个脚本

  3. 先确认你要响应的事件

接口事件说明
IPointerEnterHandlerOnPointerEnterCalled when a pointer enters the object
IPointerExitHandlerOnPointerExitCalled when a pointer exits the object
IPointerDownHandlerOnPointerDownCalled when a pointer is pressed on the object
IPointerUpHandlerOnPointerUpCalled when a pointer is released (called on the GameObject that the pointer is clicking)
IPointerClickHandlerOnPointerClickCalled when a pointer is pressed and released on the same object
IInitializePotentialDragHandlerOnInitializePotentialDragCalled when a drag target is found, can be used to initialise values
IBeginDragHandlerOnBeginDragCalled on the drag object when dragging is about to begin
IDragHandlerOnDragCalled on the drag object when a drag is happening
IEndDragHandlerOnEndDragCalled on the drag object when a drag finishes
IDropHandlerOnDropCalled on the object where a drag finishes
IUpdateSelectedHandlerOnUpdateSelectedCalled on the selected object each tick
ISelectHandlerOnSelectCalled when the object becomes the selected object
IDeselectHandlerOnDeselectCalled on the selected object becomes deselected
IMoveHandlerOnMoveCalled when a move event occurs (left, right, up, down, ect)
ISubmitHandlerOnSubmitCalled when the submit button is pressed
ICancelHandlerOnCancelCalled when the cancel button is pressed
  1. 比如我们要响应一个点击事件

第一步,实现 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("鼠标弹起事件");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  1. END

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/107916
推荐阅读
相关标签
  

闽ICP备14008679号