c# - 如何从 Unity 上的 C# 静态类获取当前文件夹

标签 c# unity-game-engine

我导入了一个 Unity 包,其中多个编辑器脚本依赖于静态类中的 RootPath 属性:

public static class EditorUtils
{
    public static string RootPath => "Assets/HardcodedPath";
}

问题在于该路径是硬编码的,因此包位置根本无法更改。我计划更改 RootPath 属性,以便它获取当前脚本位置并使用给定模式搜索父文件夹。这样,即使在我自己的自定义包中(即在 Packages 文件夹而不是 Assets 文件夹中),我也可以使用此包。

缺少的部分是我找不到从代码中获取此静态类文件夹位置的方法。谁能帮我解决这个问题吗?

最佳答案

深入研究 AssetDatabase 并找到对您的脚本的引用。这应该返回您想要的内容。

public static class EditorUtils
{
    public static string RootPath
    {
        get
        {
            var g = AssetDatabase.FindAssets ( $"t:Script {nameof(EditorUtils)}" );
            return AssetDatabase.GUIDToAssetPath ( g [ 0 ] );
        }
    }
}

这也适用于包中的脚本。 请记住将 EditorUtils 更改为您的脚本名称。

Here's相关的 Unity 文档。

关于c# - 如何从 Unity 上的 C# 静态类获取当前文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65013213/

相关文章:

unity-game-engine - 统一 : video is playing without sound (2017. 3)

c# - 如何获取任务管理器中显示的事件窗口应用程序名称

c# - c++:使用类型作为 map /字典的键?

c# - 未满18岁如何不注册

c# - 使用 System.Threading 使用 50% CPU 的简单 .exe

unity-game-engine - 对 'Script Execution Order' 的任何更改都会立即恢复

c# - 如何在Unity3d C#关卡中发送一次消息

java - 命令调用失败 : Failed to sign APK package

c# - Unity - 如何将运行时源添加到 ParentConstraint

c# - C# 中的图像转换