因?yàn)榻?jīng)常用到,還要到舊項(xiàng)目去找,因此記錄一下:
using UnityEditor;
using UnityEngine;
public class TextureHelp : Editor
{
[MenuItem("Tools/獲取選中文件相對路徑")]
public static void GetFileRelativePath()
{
string[] GUIDs = Selection.assetGUIDs;
foreach (var guid in GUIDs)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
Debug.Log(path);
//輸出結(jié)果為:Assets/測試文件.png
}
}
[MenuItem("Tools/獲取選中文件絕對路徑")]
public static void GetFileAbsolutePath()
{
string[] GUIDs = Selection.assetGUIDs;
foreach (var guid in GUIDs)
{
string path = Application.dataPath.Remove(Application.dataPath.Length - 6) + AssetDatabase.GUIDToAssetPath(guid);
Debug.Log(path);
///輸出結(jié)果為:Volumes/Eevee_4TB/ZKCM/Test/Draw/Assets/測試文件.png
}
}
[MenuItem("Tools/遍歷全項(xiàng)目Texture文件相對路徑")]
public static void TraverseTexturesPath()
{
foreach (var guid in AssetDatabase.FindAssets("t:Texture"))//此處Texture類型也可以是Model、Audio之類
{
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
Debug.Log(assetPath);
//后續(xù)可以用以下加載方式繼續(xù)操作
//var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
}
}
[MenuItem("Tools/遍歷子孫物體", false, 1)]
static void SelectionObjects()
{
GameObject[] selections = Selection.gameObjects;
foreach (GameObject selection in selections)//遍歷每個選中的物體
{
foreach (Transform child in selection.GetComponentsInChildren<Transform>())
{
//遍歷子物體及孫物體
Debug.Log(child.name);
}
//如果僅遍歷子物體
//foreach (Transform child in selection.transform)
//{
// Debug.Log(child.name);
//}
}
Debug.Log("SelectionObjects End!");
}
}