GxrAndroidPluginVerify.cs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //==========================================================
  2. //
  3. // Copyright (c) Guangzhou Shixiang Technology Co.,Ltd.
  4. // All rights reserved.
  5. //
  6. //==========================================================
  7. using System.IO;
  8. using UnityEditor;
  9. using UnityEngine;
  10. using UnityEditor.XR.Management.Metadata;
  11. namespace GxrSdk.Editor
  12. {
  13. /// <summary>
  14. /// (gxrsdk_android_plugin.aar)插件校验
  15. ///
  16. /// UnityPlayer的构造函数在Unity2022和之前的版本中是不兼容的,这样就导致了需要2份不同的
  17. /// gxrsdk_android_plugin.aar(2022和2021(2022之前的是一样的)),aar存放在GxrSdk/Plugins/Android/gxrsdk_android_plugin中,
  18. /// 每次打开工程时需要判断当前编辑器版本把对应的aar文件复制出来
  19. /// </summary>
  20. [InitializeOnLoad]
  21. public class GxrAndroidPluginVerify
  22. {
  23. /// <summary>
  24. /// 本地存放不同版本插件的路径
  25. /// </summary>
  26. private static readonly string GxrSdkAndroidPluginFolder = "Assets/GxrSdk/Sdk/Plugins/Android/gxrsdk_android_plugin/";
  27. /// <summary>
  28. /// 2022版本插件路径
  29. /// </summary>
  30. private static readonly string AndroidPluginPath2022 = GxrSdkAndroidPluginFolder + "gxrsdk_android_plugin-release.aar.2022";
  31. private static readonly string AndroidPluginPath2022XrSdk = GxrSdkAndroidPluginFolder + "gxrsdk_android_plugin-release.aar.2022.xrsdk";
  32. /// <summary>
  33. /// 2021版本插件路径
  34. /// </summary>
  35. private static readonly string AndroidPluginPath2021 = GxrSdkAndroidPluginFolder + "gxrsdk_android_plugin-release.aar.2021";
  36. private static readonly string AndroidPluginPath2021XrSdk = GxrSdkAndroidPluginFolder + "gxrsdk_android_plugin-release.aar.2021.xrsdk";
  37. /// <summary>
  38. /// 插件目标路径
  39. /// </summary>
  40. private static readonly string AndroidPluginDestPath = "Assets/GxrSdk/Sdk/Plugins/Android/gxrsdk_android_plugin-release.aar";
  41. static GxrAndroidPluginVerify()
  42. {
  43. EditorApplication.update += Update;
  44. }
  45. private static void Update()
  46. {
  47. EditorApplication.update -= Update;
  48. bool xrProviderToggled = IsGxrSdkXrProviderToggled();
  49. string srcPath = null;
  50. #if UNITY_2022_1_OR_NEWER
  51. srcPath = xrProviderToggled ? AndroidPluginPath2022XrSdk : AndroidPluginPath2022;
  52. #else
  53. srcPath = xrProviderToggled ? AndroidPluginPath2021XrSdk : AndroidPluginPath2021;
  54. #endif
  55. if (string.IsNullOrEmpty(srcPath))
  56. {
  57. Debug.LogError($"[GxrAndroidPluginVerify] The source path is null or empty.");
  58. return;
  59. }
  60. if (!File.Exists(srcPath))
  61. {
  62. Debug.Log($"[GxrAndroidPluginVerify] Cannot find {srcPath}.");
  63. return;
  64. }
  65. if (File.Exists(AndroidPluginDestPath))
  66. {
  67. File.Delete(AndroidPluginDestPath);
  68. }
  69. File.Copy(srcPath, AndroidPluginDestPath);
  70. }
  71. /// <summary>
  72. /// XR Plugin Management中是否勾选了GxrSdk xr plugin
  73. /// 勾选了就是要使用XrSdk模式,就得使用对应版本的gxrsdk_android_plugin.aar
  74. /// [已知问题]如果在修改了是否勾选后,没有其它修改也就没有重新触发这里进行重新检测,那么使用的aar就可能不是预期的版本。
  75. /// </summary>
  76. /// <returns></returns>
  77. private static bool IsGxrSdkXrProviderToggled()
  78. {
  79. // 正常的话应该使用typeof(GxrLoader).FullName去获取这个名字,
  80. // 暂时为了不引用插件模块,直接写名字在这里,注意名字要保持一致
  81. string loaderFullName = "Unity.XR.GXRSDK.GxrLoader";
  82. var toggled = XRPackageMetadataStore.IsLoaderAssigned(loaderFullName, BuildTargetGroup.Android);
  83. return toggled;
  84. }
  85. }
  86. }