Update EditorExtUtil.cs

This commit is contained in:
ALEXTANG
2023-04-25 00:42:32 +08:00
parent 3f67b27e45
commit ab532c73d7

View File

@@ -7,8 +7,15 @@ using Object = UnityEngine.Object;
namespace TEngine namespace TEngine
{ {
/// <summary>
/// 编辑器运行时工具。
/// </summary>
public static class EditorExtUtil public static class EditorExtUtil
{ {
/// <summary>
/// 游戏物体保存为预制体。
/// </summary>
/// <param name="asset">游戏物体。</param>
public static void SavePrefabAsset(GameObject asset) public static void SavePrefabAsset(GameObject asset)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
@@ -17,6 +24,11 @@ namespace TEngine
#endif #endif
} }
/// <summary>
/// 创建资源。
/// </summary>
/// <param name="asset">游戏物体。</param>
/// <param name="path">资源路径。</param>
public static void CreateAsset(Object asset, string path) public static void CreateAsset(Object asset, string path)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
@@ -25,24 +37,39 @@ namespace TEngine
#endif #endif
} }
/// <summary>
/// 文件路径转化成guid。
/// </summary>
/// <param name="path">文件路径</param>
/// <returns>guid。</returns>
public static string AssetPathToGUID(string path) public static string AssetPathToGUID(string path)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
return UnityEditor.AssetDatabase.AssetPathToGUID(path); return UnityEditor.AssetDatabase.AssetPathToGUID(path);
#else #else
return ""; return "";
#endif #endif
} }
/// <summary>
/// 获取资源来源预制体。
/// </summary>
/// <param name="obj">资源实例。</param>
/// <returns>来源预制体。</returns>
public static UnityEngine.Object GetPrefabParent(UnityEngine.Object obj) public static UnityEngine.Object GetPrefabParent(UnityEngine.Object obj)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
return UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(obj); return UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(obj);
#else #else
return null; return null;
#endif #endif
} }
/// <summary>
/// 获取资源路径。
/// </summary>
/// <param name="obj">资源。</param>
/// <returns>路径。</returns>
public static string GetAssetPath(GameObject obj) public static string GetAssetPath(GameObject obj)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
@@ -52,6 +79,11 @@ namespace TEngine
#endif #endif
} }
/// <summary>
/// 获取资源路径。
/// </summary>
/// <param name="obj">资源。</param>
/// <returns>路径。</returns>
public static string GetAssetPath(ScriptableObject obj) public static string GetAssetPath(ScriptableObject obj)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
@@ -61,6 +93,12 @@ namespace TEngine
#endif #endif
} }
/// <summary>
/// 获取AB包中的资源依赖。
/// </summary>
/// <param name="assetBundleName">The name of the AssetBundle for which dependencies are required.</param>
/// <param name="recursive">If false, returns only AssetBundles which are direct dependencies of the input; if true, includes all indirect dependencies of the input.</param>
/// <returns>returns the list of AssetBundles that it depends on.</returns>
public static string[] GetAssetBundleDependencies(string assetBundleName, bool recursive) public static string[] GetAssetBundleDependencies(string assetBundleName, bool recursive)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
@@ -70,6 +108,11 @@ namespace TEngine
#endif #endif
} }
/// <summary>
/// guid转化成文件路径。
/// </summary>
/// <param name="guid">guid。</param>
/// <returns>文件路径。</returns>
public static string GUIDToAssetPath(string guid) public static string GUIDToAssetPath(string guid)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
@@ -79,33 +122,55 @@ namespace TEngine
#endif #endif
} }
/// <summary>
/// 编辑器下加载资源。
/// </summary>
/// <param name="assetPath">资源地址。</param>
/// <typeparam name="T">资源类型。</typeparam>
/// <returns>资源实例。</returns>
public static T LoadAssetAtPath<T>(string assetPath) where T : Object public static T LoadAssetAtPath<T>(string assetPath) where T : Object
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
return UnityEditor.AssetDatabase.LoadAssetAtPath<T>(assetPath); return UnityEditor.AssetDatabase.LoadAssetAtPath<T>(assetPath);
#else #else
return null; return null;
#endif #endif
} }
/// <summary>
/// 获取资源包名称里的资源列表。
/// </summary>
/// <param name="assetBundleName">资源包名称。</param>
public static string[] GetAssetPathsFromAssetBundle(string assetBundleName) public static string[] GetAssetPathsFromAssetBundle(string assetBundleName)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
return UnityEditor.AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName); return UnityEditor.AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
#else #else
return null; return null;
#endif #endif
} }
/// <summary>
/// 开关宏定义。
/// </summary>
/// <param name="def">宏定义。</param>
/// <param name="isOn">是否开启。</param>
public static void ToggleScriptingDefineSymbols(string def, bool isOn) public static void ToggleScriptingDefineSymbols(string def, bool isOn)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
ToggleScriptingDefineSymbols(def, isOn, (int)BuildTargetGroup.Standalone); ToggleScriptingDefineSymbols(def, isOn, (int)BuildTargetGroup.Standalone);
ToggleScriptingDefineSymbols(def, isOn, (int)BuildTargetGroup.Android); ToggleScriptingDefineSymbols(def, isOn, (int)BuildTargetGroup.Android);
ToggleScriptingDefineSymbols(def, isOn, (int)BuildTargetGroup.iOS);
#endif #endif
} }
static void ToggleScriptingDefineSymbols(string def, bool isOn, int type) /// <summary>
/// 开关宏定义。
/// </summary>
/// <param name="def">宏定义。</param>
/// <param name="isOn">是否开启。</param>
/// <param name="type">BuildTargetGroup打包平台类型。</param>
public static void ToggleScriptingDefineSymbols(string def, bool isOn, int type)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
var targetGroup = (BuildTargetGroup)type; var targetGroup = (BuildTargetGroup)type;
@@ -128,13 +193,21 @@ namespace TEngine
} }
public static void SetDirty(ScriptableObject obj) /// <summary>
/// 设置脏标记。
/// </summary>
/// <param name="obj">ScriptableObject。</param>
public static void SetDirty(this ScriptableObject obj)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(obj); UnityEditor.EditorUtility.SetDirty(obj);
#endif #endif
} }
/// <summary>
/// 导入资源。
/// </summary>
/// <param name="path">资源定位地址。</param>
public static void ImportAsset(string path) public static void ImportAsset(string path)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
@@ -142,7 +215,11 @@ namespace TEngine
#endif #endif
} }
public static void SaveTblConfig(ScriptableObject config) /// <summary>
/// 把ScriptableObject保存为CSV表格。
/// </summary>
/// <param name="config">ScriptableObject。</param>
public static void SaveTblConfig(this ScriptableObject config)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
EditorUtility.SetDirty(config); EditorUtility.SetDirty(config);
@@ -156,6 +233,14 @@ namespace TEngine
} }
/// <summary>
/// 实例化游戏物体操作。
/// </summary>
/// <param name="original">来源游戏物体。</param>
/// <param name="position">实例化的位置。</param>
/// <param name="rotation">实例化的四元数。</param>
/// <param name="parent">实例化的父节点。</param>
/// <returns>实例化游戏物体。</returns>
public static GameObject Instantiate( public static GameObject Instantiate(
GameObject original, GameObject original,
Vector3 position, Vector3 position,
@@ -178,6 +263,9 @@ namespace TEngine
#endif #endif
} }
/// <summary>
/// 停止运行游戏。
/// </summary>
public static void StopGame() public static void StopGame()
{ {
#if UNITY_EDITOR #if UNITY_EDITOR