Unity实现360度可旋转、缩放单独看模型
创作时间:
作者:
@小白创作中心
Unity实现360度可旋转、缩放单独看模型
引用
CSDN
1.
https://m.blog.csdn.net/weixin_48388330/article/details/138290070
本文介绍如何在Unity中实现一个可以360度旋转和缩放的模型展示功能。通过自定义脚本控制模型的旋转和缩放,结合鼠标交互实现直观的模型查看体验。
一、说明
实现模型的单独交互,显示在面板上,可以进行旋转和缩放操作。
二、代码演示
using UnityEngine;
public class ModelShowControl : MonoBehaviour
{
[SerializeField][Header("渲染展示相机")]
private GameObject showCamera;
[SerializeField][Header("展示交互的物品")]
private GameObject showGameObject;
[SerializeField][Header("是否自动旋转")]
private bool isAutoRotate;
[SerializeField][Header("自动旋转方向,例如(0,1,0)按照Y轴旋转")]
private Vector3 autoRotateDirection;
[SerializeField][Header("是否按照自身坐标系轴自动旋转")]
private bool isAutoRotatePivot;
[SerializeField][Header("自动旋转速度")]
private float autoRotationSpeed = 5;
[SerializeField][Header("鼠标旋转速度")]
private float mouseRotateSpeed = 0.5f;
[SerializeField][Header("是否反向旋转X")]
private bool isRotateInvertX;
[SerializeField][Header("是否反向旋转Y")]
private bool isRotateInvertY;
[SerializeField][Header("缩放速度")]
private float zoomSpeed = 0.5f;
[SerializeField][Header("是否反向缩放")]
private bool isZoomInvert;
[SerializeField][Header("最近缩放距离")]
private float minZoomDistance = 1f;
[SerializeField][Header("最远缩放距离")]
private float maxZoomDistance = 10f;
private Vector3 cameraInitPos;
private Vector3 lastMousePosition;
private bool isRotating; //是否正在旋转
private Vector3[] showGameObjectInitTrans = new Vector3[3];
public static ModelShowControl Instance;
private void Awake()
{
Instance = this;
cameraInitPos = showCamera.transform.position;
}
public void SetShowGameObject(GameObject _gameObject)
{
showGameObjectInitTrans[0] = _gameObject.transform.position;
showGameObjectInitTrans[1] = _gameObject.transform.eulerAngles;
showGameObjectInitTrans[2] = _gameObject.transform.localScale;
showGameObject = _gameObject;
showCamera.transform.position = cameraInitPos;
}
public void RestoreShowGameObject()
{
showGameObject.transform.position = showGameObjectInitTrans[0];
showGameObject.transform.eulerAngles = showGameObjectInitTrans[1];
showGameObject.transform.localScale = showGameObjectInitTrans[2];
showGameObject = null;
showCamera.transform.position = cameraInitPos;
isRotating = false;
}
void Update()
{
if (showGameObject == null)
return;
if (isAutoRotate && !Input.GetMouseButton(0)) // 如果鼠标左键没按下
RotateModelContinuously(); // 持续旋转模型
else
RotateModelOnMouseDrag(); // 根据鼠标拖拽旋转模型
ZoomCamera();
}
void RotateModelContinuously()
{
if (!isAutoRotatePivot)
showGameObject.transform.Rotate(autoRotateDirection, autoRotationSpeed * Time.deltaTime, Space.World);
else
{
var rotateEuler = autoRotateDirection * autoRotationSpeed * Time.deltaTime;
// 根据模型当前朝向构造一个围绕轴旋转的四元数
Quaternion deltaRotation = Quaternion.Euler(rotateEuler);
// 将新的旋转应用到模型
showGameObject.transform.rotation *= deltaRotation;
}
}
void RotateModelOnMouseDrag()
{
if (Input.GetMouseButtonDown(0))
{
isRotating = true;
lastMousePosition = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0))
{
isRotating = false;
}
if (isRotating && (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0))
{
Vector3 currentMousePosition = Input.mousePosition;
Vector3 mouseDeltaPosition = currentMousePosition - lastMousePosition;
float rotationX = -mouseDeltaPosition.y * mouseRotateSpeed;
float rotationY = mouseDeltaPosition.x * mouseRotateSpeed;
showGameObject.transform.Rotate(Vector3.up, isRotateInvertX ? rotationY : -rotationY, Space.World);
showGameObject.transform.Rotate(Vector3.right, isRotateInvertY ? rotationX : -rotationX, Space.World);
lastMousePosition = currentMousePosition;
}
}
void ZoomCamera()
{
// 获取鼠标滚轮的滚动值
float scrollWheelInput = Input.GetAxis("Mouse ScrollWheel");
if (scrollWheelInput == 0) return;
// 计算缩放后的相机距离
float zoomDistance = showCamera.transform.localPosition.z -
scrollWheelInput * (isZoomInvert ? zoomSpeed : -zoomSpeed);
zoomDistance = Mathf.Clamp(zoomDistance, -maxZoomDistance, -minZoomDistance);
// 设置相机距离
showCamera.transform.localPosition = new Vector3(cameraInitPos.x, cameraInitPos.y, zoomDistance);
}
}
三、使用说明
- 设置一个需要显示交互的模型相机
- 将相机拖入ShowCamera中,需要展示的模型可以在运行时直接放入,需要使用
SetShowGameObject(showGameObject);//调用方法
也可以运行时添加显示模型:
ModelShowControl.Instance.SetShowGameObject(showGameObject);//调用方法
配合鼠标射线检测进行如下调用(要展示的模型设置Tag为ModelShow):
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ModelShowManager : MonoBehaviour
{
public GameObject modelShowGameObjectParent;
public GameObject modelShowPanel;
public Button closeModelShowPanelBtn;
private void Awake()
{
closeModelShowPanelBtn.onClick.AddListener(() =>
{
modelShowPanel.SetActive(false);
ModelShowControl.Instance.RestoreShowGameObject();
});
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
if (hitInfo.collider.CompareTag("ModelShow"))
{
modelShowPanel.SetActive(true);
for (int i = 0; i < modelShowGameObjectParent.transform.childCount; i++)
{
var child = modelShowGameObjectParent.transform.GetChild(i).gameObject;
if (child.name == hitInfo.collider.gameObject.name)
{
child.SetActive(true);
ModelShowControl.Instance.SetShowGameObject(child);
}
else
{
child.SetActive(false);
}
}
}
}
}
}
}
- 添加一个New Render Texture给相机上
- 添加Image的UI,创建一个材质,选择
将New Render Texture赋给该材质,然后将材质赋给Image
四、演示结果
现在就可以360度显示模型,可以鼠标交互旋转缩放。
热门推荐
三国十大战役:从界桥到赤壁,哪些战役真正改变了历史走向?
三国时期的人口数量与分布探秘
安宫牛黄丸:中医界的“救命神器”如何科学使用?
天文学家发现55颗高速恒星
1070元的机票 第三方订票平台卖3357元?差价引发争议
靠“智商税”扭亏,屡遭用户质疑,高铁管家母公司赴港上市
汽车静电如何有效消除?消除静电的方法有哪些实用技巧?
动物繁殖的方式有哪些呢(盘点五种动物独特的繁殖方式)
加州草原与西藏高原:草原生态保护的两种路径
它自身的辐射将其撕裂:R136a1是宇宙中质量最大、温度最高的恒星
职场成功的秘诀:有效沟通
余莳华教你通背拳实战技巧
自动挡驾驶误区,你中了几枪?
狗狗一天的睡眠时间有多久
卤肉历史与文化溯源
文昌航天超算中心打造航天数据产业链 研学游热度攀升
破壁机常见故障及解决方法(探究破壁机的故障原因和解决之道)
小心身边的电气火灾隐患!这些安全事项要牢记
黄芪:提升白细胞的天然良药
牛黄:心脑血管病的守护神
牛黄:从《神农本草经》到外交国礼的传奇历程
东吴大帝孙权:从英明到昏庸的转变
实力与机遇:探讨东吴一统天下的可能性
韵达快递春节停运,其他快递怎么选?
中医养生视角下的皮冻制作,调和营养,滋养身心
印度"硅谷"班加罗尔缺水危机加剧,科技企业纷纷外迁
44张搞笑动物照片大放送!尴尬瞬间让你捧腹大笑
狗狗一天的睡眠时间有多久
敏奇教你正确存放盐酸氮䓬斯汀片
春节高速免费8天,春运抢票进入高峰期