Files
JJBB/Assets/Project/Script/Common/Utilities/EditorTestUtility.cs

43 lines
1.4 KiB
C#
Raw Normal View History

2024-08-23 15:49:34 +08:00
#if UNITY_EDITOR
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
/// <summary>
/// 编辑器测试运行用的工具
/// </summary>
public static class EditorTestUtility
{
public static Dictionary<string, string> prefabDict;
public static void InitPrefabDict()
{
prefabDict = new Dictionary<string, string>();
var prefabPaths = from prefabPath in AssetDatabase.GetAllAssetPaths()
where Path.GetExtension(prefabPath) == ".prefab"
select new {name = Path.GetFileNameWithoutExtension(prefabPath), path = prefabPath};
foreach (var prefabPath in prefabPaths)
if (!prefabDict.ContainsKey(prefabPath.name))
prefabDict.Add(prefabPath.name, prefabPath.path);
}
public static GameObject LoadGameObject(string name)
{
if (prefabDict == null)
InitPrefabDict();
string path;
GameObject result = null;
// ReSharper disable once PossibleNullReferenceException
if (prefabDict.TryGetValue(name, out path))
{
result = AssetDatabase.LoadAssetAtPath<GameObject>(path);
// 特殊警告使用EffectBackup
if (path.StartsWith("Assets/Project3D/EffectBackup/"))
Debug.LogError("有物体试图调用EffectBackup路径的资源 " + path);
}
return result;
}
}
#endif