うにてぃブログ

主にUnityとC#に関する記事を書いていきます

【Unity】 選択したアセットのパスを取得する

f:id:hacchi_man:20201001181257p:plain:w300

ProjectWindow で右クリックで出てくる CopyPath を選択するとそのアセットのパスを取得することができます

あとはどこかでペーストすれば クリップボードに保存されているアセットのパスがペーストされます

特に使わない気がしたのですが、フルパスを取得するにはどうするんだろうと思い作成してみました

using UnityEditor;
using UnityEngine;

public static class Utility
{
    [MenuItem("Assets/Copy Full Path")]
    public static void CopyAssetPath()
    {
        var guids = Selection.assetGUIDs;
        if (guids.Length <= 0)
            return;
 
        var fullPathEnv = Application.dataPath.Replace("Assets", AssetDatabase.GUIDToAssetPath(guids[0]));
        var fullPath = fullPathEnv.Replace('/', System.IO.Path.DirectorySeparatorChar);
        GUIUtility.systemCopyBuffer = fullPath;
    }
}