using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
using UnityEngine.Rendering;
using UnityEditorGameObjectUtility = UnityEditor.GameObjectUtility;
///
/// 场景操作工具类
///
public static class SceneUtils
{
///
/// 递归设置GameObject的静态标志
///
public static void SetStaticFlagsRecursive(GameObject obj, StaticEditorFlags flags)
{
// 设置当前物体的静态标志
UnityEditorGameObjectUtility.SetStaticEditorFlags(obj, flags);
// 递归设置所有子物体
for (int i = 0; i < obj.transform.childCount; i++)
{
Transform child = obj.transform.GetChild(i);
SetStaticFlagsRecursive(child.gameObject, flags);
}
}
///
/// 给地面移除MeshCollider
///
public static void RemoveCollider(GameObject ground)
{
if (ground != null)
{
Object.DestroyImmediate(ground.GetComponent());
/*var boxCollider = ground.gameObject.AddComponent();
boxCollider.size = new Vector3(1, 1, 1);
boxCollider.center = new Vector3(0, 0, 0.5f);*/
EditorSceneManager.MarkSceneDirty(ground.gameObject.scene);
EditorSceneManager.SaveOpenScenes();
AssetDatabase.Refresh();
}
else
{
Debug.LogError("地面节点不存在,无法添加BoxCollider");
}
}
///
/// 给地面移除MeshCollider
///
public static void AddBoxCollider(GameObject root)
{
if (root != null)
{
Object.DestroyImmediate(root.GetComponent());
var boxCollider = root.gameObject.AddComponent();
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");
}
}
///
/// 设置地面渲染队列
///
public static void SetGroundRenderQueue(GameObject ground, int renderQueue)
{
if (ground != null)
{
UnityEditorGameObjectUtility.SetStaticEditorFlags(ground, StaticEditorFlags.ContributeGI);
var renderer = ground.GetComponent();
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();
}
}
///
/// 移除Scene中的重复EventSystem
///
public static void RemoveRedundantEventSystems(Scene scene)
{
foreach (var rootObj in scene.GetRootGameObjects())
{
UnityEngine.EventSystems.EventSystem[] eventSystems =
rootObj.GetComponentsInChildren();
if (null == eventSystems)
continue;
for (var i = 0; i < eventSystems.Length; ++i)
{
Object.DestroyImmediate(eventSystems[i].gameObject);
}
}
}
///
/// 设置雾的位置
///
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);
}
///
/// 获取标准化的资源路径
///
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;
}
}