151 lines
4.3 KiB
C#
151 lines
4.3 KiB
C#
using UnityEngine;
|
||
using System.Collections.Generic;
|
||
using System;
|
||
|
||
// 定义一个可序列化的子网格类
|
||
[Serializable]
|
||
public class SubMeshes
|
||
{
|
||
// 网格渲染器
|
||
public MeshRenderer meshRenderer;
|
||
// 原始位置
|
||
public Vector3 originalPosition;
|
||
// 展开后的位置
|
||
public Vector3 explodedPosition;
|
||
}
|
||
|
||
// 定义一个三维模型功能类,继承自MonoBehaviour
|
||
public class ThreeDModelFunctions : MonoBehaviour
|
||
{
|
||
public static ThreeDModelFunctions Instance;
|
||
|
||
// 变量区域
|
||
#region Variables
|
||
// 子网格渲染器列表
|
||
public List<SubMeshes> childMeshRenderers;
|
||
// 是否在展开视图
|
||
public bool isInExplodedView = false;
|
||
// 展开速度
|
||
public float explosionSpeed = 0.1f;
|
||
// 是否正在移动
|
||
public bool isMoving = false;
|
||
#endregion
|
||
|
||
// Unity函数区域
|
||
#region UnityFunctions
|
||
|
||
// 在对象被激活时调用一次
|
||
private void Awake()
|
||
{
|
||
Instance=this;
|
||
Init();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化函数
|
||
/// </summary>
|
||
public void Init()
|
||
{
|
||
// 初始化子网格渲染器列表
|
||
childMeshRenderers = new List<SubMeshes>();
|
||
// 遍历所有子物体中的MeshRenderer组件
|
||
foreach (var item in GetComponentsInChildren<MeshRenderer>())
|
||
{
|
||
// 创建一个新的子网格对象
|
||
SubMeshes mesh = new SubMeshes();
|
||
// 设置网格渲染器
|
||
mesh.meshRenderer = item;
|
||
// 设置原始位置
|
||
mesh.originalPosition = item.transform.position;
|
||
// 设置展开后的位置
|
||
mesh.explodedPosition = item.bounds.center * 2f;
|
||
// 将子网格对象添加到列表中
|
||
childMeshRenderers.Add(mesh);
|
||
}
|
||
}
|
||
|
||
// 每帧调用一次
|
||
private void Update()
|
||
{
|
||
// 如果正在移动
|
||
if (isMoving)
|
||
{
|
||
// 如果在展开视图状态
|
||
if (isInExplodedView)
|
||
{
|
||
// 遍历所有子网格渲染器
|
||
foreach (var item in childMeshRenderers)
|
||
{
|
||
// 通过插值移动到展开位置
|
||
item.meshRenderer.transform.position = Vector3.Lerp(item.meshRenderer.transform.position, item.explodedPosition, explosionSpeed);
|
||
// 如果距离小于阈值,停止移动
|
||
if (Vector3.Distance(item.meshRenderer.transform.position, item.explodedPosition) < 0.001f)
|
||
{
|
||
isMoving = false;
|
||
}
|
||
}
|
||
}
|
||
// 如果不在展开视图状态
|
||
else
|
||
{
|
||
// 遍历所有子网格渲染器
|
||
foreach (var item in childMeshRenderers)
|
||
{
|
||
// 通过插值移动到原始位置
|
||
item.meshRenderer.transform.position = Vector3.Lerp(item.meshRenderer.transform.position, item.originalPosition, explosionSpeed);
|
||
// 如果距离小于阈值,停止移动
|
||
if (Vector3.Distance(item.meshRenderer.transform.position, item.originalPosition) < 0.001f)
|
||
{
|
||
isMoving = false;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
if (Input.GetKeyDown(KeyCode.Q))
|
||
{
|
||
ToggleExplodedView();
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
// 自定义函数区域
|
||
#region CustomFunctions
|
||
|
||
// 切换展开视图状态
|
||
public void ToggleExplodedView()
|
||
{
|
||
// 如果当前在展开视图
|
||
if (isInExplodedView)
|
||
{
|
||
// 切换到非展开视图
|
||
isInExplodedView = false;
|
||
// 开始移动
|
||
isMoving = true;
|
||
}
|
||
// 如果当前不在展开视图
|
||
else
|
||
{
|
||
// 切换到展开视图
|
||
isInExplodedView = true;
|
||
// 开始移动
|
||
isMoving = true;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置展开视图状态
|
||
/// </summary>
|
||
/// <param name="explode"></param>
|
||
public void ToggleExplodedView(bool explode)
|
||
{
|
||
// 根据传入的参数设置视图状态
|
||
isInExplodedView = explode;
|
||
// 开始移动
|
||
isMoving = true;
|
||
}
|
||
|
||
#endregion
|
||
}
|