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度显示模型,可以鼠标交互旋转缩放。
热门推荐
哪吒IP新作来袭:文化符号再升级!
冬至养生新姿势:安宫牛黄丸来帮忙
秋冬急救必备:北京同仁堂安宫牛黄丸使用全攻略
哪吒新造型揭秘:从丑娃到顶流的秘密!
《哪吒2》:打破命运枷锁的新英雄
哪吒:从佛到道的文化逆袭
电磁炉F6冬季安全使用全攻略
Mini Cooper后备箱打不开的原因是什么?
中外合作办学:跨校辅修的语言挑战与应对策略
上外英语专业跨校辅修全攻略:从申请到学习一文搞定!
北京大学法学院跨校辅修项目管理经验大揭秘!
揭秘哪吒闹海:从蚌埠九湾到银幕传奇
福田新春艺术节:京剧版《哪吒闹海新传》火爆出圈
正宗程式八卦掌:零基础也能学!
八卦掌:提升身体协调性的秘密武器
甘肃西和:八卦掌的神奇养生法
揭秘董海川与八卦掌的传奇故事
八卦掌基本功训练:你站对了吗?
从松下幸之助看企业家精神:如何成为经营之神?
前央视女主持人张蕾的新身份:北京电影学院副教授
成都熊猫灯会:元宵节必打卡!
成都元宵节:灯火璀璨映蓉城
成都元宵节亲子活动大集合,你最期待哪个?
家乡的土头碗,传统就像血缘将人们联系在一起|故乡的肉味
智能手机省流小妙招:高效工作不费劲!
颈椎病+视力受损:长时间玩手机的致命组合!
广西有一个鲜为人知的避暑小城,风景不输阳朔,比北海更幽静
白醋浇杜鹃花,效果惊人!
白醋养花新姿势,你get了吗?
用白醋养出爆盆绣球花!