Unity3D 鼠标拖动和旋转物体以及鼠标拖动图片

1.鼠标拖动物体Cube
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
///
/// 脚本挂到一个Cube上
///
public class Ts : MonoBehaviour
{

public Texture2D mouseTexture;//鼠标样式纹理图
public Vector3 mousePos;


void Start()
{

}

void OnMouseEnter()
{
    //更换鼠标的样式
    Cursor.SetCursor (this.mouseTexture, Vector2.zero, CursorMode.Auto);

}

void OnMouseExit()
{
    //恢复回鼠标的样式
    Cursor.SetCursor (null, Vector2.zero, CursorMode.Auto);

}

//通过鼠标拖动物体Cube
IEnumerator OnMouseDown()
{

    mousePos = Input.mousePosition;

    while (Input.GetMouseButton(0))
    {
        Vector3 offset = mousePos - Input.mousePosition;
        //左右旋转
        transform.Rotate(Vector3.up * offset.x, Space.World);  
        //上下旋转
        transform.Rotate(Vector3.right*offset.y,Space.World);

        mousePos=Input.mousePosition;
        yield return null;
    }
}

}

2.鼠标旋转物体
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
///
/// 脚本挂到一个Cube上
///

public class Ts : MonoBehaviour
{

public Texture2D mouseTexture;//鼠标样式纹理图
public Vector3 mousePos;


void Start()
{

}

void OnMouseEnter()
{
    //更换鼠标的样式
    Cursor.SetCursor (this.mouseTexture, Vector2.zero, CursorMode.Auto);

}

void OnMouseExit()
{
    //恢复回鼠标的样式
    Cursor.SetCursor (null, Vector2.zero, CursorMode.Auto);

}



//通过鼠标移动物体Cube
IEnumerator OnMouseDown()
{
    Vector3 screenSpace = Camera.main.WorldToScreenPoint(transform.position);//三维物体坐标转屏幕坐标
    //将鼠标屏幕坐标转为三维坐标,再计算物体位置与鼠标之间的距离
    var offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
    print("down");
    while (Input.GetMouseButton(0))
    {
        Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
        var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
        transform.position = curPosition;
        yield return new WaitForFixedUpdate();
    }
}

}

3.鼠标拖动图片
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
///
/// 脚本挂到一个Cube上
///

public class Ts : MonoBehaviour
{

public Texture2D mouseTexture;//鼠标样式纹理图
public Vector3 mousePos;


void Start()
{

}

void OnMouseEnter()
{
    //更换鼠标的样式
    Cursor.SetCursor (this.mouseTexture, Vector2.zero, CursorMode.Auto);

}

void OnMouseExit()
{
    //恢复回鼠标的样式
    Cursor.SetCursor (null, Vector2.zero, CursorMode.Auto);

}




//通过鼠标移动图片
public void OnDrag(BaseEventData baseEventData)     
{      
    PointerEventData data = baseEventData as PointerEventData;   
    var rt = data.pointerPress.GetComponent<RectTransform>();   
    Vector3 globalMousePos;  
    if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, data.position, data.pressEventCamera, out globalMousePos))     
    {      
        rt.position = globalMousePos;   
    }   
} 

}
鼠标拖动图片需要在Inspector视图里添加EventTrigger组件
这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值