UnityのEditor C# スクリプトでパッケージ情報をパッケージネームから取得する方法。
Unity Package Managerでインストールされたパッケージ情報を取得します。
PackageInfoManager.cs
using System.Linq;
using UnityEngine;
using UnityEditor;
using UnityEditor.PackageManager;
public static class PackageInfoManager
{
[MenuItem("TEST / Package Info Test")]
public static void PackageInfoTest()
{
var PackageInfomation = GetPackageInfo("com.unity.inputsystem");
Debug.Log(PackageInfomation.displayName);
Debug.Log(PackageInfomation.version);
Debug.Log(PackageInfomation.name);
Debug.Log(PackageInfomation.packageId);
Debug.Log(PackageInfomation.source);
Debug.Log(PackageInfomation.resolvedPath);
}
/// <summary>
/// Get package information by package name
/// </summary>
public static UnityEditor.PackageManager.PackageInfo GetPackageInfo(string packageName)
{
var request = Client.List(true, true);
while (!request.IsCompleted) { }
if (request.Status == StatusCode.Success) { return request.Result.FirstOrDefault(pkg => pkg.name == packageName); }
return null;
}
}