154 lines
4.9 KiB
C#
154 lines
4.9 KiB
C#
using System.Collections.Generic;
|
||
using System.IO;
|
||
using UnityEngine;
|
||
using UnityEditor;
|
||
using UnityEditor.SceneManagement;
|
||
using UnityEngine.SceneManagement;
|
||
using UnityEngine.Rendering;
|
||
using UnityEditorGameObjectUtility = UnityEditor.GameObjectUtility;
|
||
|
||
/// <summary>
|
||
/// 场景操作工具类
|
||
/// </summary>
|
||
public static class SceneUtils
|
||
{
|
||
/// <summary>
|
||
/// 递归设置GameObject的静态标志
|
||
/// </summary>
|
||
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);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 给地面移除MeshCollider
|
||
/// </summary>
|
||
public static void RemoveCollider(GameObject ground)
|
||
{
|
||
if (ground != null)
|
||
{
|
||
Object.DestroyImmediate(ground.GetComponent<Collider>());
|
||
/*var boxCollider = ground.gameObject.AddComponent<BoxCollider>();
|
||
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");
|
||
}
|
||
}
|
||
|
||
/// <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");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置地面渲染队列
|
||
/// </summary>
|
||
public static void SetGroundRenderQueue(GameObject ground, int renderQueue)
|
||
{
|
||
if (ground != null)
|
||
{
|
||
UnityEditorGameObjectUtility.SetStaticEditorFlags(ground, StaticEditorFlags.ContributeGI);
|
||
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())
|
||
{
|
||
UnityEngine.EventSystems.EventSystem[] eventSystems =
|
||
rootObj.GetComponentsInChildren<UnityEngine.EventSystems.EventSystem>();
|
||
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;
|
||
}
|
||
} |