4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Unity】Editor拡張でメニューからHLSLを作成できるようにした

Last updated at Posted at 2022-03-23

ソースコード



こんな感じで作れるようになります。

作ったわけ

image.png
HLSLをUnityで作るには右クリックしてShow in Explorer

image.png
テキストを作成して

image.png
名前を変更して拡張子をhlslに書き換えれば完成!
しかしこの一連の動作が面倒くさいのでEditor拡張して簡単に作れるようにしようというわけです。

解説

[MenuItem("Assets/Create/Shader/HLSLScript")]

image.png
image.png
これでUnityの上のメニューバーのAssetsボタンやProjectウィンドウで右クリックしたときにメニューを追加します。

path = AssetDatabase.GetAssetPath(Selection.activeInstanceID);

これで右クリックした場所のパスを取得します。

path = EditorUtility.SaveFilePanelInProject("CreatHLSLScript", "NewShader", "hlsl", "", path);

image.png
EditorUtility.SaveFilePanelInProject()を使うことでファイルを生成するウィンドウを出すことができます。
参考 : https://docs.unity3d.com/ja/current/ScriptReference/EditorUtility.SaveFilePanelInProject.html

File.WriteAllText(path,"");

File.WriteAllText()で新しいファイルを作成します。
参考 : https://docs.microsoft.com/ja-jp/dotnet/api/system.io.file.writealltext?view=net-6.0

AssetDatabase.Refresh();

最後にAssetDatabase.Refresh()で更新を行います。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?