Unity Editor获取文件路径

因为经常用到,还要到旧项目去找,因此记录一下:

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);
            //输出结果为: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);
            ///输出结果为:Volumes/Eevee_4TB/ZKCM/Test/Draw/Assets/测试文件.png
        }
    }

    [MenuItem("Tools/遍历全项目Texture文件相对路径")]
    public static void TraverseTexturesPath()
    {
        foreach (var guid in AssetDatabase.FindAssets("t:Texture"))//此处Texture类型也可以是Model、Audio之类
        {
            var assetPath = AssetDatabase.GUIDToAssetPath(guid);
            Debug.Log(assetPath);
            //后续可以用以下加载方式继续操作
            //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!");
    }
}
Unity 中,可以使用 `EditorUtility.OpenFilePanel` 方法来打开一个弹窗选择文件,并获取选择的文件路径。具体步骤如下: 1. 在 Unity 编辑器中,创建一个按钮或其他 UI 元素,用于触发选择文件的操作。 2. 在按钮或 UI 元素的点击事件中,调用 `EditorUtility.OpenFilePanel` 方法。 3. 在 `EditorUtility.OpenFilePanel` 方法中传入弹窗的标题、默认目录、默认文件名和文件类型过滤器等参数。 4. 在方法调用完成后,判断用户是否选择了文件,如果选择了文件,则获取选择的文件路径。 下面是一个示例代码: ```c# using UnityEditor; public class FileSelectionExample { [MenuItem("Examples/Select File")] public static void SelectFile() { string title = "Select a file"; string directory = "Assets/"; string defaultName = "example.txt"; string extension = "txt"; string filter = $"Text files (*.{extension})|*.{extension}"; string filePath = EditorUtility.OpenFilePanel(title, directory, extension); if (!string.IsNullOrEmpty(filePath)) { Debug.Log($"Selected file: {filePath}"); } else { Debug.Log("No file selected"); } } } ``` 在上面的代码中,我们定义了一个名为 `SelectFile` 的静态方法,并在该方法上添加了 `MenuItem` 属性,用于在 Unity 编辑器中创建一个菜单,当用户点击该菜单时,会触发 `SelectFile` 方法。 在 `SelectFile` 方法中,我们调用了 `EditorUtility.OpenFilePanel` 方法,并传入了弹窗的标题、默认目录、默认文件名、文件类型过滤器等参数。如果用户选择了文件,则会得到选择的文件路径,并在控制台输出该路径。如果用户没有选择文件,则会输出 "No file selected"。 注意:`EditorUtility.OpenFilePanel` 方法只能在 Unity 编辑器中使用,不能在游戏运行时使用。如果需要在游戏运行时选择文件,可以使用 `System.Windows.Forms.OpenFileDialog` 等其他方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值