• Unity代码通过Package Manager导入包的方法


    今天在做一个项目,项目中依赖于很多其他的Package,其他的Package又是通过tgz包提供的,没有放到中心仓库,要是通过Package Manager界面导入需要一个一个点击加载,费时又费力,那我就写了一个脚本,点击import package,就会扫描Package tgz目录,把所有的Package加载进来,这个方法无法做到依赖包关系,需要你自己多加载几次。

    代码如下:

    1. using System.Collections.Generic;
    2. using System.IO;
    3. using UnityEditor;
    4. using UnityEditor.PackageManager;
    5. using UnityEditor.PackageManager.Requests;
    6. using UnityEngine;
    7. namespace XXXX.MetaSpace
    8. {
    9. internal static class MenuItems
    10. {
    11. static AddRequest request;
    12. static ListRequest listRequest;
    13. static List<string> packageNames;
    14. static bool isImporting = false;
    15. [MenuItem("MetaSpace/Import Package")]
    16. static void ImportPackages()
    17. {
    18. if (isImporting)
    19. {
    20. Debug.LogWarning("All packages are being imported.");
    21. return;
    22. }
    23. isImporting = true;
    24. PackagesOk();
    25. }
    26. static List<string> GetLocalPackages()
    27. {
    28. List<string> packages = new List<string>();
    29. string[] allFilePaths = Directory.GetFiles("Packages/com.xxxx.metaspace/Runtime/ThirdParty");
    30. foreach (var one in allFilePaths)
    31. {
    32. if (one.EndsWith("tgz"))
    33. {
    34. string pn = one.Replace("Packages/", "");
    35. string packageName = "file:" + pn;
    36. Debug.Log("packageName: " + packageName);
    37. packages.Add(packageName);
    38. }
    39. }
    40. return packages;
    41. }
    42. static void PackagesOk()
    43. {
    44. Debug.Log("Start List Packages...");
    45. listRequest = Client.List(false);
    46. EditorApplication.update += ListPackages;
    47. }
    48. static void StartInstallPackages()
    49. {
    50. EditorApplication.update += Progress;
    51. EditorApplication.update += AddPackage;
    52. }
    53. static void AddPackage()
    54. {
    55. if (request == null && (packageNames == null || packageNames.Count <= 0))
    56. {
    57. EditorApplication.update -= Progress;
    58. EditorApplication.update -= AddPackage;
    59. //isImporting = false;
    60. PackagesOk();
    61. return;
    62. }
    63. if (request == null && packageNames.Count > 0)
    64. {
    65. request = Client.Add(packageNames[0]);
    66. packageNames.RemoveAt(0);
    67. }
    68. }
    69. static void Progress()
    70. {
    71. if (request != null && request.IsCompleted)
    72. {
    73. if (request.Status == StatusCode.Success)
    74. Debug.Log("Installed: " + request.Result.packageId);
    75. else if (request.Status >= StatusCode.Failure)
    76. Debug.Log(request.Error.message);
    77. request = null;
    78. }
    79. }
    80. static void ListPackages()
    81. {
    82. if (listRequest.IsCompleted)
    83. {
    84. if (listRequest.Status == StatusCode.Success)
    85. {
    86. List<string> localPackages = GetLocalPackages();
    87. packageNames = new List<string>();
    88. foreach (var localPackage in localPackages)
    89. {
    90. string[] splitPaths = localPackage.Split('/');
    91. if (splitPaths.Length <= 0)
    92. continue;
    93. bool isFind = false;
    94. foreach (var package in listRequest.Result)
    95. {
    96. if (package.packageId.Contains(splitPaths[splitPaths.Length - 1]))
    97. {
    98. isFind = true;
    99. break;
    100. }
    101. }
    102. if (!isFind)
    103. {
    104. Debug.Log($"package: {localPackage} need to install!");
    105. packageNames.Add(localPackage);
    106. }
    107. }
    108. if (packageNames.Count > 0)
    109. StartInstallPackages();
    110. else
    111. {
    112. Debug.Log("all packages are installed!");
    113. isImporting = false;
    114. }
    115. }
    116. else if (listRequest.Status >= StatusCode.Failure)
    117. Debug.Log(listRequest.Error.message);
    118. EditorApplication.update -= ListPackages;
    119. listRequest = null;
    120. }
    121. }
    122. }
    123. }

  • 相关阅读:
    Day4:写前端项目(html+css+js)
    隐私保护协议
    第2-1-2章 传统方式安装FastDFS-附FastDFS常用命令
    基于JavaScript粒子流动效果
    单调栈-42. 接雨水
    112. 路径总和
    API接口中大数据在淘宝中的应用【获取商品详情接口,商品价格,SKU数据,订单……】
    PHP + Laravel + RabbitMQ + Redis 实现消息队列 (二) 消费队列在RabbitMQ和redis中的简单使用
    RobotFramework框架之导入自己打包的python程序(十五)
    文件上传—WAF拦截的绕过方式
  • 原文地址:https://blog.csdn.net/grace_yi/article/details/125898723