三维物体的拖拽和移动:分为Drag(拖拽)和Move(移动)两个脚本
Drag
using System.Collections;
using UnityEngine;
public class Drag : MonoBehaviour
{
private Transform targetTran; // 目标物体的空间变换组件
private Vector3 targetScreenPos;// 目标物体的屏幕空间坐标
private Vector3 targetWorldPos; // 目标物件的世界空间坐标
private Vector3 mouseScreenPos; // 鼠标的屏幕空间坐标
private Vector3 vec3Offset; // 偏移
private void Awake()
{
targetTran = this.transform;
}
private IEnumerator OnMouseDown()
{
Debug.Log(this.gameObject.name);
// 把目标物件的世界坐标转换到它自身的屏幕空间坐标
targetScreenPos = Camera.main.WorldToScreenPoint(targetTran.position);
// 存储鼠标的屏幕空间坐标(z值使用目标物体的屏幕空间坐标)
mouseScreenPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, targetScreenPos.z);
// 计算目标物体与鼠标物体在世界空间中的偏移量
vec3Offset = targetTran.position - Camera.main.ScreenToWorldPoint(mouseScreenPos);
// 鼠标左键按下
while (Input.GetMouseButton(0))
{
// 存储鼠标的屏幕空间坐标(z值使用目标物体的屏幕空间坐标)
mouseScreenPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, targetScreenPos.z);
// 把鼠标的屏幕空间坐标装换到世界空间坐标(Z值使用目标物体的屏幕空间坐标),
// 加上偏移量,以此作为目标物体的时间空间坐标
targetWorldPos = Camera.main.ScreenToWorldPoint(mouseScreenPos) + vec3Offset;
// 更新目标物件的世界空间坐标
targetTran.position = targetWorldPos;
// 等待固定更新
yield return new WaitForFixedUpdate();
}
}
}
Move
using System;
using UnityEngine;
public class Move : MonoBehaviour
{
//public Transform target;
private float distence = 30f;
private float xSpeed = 150f;
private float ySpeed = 150f;
private float yMinLimit = -180f;
private float yMaxLimit = 180f;
private float x = 0f;
private float y = 0f;
private Vector2 oldPosition1;
private Vector2 oldPosition2;
private bool flag_Roable = true; //自动旋转标记
private DateTime oldTime;
private DateTime nowTime;
void Start()
{
transform.eulerAngles = new Vector3(0, -90, 0);
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
if (GetComponent<Rigidbody>())
{
GetComponent<Rigidbody>().freezeRotation = true;
}
oldTime = DateTime.Now;
}
void Update()
{
nowTime = DateTime.Now;
TimeSpan ts1 = new TimeSpan(oldTime.Ticks);
TimeSpan ts2 = new TimeSpan(nowTime.Ticks);
TimeSpan ts = ts2.Subtract(ts1).Duration();
if(ts.Seconds > 8 && !Input.anyKey)
{
flag_Roable = true;
oldTime = DateTime.Now;
}
if (Input.anyKey)
{
string msg = "InputCount:" + Input.touchCount;
DragAndMoveManager.Instance.ShowText(msg);
if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
x = Input.GetAxis("Mouse X") * xSpeed;
y = Input.GetAxis("Mouse Y") * ySpeed;
transform.Rotate(Vector3.up * -x * Time.deltaTime, Space.Self);
transform.Rotate(Vector3.right * y * Time.deltaTime, Space.Self);
}
}
if (Input.touchCount > 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
{
Vector2 tempPosition1 = Input.GetTouch(0).position;
Vector2 tempPosition2 = Input.GetTouch(1).position;
if(isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
{
float oldScale = transform.localScale.x;
float newScale = oldScale * 1.025f;
transform.localScale = new Vector3(newScale, newScale, newScale);
}
else
{
float oldScale = transform.localScale.x;
float newScale = oldScale / 1.025f;
transform.localScale = new Vector3(newScale, newScale, newScale);
}
// 备份上一次的触摸点的位置,用于对比
oldPosition1 = tempPosition1;
oldPosition2 = tempPosition2;
}
}
}
}
// 是否为放大手势
private bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
{
// 函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势
var leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
var leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
if (leng1 < leng2)
{
// 放大手势
return true;
}
else
{
// 缩小手势
return false;
}
}
}
Drag代码性能小优化:
using System.Collections;
using UnityEngine;
public class Drag : MonoBehaviour
{
private Transform targetTran;
private Vector3 targetScreenPos;// 目标物体的屏幕空间坐标
private Vector3 targetWorldPos; // 目标物体的世界空间坐标
private Vector3 mouseScreenPos; // 鼠标的屏幕空间坐标
private Vector3 vec3Offset; // 偏移
private Camera _mainCamera;
private Camera mainCamera
{
get
{
if(_mainCamera == null)
_mainCamera = Camera.main;
return _mainCamera;
}
}
private void Awake()
{
targetTran = this.transform;
}
// OnMouseDown is called when the user has pressed the mouse button while over the Collider.
// This function can be a coroutine.
private IEnumerator OnMouseDown()
{
Debug.Log(this.gameObject.name);
targetScreenPos = mainCamera.WorldToScreenPoint(targetTran.position);
Vector3 mouseInputPos = Input.mousePosition;
mouseScreenPos = new Vector3(mouseInputPos.x, mouseInputPos.y, targetScreenPos.z);
vec3Offset = targetTran.position - mainCamera.ScreenToWorldPoint(mouseScreenPos);
WaitForFixedUpdate waitForFixedUpdate = new WaitForFixedUpdate();
while (Input.GetMouseButton(0))
{
mouseInputPos = Input.mousePosition;
mouseScreenPos = new Vector3(mouseInputPos.x, mouseInputPos.y, targetScreenPos.z);
targetWorldPos = mainCamera.ScreenToWorldPoint(mouseScreenPos) + vec3Offset;
targetTran.position = targetWorldPos;
yield return waitForFixedUpdate;
}
}
}