unity--拖拽(UI-Image Sprite Render)
Posted 格拉格拉
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity--拖拽(UI-Image Sprite Render)相关的知识,希望对你有一定的参考价值。
目录
1.UI ---Image拖拽
控制脚本需继承IBeginDragHandler, IEndDragHandler, IDragHandler 三个接口 并实现接口方法
public class ItemContr : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
private RectTransform rectTrans;
private void Awake()
rectTrans = GetComponent<RectTransform>();
private void OnTriggerEnter2D(Collider2D other)
//....
private void OnTriggerExit2D(Collider2D other)
//....
public void OnBeginDrag(PointerEventData eventData)
rectTrans.SetAsLastSibling();
public void OnDrag(PointerEventData eventData)
rectTrans.anchoredPosition += eventData.delta;
public void OnEndDrag(PointerEventData eventData)
rectTrans.SetSiblingIndex(1);
2.spriteReder拖拽
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemContr : MonoBehaviour
private Vector3 oriMousePos;
private Vector3 oriPos;
void Start()
void Update()
private void OnMouseDown()
oriMousePos = Input.mousePosition;
oriPos = transform.position;
private void OnMouseDrag()
var offset = (Camera.main.ScreenToWorldPoint(Input.mousePosition).x - Camera.main.ScreenToWorldPoint(oriMousePos).x);
transform.position = new Vector3(oriPos.x + offset, transform.position.y, transform.position.z);
private void OnMouseUp()
Debug.LogError("Up");
以上是关于unity--拖拽(UI-Image Sprite Render)的主要内容,如果未能解决你的问题,请参考以下文章