Unity Editor獲取文件路徑

因?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!");
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容