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组件
这里写图片描述

展开阅读全文

没有更多推荐了,返回首页