0%

Unity Editor

获取当前选择的目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static string GetCurrentAssetDirectory()
{
foreach(var obj in Selection.GetFiltered<Object>(SelectionMode.Assets))
{
var path = AssetDataBase.GetAssetPath(obj);
if(string.IsNullOrEmpty(path))
{
return path;
}
else if(System.IO.File.Exists(path))
{
return System.IO.Path.GetDirectory(path);
}
}
return "Assets";
}

创建hlsl脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static class AssetsMenu
{
[MenuItem("Assets/Create HLSL")]
public static void CreateHLSL()
{
//string projectPath = Path.GetDirectoryName(Application.dataPath);

string assetPath = Path.Combine(GetCurrentAssetDirectory(), "temp.hlsl");
assetPath = AssetDataBase.generateuniqueAssetPath(assetPath);

AssetDataBase.CreateAsset(new TextAsset("--", assetPath);
File.WriteAllText(assetPath, ""); // clean

AssetDataBase.SaveAssets();
AssetDataBase.Refresh();
}
}