[英]How to get current folder from a C# static class on Unity
我導入了一個 Unity package,其中幾個編輯器腳本依賴於 static class 的RootPath
屬性:
public static class EditorUtils
{
public static string RootPath
{
get
{
return "Assets/HardcodedPath";
}
}
}
問題是該路徑是硬編碼的,因此 package 位置根本無法更改。 我計划更改RootPath
屬性,以便它獲取當前腳本位置並搜索具有給定模式的父文件夾。 這樣,即使在我自己的自定義 package 中(即在 Packages 文件夾而不是 Assets 文件夾中),我也可以使用這個 package。
缺少的部分是我找不到從代碼中獲取此 static class 文件夾位置的方法。 誰能幫我這個?
挖掘 AssetDatabase 並找到對您的腳本的引用。 這應該返回你想要的。
public static class EditorUtils
{
public static string RootPath
{
get
{
var g = AssetDatabase.FindAssets ( "t:Script EditorUtils" );
return AssetDatabase.GUIDToAssetPath ( g [ 0 ] );
}
}
}
這也適用於包中的腳本。 請記住將EditorUtils
更改為您的腳本名稱。
這是相關的 Unity 文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.