U3D_TobaccoWarehouseISMDTSy.../Assets/Editor/ModelColliderEditor.cs

158 lines
5.4 KiB
C#

using UnityEngine;
using UnityEditor;
using System.Collections;
using Unity.EditorCoroutines.Editor;
#if UNITY_EDITOR
public class ModelColliderEditor : EditorWindow
{
private static float firstLayerMoveDistance = 1; // 第一层的移动距离
private static float explosionDuration = 1f; // 爆炸动画持续时间
private static readonly string[] Directions = {
"前", "后", "左", "右",
"上", "下", "前上", "前下",
"后上", "后下", "左上", "左下",
"右上", "右下"
};
[MenuItem("Tools/Add Mesh Colliders")]
public static void AddMeshColliders()
{
GameObject selectedObject = Selection.activeGameObject;
if (selectedObject == null)
{
Debug.LogError("No object selected.");
return;
}
// 遍历第一层级子物体
foreach (Transform child in selectedObject.transform)
{
AddMeshColliderAndDirection(child, selectedObject.name); // 直接调用处理函数
// 递归添加给子物体
AddMeshCollidersToChildren(child); // 使用自身位置作为新的中心
}
}
private static void AddMeshCollidersToChildren(Transform parent)
{
foreach (Transform child in parent)
{
AddMeshColliderAndDirection(child, parent.name); // 直接调用处理函数
// 递归调用
AddMeshCollidersToChildren(child); // 计算子物体的中心
}
}
private static void AddMeshColliderAndDirection(Transform child, string parentName)
{
// 添加 MeshCollider
if (child.GetComponent<MeshCollider>() == null)
{
child.gameObject.AddComponent<MeshCollider>();
}
// 添加 ModelChilderComponent 脚本
ModelChilderComponent component = child.gameObject.AddComponent<ModelChilderComponent>();
component.Parent = parentName;
// 随机选择方位
string randomDirection = Directions[Random.Range(0, Directions.Length)];
component.Direction = randomDirection;
Debug.Log($"{child.name} is at {component.Direction} from its parent.");
}
[MenuItem("Tools/Explode Objects")]
public static void ExplodeObjects()
{
GameObject selectedObject = Selection.activeGameObject;
if (selectedObject == null)
{
Debug.LogError("No object selected.");
return;
}
foreach (Transform child in selectedObject.transform)
{
EditorCoroutineUtility.StartCoroutine(Explode(child), child);
}
}
private static IEnumerator Explode(Transform parent)
{
float moveDistance = CalculateMoveDistance(0); // 假设为第一层
Vector3 targetPosition = GetTargetPosition(parent, moveDistance);
float elapsedTime = 0f;
Vector3 startingPosition = parent.localPosition;
while (elapsedTime < explosionDuration)
{
float t = elapsedTime / explosionDuration;
parent.localPosition = Vector3.Lerp(startingPosition, targetPosition, t);
elapsedTime += Time.deltaTime;
yield return null; // 等待下一帧
}
parent.localPosition = targetPosition; // 确保最终位置
}
private static Vector3 GetTargetPosition(Transform child, float distance)
{
string direction = child.GetComponent<ModelChilderComponent>()?.Direction;
switch (direction)
{
case "前":
return child.localPosition + new Vector3(0, 0, distance);
case "后":
return child.localPosition + new Vector3(0, 0, -distance);
case "左":
return child.localPosition + new Vector3(-distance, 0, 0);
case "右":
return child.localPosition + new Vector3(distance, 0, 0);
case "上":
return child.localPosition + new Vector3(0, distance, 0);
case "下":
return child.localPosition + new Vector3(0, -distance, 0);
case "前上":
return child.localPosition + new Vector3(0, distance, distance);
case "前下":
return child.localPosition + new Vector3(0, -distance, distance);
case "后上":
return child.localPosition + new Vector3(0, distance, -distance);
case "后下":
return child.localPosition + new Vector3(0, -distance, -distance);
case "左上":
return child.localPosition + new Vector3(-distance, distance, 0);
case "左下":
return child.localPosition + new Vector3(-distance, -distance, 0);
case "右上":
return child.localPosition + new Vector3(distance, distance, 0);
case "右下":
return child.localPosition + new Vector3(distance, -distance, 0);
default:
return child.localPosition; // 默认不移动
}
}
private static float CalculateMoveDistance(int level)
{
if (level == 0)
return firstLayerMoveDistance; // 第一层的移动距离
else if (level == 1)
return 7f; // 第二层的移动距离
else if (level == 2)
return 5f; // 第三层的移动距离
return 0f; // 更深层次不移动
}
}
#endif