2025-04-25 15:07:22 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
|
using UnityEditor.SceneManagement;
|
|
|
|
|
|
using UnityEngine.SceneManagement;
|
|
|
|
|
|
using UnityEngine.Rendering;
|
2026-01-13 13:20:29 +08:00
|
|
|
|
using UnityEditorGameObjectUtility = UnityEditor.GameObjectUtility;
|
2025-04-25 15:07:22 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 场景操作工具类
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class SceneUtils
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 递归设置GameObject的静态标志
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void SetStaticFlagsRecursive(GameObject obj, StaticEditorFlags flags)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 设置当前物体的静态标志
|
2026-01-13 13:20:29 +08:00
|
|
|
|
UnityEditorGameObjectUtility.SetStaticEditorFlags(obj, flags);
|
2025-04-25 15:07:22 +08:00
|
|
|
|
|
|
|
|
|
|
// 递归设置所有子物体
|
|
|
|
|
|
for (int i = 0; i < obj.transform.childCount; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
Transform child = obj.transform.GetChild(i);
|
|
|
|
|
|
SetStaticFlagsRecursive(child.gameObject, flags);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-11-27 17:03:33 +08:00
|
|
|
|
/// 给地面移除MeshCollider
|
2025-04-25 15:07:22 +08:00
|
|
|
|
/// </summary>
|
2025-11-27 17:03:33 +08:00
|
|
|
|
public static void RemoveCollider(GameObject ground)
|
2025-04-25 15:07:22 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (ground != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Object.DestroyImmediate(ground.GetComponent<Collider>());
|
2025-11-27 17:03:33 +08:00
|
|
|
|
/*var boxCollider = ground.gameObject.AddComponent<BoxCollider>();
|
2025-04-25 15:07:22 +08:00
|
|
|
|
boxCollider.size = new Vector3(1, 1, 1);
|
2025-11-27 17:03:33 +08:00
|
|
|
|
boxCollider.center = new Vector3(0, 0, 0.5f);*/
|
2025-04-25 15:07:22 +08:00
|
|
|
|
|
|
|
|
|
|
EditorSceneManager.MarkSceneDirty(ground.gameObject.scene);
|
|
|
|
|
|
EditorSceneManager.SaveOpenScenes();
|
|
|
|
|
|
AssetDatabase.Refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError("地面节点不存在,无法添加BoxCollider");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-27 17:03:33 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 给地面移除MeshCollider
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void AddBoxCollider(GameObject root)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (root != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Object.DestroyImmediate(root.GetComponent<Collider>());
|
|
|
|
|
|
var boxCollider = root.gameObject.AddComponent<BoxCollider>();
|
|
|
|
|
|
boxCollider.size = new Vector3(1, 1, 1);
|
|
|
|
|
|
boxCollider.center = new Vector3(0, 0, 0.5f);
|
|
|
|
|
|
|
|
|
|
|
|
EditorSceneManager.MarkSceneDirty(root.gameObject.scene);
|
|
|
|
|
|
EditorSceneManager.SaveOpenScenes();
|
|
|
|
|
|
AssetDatabase.Refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError("BoxCollider节点不存在,无法添加BoxCollider");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-25 15:07:22 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置地面渲染队列
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void SetGroundRenderQueue(GameObject ground, int renderQueue)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ground != null)
|
|
|
|
|
|
{
|
2026-01-13 13:20:29 +08:00
|
|
|
|
UnityEditorGameObjectUtility.SetStaticEditorFlags(ground, StaticEditorFlags.ContributeGI);
|
2025-04-25 15:07:22 +08:00
|
|
|
|
var renderer = ground.GetComponent<Renderer>();
|
|
|
|
|
|
if (renderer != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
renderer.shadowCastingMode = ShadowCastingMode.Off;
|
|
|
|
|
|
var materials = renderer.sharedMaterials;
|
|
|
|
|
|
foreach (var material in materials)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (material != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
renderer.shadowCastingMode = ShadowCastingMode.Off;
|
|
|
|
|
|
material.renderQueue = renderQueue;
|
|
|
|
|
|
Debug.Log($"将地面的材质 {material.name} 的RenderQueue改为{renderQueue}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 刷新资源以确保RenderQueue修改已保存
|
|
|
|
|
|
AssetDatabase.SaveAssets();
|
|
|
|
|
|
AssetDatabase.Refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 移除Scene中的重复EventSystem
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void RemoveRedundantEventSystems(Scene scene)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var rootObj in scene.GetRootGameObjects())
|
|
|
|
|
|
{
|
2025-11-27 17:03:33 +08:00
|
|
|
|
UnityEngine.EventSystems.EventSystem[] eventSystems =
|
|
|
|
|
|
rootObj.GetComponentsInChildren<UnityEngine.EventSystems.EventSystem>();
|
2025-04-25 15:07:22 +08:00
|
|
|
|
if (null == eventSystems)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < eventSystems.Length; ++i)
|
|
|
|
|
|
{
|
|
|
|
|
|
Object.DestroyImmediate(eventSystems[i].gameObject);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置雾的位置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void SetFogPosition(GameObject fog)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (fog == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
var transform = fog.transform;
|
|
|
|
|
|
var position = transform.position;
|
|
|
|
|
|
transform.position = new Vector3(position.x, position.y + 0.01f, position.z);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取标准化的资源路径
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static string GetAssetPath(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 已经是Assets路径格式
|
|
|
|
|
|
if (path.StartsWith("Assets"))
|
|
|
|
|
|
return path;
|
|
|
|
|
|
|
|
|
|
|
|
// 绝对路径转相对Assets路径
|
|
|
|
|
|
string fullPath = Path.GetFullPath(path);
|
|
|
|
|
|
string assetsFullPath = Path.GetFullPath(Application.dataPath);
|
|
|
|
|
|
|
|
|
|
|
|
if (fullPath.StartsWith(assetsFullPath))
|
|
|
|
|
|
return "Assets" + fullPath.Substring(assetsFullPath.Length).Replace('\\', '/');
|
|
|
|
|
|
|
|
|
|
|
|
return path;
|
|
|
|
|
|
}
|
2025-11-27 17:03:33 +08:00
|
|
|
|
}
|