增加怪物功能,调整灵灯参数
This commit is contained in:
@@ -2,11 +2,123 @@ using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
/// <summary>
|
||||
/// Elements底对齐工具 - 将Elements下所有物体底对齐到Y=0平面
|
||||
/// Elements底对齐工具 - 将选中的物体底对齐到同一Y坐标
|
||||
/// </summary>
|
||||
public class ElementsAligner : Editor
|
||||
{
|
||||
[MenuItem("Tools/Elements/底对齐到地面 (Y=0)")]
|
||||
[MenuItem("Tools/Elements/选中物体底对齐")]
|
||||
public static void AlignSelectedToBottom()
|
||||
{
|
||||
var selected = Selection.gameObjects;
|
||||
|
||||
if (selected == null || selected.Length < 2)
|
||||
{
|
||||
EditorUtility.DisplayDialog("提示", "请至少选中2个物体进行底对齐", "确定");
|
||||
return;
|
||||
}
|
||||
|
||||
// 找到所有选中物体中最低的底部Y坐标
|
||||
float lowestBottom = float.MaxValue;
|
||||
var boundsData = new (Transform transform, Bounds bounds)[selected.Length];
|
||||
int validCount = 0;
|
||||
|
||||
for (int i = 0; i < selected.Length; i++)
|
||||
{
|
||||
Transform t = selected[i].transform;
|
||||
Bounds bounds = GetObjectBounds(t);
|
||||
|
||||
if (bounds.size == Vector3.zero)
|
||||
{
|
||||
Debug.LogWarning($"跳过 {t.name} - 无法计算包围盒");
|
||||
continue;
|
||||
}
|
||||
|
||||
boundsData[validCount] = (t, bounds);
|
||||
validCount++;
|
||||
|
||||
if (bounds.min.y < lowestBottom)
|
||||
lowestBottom = bounds.min.y;
|
||||
}
|
||||
|
||||
if (validCount < 2)
|
||||
{
|
||||
EditorUtility.DisplayDialog("提示", "有效物体不足2个,无法对齐", "确定");
|
||||
return;
|
||||
}
|
||||
|
||||
// 将所有物体的底部对齐到最低点
|
||||
Undo.SetCurrentGroupName("选中物体底对齐");
|
||||
int count = 0;
|
||||
|
||||
for (int i = 0; i < validCount; i++)
|
||||
{
|
||||
var (t, bounds) = boundsData[i];
|
||||
Undo.RecordObject(t, $"Align {t.name}");
|
||||
|
||||
float bottomY = bounds.min.y;
|
||||
Vector3 newPosition = t.position;
|
||||
newPosition.y = t.position.y - bottomY + lowestBottom;
|
||||
t.position = newPosition;
|
||||
|
||||
count++;
|
||||
Debug.Log($"✓ 对齐 {t.name}: 底部Y {bottomY:F3} → {lowestBottom:F3}");
|
||||
}
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(
|
||||
UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene()
|
||||
);
|
||||
Debug.Log($"完成!底对齐了 {count} 个物体到 Y={lowestBottom:F3}");
|
||||
}
|
||||
}
|
||||
|
||||
[MenuItem("Tools/Elements/选中物体底对齐到Y=0")]
|
||||
public static void AlignSelectedToGround()
|
||||
{
|
||||
var selected = Selection.gameObjects;
|
||||
|
||||
if (selected == null || selected.Length == 0)
|
||||
{
|
||||
EditorUtility.DisplayDialog("提示", "请选中至少1个物体", "确定");
|
||||
return;
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
Undo.SetCurrentGroupName("选中物体底对齐到Y=0");
|
||||
|
||||
for (int i = 0; i < selected.Length; i++)
|
||||
{
|
||||
Transform t = selected[i].transform;
|
||||
Undo.RecordObject(t, $"Align {t.name}");
|
||||
|
||||
Bounds bounds = GetObjectBounds(t);
|
||||
|
||||
if (bounds.size == Vector3.zero)
|
||||
{
|
||||
Debug.LogWarning($"跳过 {t.name} - 无法计算包围盒");
|
||||
continue;
|
||||
}
|
||||
|
||||
float bottomY = bounds.min.y;
|
||||
Vector3 newPosition = t.position;
|
||||
newPosition.y = t.position.y - bottomY;
|
||||
t.position = newPosition;
|
||||
|
||||
count++;
|
||||
Debug.Log($"✓ 对齐 {t.name}: 底部Y {bottomY:F3} → 0");
|
||||
}
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(
|
||||
UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene()
|
||||
);
|
||||
Debug.Log($"完成!底对齐了 {count} 个物体到 Y=0");
|
||||
}
|
||||
}
|
||||
|
||||
[MenuItem("Tools/Elements/底对齐到地面 (Y=0) (Elements全部)")]
|
||||
public static void AlignElementsToGround()
|
||||
{
|
||||
GameObject elements = GameObject.Find("Elements");
|
||||
@@ -23,11 +135,8 @@ public class ElementsAligner : Editor
|
||||
for (int i = 0; i < elements.transform.childCount; i++)
|
||||
{
|
||||
Transform child = elements.transform.GetChild(i);
|
||||
|
||||
// 记录到Undo栈
|
||||
Undo.RecordObject(child, $"Align {child.name}");
|
||||
|
||||
// 获取物体的包围盒
|
||||
Bounds bounds = GetObjectBounds(child);
|
||||
|
||||
if (bounds.size == Vector3.zero)
|
||||
@@ -36,11 +145,9 @@ public class ElementsAligner : Editor
|
||||
continue;
|
||||
}
|
||||
|
||||
// 计算需要移动的距离:让物体底部对齐到Y=0
|
||||
float bottomY = bounds.min.y;
|
||||
Vector3 newPosition = child.position;
|
||||
newPosition.y = child.position.y - bottomY;
|
||||
|
||||
child.position = newPosition;
|
||||
|
||||
count++;
|
||||
@@ -52,69 +159,10 @@ public class ElementsAligner : Editor
|
||||
UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(
|
||||
UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene()
|
||||
);
|
||||
|
||||
EditorUtility.DisplayDialog("完成", $"已对齐 {count} 个物体到地面 (Y=0)", "确定");
|
||||
Debug.Log($"完成!对齐了 {count} 个物体");
|
||||
}
|
||||
}
|
||||
|
||||
[MenuItem("Tools/Elements/底对齐到指定高度")]
|
||||
public static void AlignElementsToCustomHeight()
|
||||
{
|
||||
string input = EditorInputDialog.Show("对齐到指定高度", "请输入目标Y坐标:", "0");
|
||||
|
||||
if (input == null) return; // 用户取消
|
||||
|
||||
if (!float.TryParse(input, out float targetY))
|
||||
{
|
||||
EditorUtility.DisplayDialog("错误", "请输入有效的数字", "确定");
|
||||
return;
|
||||
}
|
||||
|
||||
GameObject elements = GameObject.Find("Elements");
|
||||
|
||||
if (elements == null)
|
||||
{
|
||||
EditorUtility.DisplayDialog("错误", "找不到场景中的 'Elements' 物体", "确定");
|
||||
return;
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
Undo.SetCurrentGroupName("Align Elements to Custom Height");
|
||||
|
||||
for (int i = 0; i < elements.transform.childCount; i++)
|
||||
{
|
||||
Transform child = elements.transform.GetChild(i);
|
||||
Undo.RecordObject(child, $"Align {child.name}");
|
||||
|
||||
Bounds bounds = GetObjectBounds(child);
|
||||
|
||||
if (bounds.size == Vector3.zero)
|
||||
{
|
||||
Debug.LogWarning($"跳过 {child.name} - 无法计算包围盒");
|
||||
continue;
|
||||
}
|
||||
|
||||
float bottomY = bounds.min.y;
|
||||
Vector3 newPosition = child.position;
|
||||
newPosition.y = child.position.y - bottomY + targetY;
|
||||
|
||||
child.position = newPosition;
|
||||
|
||||
count++;
|
||||
Debug.Log($"✓ 对齐 {child.name}: 底部Y从 {bottomY:F3} 到 {targetY:F3}");
|
||||
}
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(
|
||||
UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene()
|
||||
);
|
||||
|
||||
EditorUtility.DisplayDialog("完成", $"已对齐 {count} 个物体到 Y={targetY}", "确定");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取物体的世界空间包围盒(包括所有子物体和渲染器)
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user