在game屏幕拖动图片

来源:互联网 发布:sql 关联删除 编辑:程序博客网 时间:2024/05/29 08:21

在Cavas中穿件一个Image图片,绑定如下脚本:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
//拖动图片
public class DragImage : MonoBehaviour,IDragHandler {
//保存图片的RectTransform信息
private RectTransform rectTransform;

// Use this for initializationvoid Start () {    **//获取图片的RectTransform信息**    rectTransform = GetComponent<RectTransform>();}// Update is called once per framevoid Update () {}public void OnDrag(PointerEventData eventData){    //获取鼠标的位置信息    Vector3 position = Input.mousePosition;    //在拖拽中实时更新图片的额位置信息    rectTransform.position = new Vector3(position.x,position.y,rectTransform.position.z);}

}

原创粉丝点击