Skip to content

Instantly share code, notes, and snippets.

@todorok1
Last active January 20, 2021 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todorok1/83cf55464c3c027bec45c47bcf52e52a to your computer and use it in GitHub Desktop.
Save todorok1/83cf55464c3c027bec45c47bcf52e52a to your computer and use it in GitHub Desktop.
/// <Summary>
/// 結合したメッシュをアセットとして保存します。
/// 保存先は現在開いているシーン名のフォルダです。
/// </Summary>
void SaveMeshAsAsset(Mesh saveMesh)
{
// 現在開いているシーンのパスを取得します。
string scenePath = EditorSceneManager.GetActiveScene().path;
// フォルダの存在を確認します。
string folderPath = scenePath.Replace(".unity", "");
bool existFolder = AssetDatabase.IsValidFolder(folderPath);
// フォルダがない場合は作成します。
if (!existFolder)
{
string sceneName = EditorSceneManager.GetActiveScene().name;
string parentFolder = folderPath.Substring(0, folderPath.LastIndexOf(sceneName) - 1);
string folderName = sceneName;
AssetDatabase.CreateFolder(parentFolder, folderName);
}
// メッシュを保存します。
string meshPath = $"{folderPath}/{saveMesh.name}.asset";
var asset = (Mesh)AssetDatabase.LoadAssetAtPath(meshPath, typeof(Mesh));
if (asset == null)
{
AssetDatabase.CreateAsset(saveMesh, meshPath);
}
else
{
EditorUtility.CopySerialized(saveMesh, asset);
AssetDatabase.SaveAssets();
}
AssetDatabase.Refresh();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment