Skip to content

Instantly share code, notes, and snippets.

@EsProgram
Created August 26, 2020 08:00
Show Gist options
  • Save EsProgram/77674bfa90107b12d28a666829fab367 to your computer and use it in GitHub Desktop.
Save EsProgram/77674bfa90107b12d28a666829fab367 to your computer and use it in GitHub Desktop.
プロジェクトビュー右クリックした時のメニューから好きな適当なアセットをコピーしてくるサンプル
using UnityEditor;
using UnityEngine;
using System.IO;
public class UnityCopyAssetMenuSample
{
[MenuItem("Assets/Create/好きなパス")]
private static void 作る()
{
// NOTE: プロジェクトビュー右クリックから、任意のアセットをコピー
var pathFrom = Path.Combine("Assets", "好きなアセット");
var pathTo = Path.Combine(GetSelectedProjectDirectoryPath(), "好きなアセットのCopy");
Debug.Log(pathFrom);
Debug.Log(pathTo);
AssetDatabase.CopyAsset(pathFrom, pathTo);
}
/// <summary>
/// プロジェクトウィンドウで表示してるディレクトリのパスを返す
/// </summary>
public static string GetSelectedProjectDirectoryPath()
{
string path = "Assets";
foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
{
path = AssetDatabase.GetAssetPath(obj);
if (!string.IsNullOrEmpty(path) && File.Exists(path))
{
path = Path.GetDirectoryName(path);
break;
}
}
return path;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment