此脚本挂在 Camera(摄像机)上。
定位模型用作查找模型后定位到该模型的跟前,然后拥有下面的操作功能;
类似编辑器模式下的操作,用作浏览模型使用,旋转、拖动和缩放均是操作的Camera(摄像机)位移;
高亮使用了HighlightingSystem插件;
中心点并非模型的transform.position,而是模型的正中心,因为有些模型的position不是模型正中心,所以需要此方法。
using HighlightingSystem;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test01 : MonoBehaviour {
public Transform obj;
float maxX, maxY, maxNum;
//float minx, minY, minZ;
private Vector3 centerPoint;
#region 浏览模型操控属性
/// <summary>
/// 缩放距离
/// </summary>
public float ZoomSpeed = 30;
/// <summary>
/// 拖动速度
/// </summary>
public float MovingSpeed = 0.5f;
/// <summary>
/// 视角摆动速度
/// </summary>
public float RotateSpeed = 1;
/// <summary>
/// 摄像机旋转速度
/// </summary>
public float distance = 30;
Quaternion rotation;
Vector3 position;
float delta_x, delta_y, delta_z;
float delta_rotation_x, delta_rotation_y;
#endregion
void Start () {
//计算模型正前方位置点
Mesh mesh = obj.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
//minZ = obj.position.z;
//计算得到最值顶点,判断模型的最长或最宽,用来计算定位点的离模型的距离
foreach (Vector3 item in vertices)
{
if (item.x > maxX)
{
maxX = item.x;
}
if (item.y > maxY)
{
maxY = item.y;
}
}
centerPoint = GetCenter(obj.gameObject);
//得到模型最长端作为计算视点距离
if (maxX > maxY)
{
maxNum = maxX;
}
else
{
maxNum = maxY;
}
if (maxNum < 5) maxNum = 5;
//获得模型正前方视点
Camera.main.transform.position = new Vector3(centerPoint.x, centerPoint.y, centerPoint.z - maxNum);
//================================================
//方法二:如果obj对象为静态对象,则用此方法
//Renderer[] mrs = obj.GetComponentsInChildren<Renderer>();
//Vector3 center = obj.transform.position;
//if (mrs.Length != 0)
//{
// Bounds bounds = new Bounds(center, Vector3.zero);
// foreach (Renderer item in mrs)
// {
// bounds.Encapsulate(item.bounds);
// }
// center = bounds.center;
// print("中心点:" + center + "最大点:" + bounds.max + "最小点:" + bounds.min);
// if ((bounds.max.x - bounds.min.x) > (bounds.max.y - bounds.min.y))
// {
// maxNum = bounds.max.x - bounds.min.x;
// }else if((bounds.max.x - bounds.min.x) < (bounds.max.y - bounds.min.y))
// {
// maxNum = bounds.max.y - bounds.min.y;
// }
// else
// {
// maxNum = bounds.max.x - bounds.min.x;
// }
// print(maxNum);
// //限制模型过小,导致定位过近
// if (maxNum < 8)
// {
// maxNum = 8;
// }
// print(maxNum);
//}
//centerPoint = center;
//================================================
//高亮
Highlighter highlighter = obj.gameObject.AddComponent<Highlighter>();//添加高亮脚本
highlighter.ConstantOn();
}
// Update is called once per frame
void Update()
{
//拖动
if (Input.GetMouseButton(2))
{
//根据距离实时调节拖动灵敏度
MovingSpeed = Vector3.Distance(transform.position, centerPoint) / 40;
delta_x = Input.GetAxis("Mouse X") * MovingSpeed;
delta_y = Input.GetAxis("Mouse Y") * MovingSpeed;
//rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
//transform.position = rotation * new Vector3(-delta_x, 0, -delta_y) + transform.position;
transform.Translate(-delta_x, -delta_y, 0);
}
//缩放
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
//缩放距离限制
if(Vector3.Distance(transform.position, centerPoint) < 5f)
{
delta_z = -Input.GetAxis("Mouse ScrollWheel") * ZoomSpeed;
if (delta_z > 0)
{
transform.Translate(0, 0, -delta_z);
}
}else if (Vector3.Distance(transform.position, centerPoint) > maxNum*2f)
{
delta_z = -Input.GetAxis("Mouse ScrollWheel") * ZoomSpeed;
if (delta_z < 0)
{
transform.Translate(0, 0, -delta_z);
}
}
else
{
delta_z = -Input.GetAxis("Mouse ScrollWheel") * ZoomSpeed;
transform.Translate(0, 0, -delta_z);
}
}
//旋转
if (Input.GetMouseButton(1))
{
//根据距离调节旋转灵敏度
//RotateSpeed = Vector3.Distance(transform.position, centerPoint) / 80;
delta_rotation_x = Input.GetAxis("Mouse X") * RotateSpeed;
delta_rotation_y = -Input.GetAxis("Mouse Y") * RotateSpeed;
position = transform.rotation * new Vector3(0, 0, distance) + transform.position;
transform.Rotate(0, delta_rotation_x, 0, Space.World);
transform.Rotate(delta_rotation_y, 0, 0);
transform.position = transform.rotation * new Vector3(0, 0, -distance) + position;
}
}
/// <summary>
/// 获得中心点
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
private Vector3 GetCenter(GameObject target)
{
Renderer[] mrs = target.GetComponentsInChildren<Renderer>();
Vector3 center = target.transform.position;
if (mrs.Length != 0)
{
Bounds bounds = new Bounds(center, Vector3.zero);
foreach (Renderer item in mrs)
{
bounds.Encapsulate(item.bounds);
}
center = bounds.center;
}
return center;
}
}