12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- //==========================================================
- //
- // Copyright (c) Guangzhou Shixiang Technology Co.,Ltd.
- // All rights reserved.
- //
- //==========================================================
- using System.IO;
- using UnityEditor;
- using UnityEngine;
- using UnityEditor.XR.Management.Metadata;
- namespace GxrSdk.Editor
- {
- /// <summary>
- /// (gxrsdk_android_plugin.aar)插件校验
- ///
- /// UnityPlayer的构造函数在Unity2022和之前的版本中是不兼容的,这样就导致了需要2份不同的
- /// gxrsdk_android_plugin.aar(2022和2021(2022之前的是一样的)),aar存放在GxrSdk/Plugins/Android/gxrsdk_android_plugin中,
- /// 每次打开工程时需要判断当前编辑器版本把对应的aar文件复制出来
- /// </summary>
- [InitializeOnLoad]
- public class GxrAndroidPluginVerify
- {
- /// <summary>
- /// 本地存放不同版本插件的路径
- /// </summary>
- private static readonly string GxrSdkAndroidPluginFolder = "Assets/GxrSdk/Sdk/Plugins/Android/gxrsdk_android_plugin/";
- /// <summary>
- /// 2022版本插件路径
- /// </summary>
- private static readonly string AndroidPluginPath2022 = GxrSdkAndroidPluginFolder + "gxrsdk_android_plugin-release.aar.2022";
- private static readonly string AndroidPluginPath2022XrSdk = GxrSdkAndroidPluginFolder + "gxrsdk_android_plugin-release.aar.2022.xrsdk";
- /// <summary>
- /// 2021版本插件路径
- /// </summary>
- private static readonly string AndroidPluginPath2021 = GxrSdkAndroidPluginFolder + "gxrsdk_android_plugin-release.aar.2021";
- private static readonly string AndroidPluginPath2021XrSdk = GxrSdkAndroidPluginFolder + "gxrsdk_android_plugin-release.aar.2021.xrsdk";
- /// <summary>
- /// 插件目标路径
- /// </summary>
- private static readonly string AndroidPluginDestPath = "Assets/GxrSdk/Sdk/Plugins/Android/gxrsdk_android_plugin-release.aar";
- static GxrAndroidPluginVerify()
- {
- EditorApplication.update += Update;
- }
- private static void Update()
- {
- EditorApplication.update -= Update;
- bool xrProviderToggled = IsGxrSdkXrProviderToggled();
- string srcPath = null;
- #if UNITY_2022_1_OR_NEWER
- srcPath = xrProviderToggled ? AndroidPluginPath2022XrSdk : AndroidPluginPath2022;
- #else
- srcPath = xrProviderToggled ? AndroidPluginPath2021XrSdk : AndroidPluginPath2021;
- #endif
- if (string.IsNullOrEmpty(srcPath))
- {
- Debug.LogError($"[GxrAndroidPluginVerify] The source path is null or empty.");
- return;
- }
- if (!File.Exists(srcPath))
- {
- Debug.Log($"[GxrAndroidPluginVerify] Cannot find {srcPath}.");
- return;
- }
- if (File.Exists(AndroidPluginDestPath))
- {
- File.Delete(AndroidPluginDestPath);
- }
- File.Copy(srcPath, AndroidPluginDestPath);
- }
- /// <summary>
- /// XR Plugin Management中是否勾选了GxrSdk xr plugin
- /// 勾选了就是要使用XrSdk模式,就得使用对应版本的gxrsdk_android_plugin.aar
- /// [已知问题]如果在修改了是否勾选后,没有其它修改也就没有重新触发这里进行重新检测,那么使用的aar就可能不是预期的版本。
- /// </summary>
- /// <returns></returns>
- private static bool IsGxrSdkXrProviderToggled()
- {
- // 正常的话应该使用typeof(GxrLoader).FullName去获取这个名字,
- // 暂时为了不引用插件模块,直接写名字在这里,注意名字要保持一致
- string loaderFullName = "Unity.XR.GXRSDK.GxrLoader";
- var toggled = XRPackageMetadataStore.IsLoaderAssigned(loaderFullName, BuildTargetGroup.Android);
- return toggled;
- }
- }
- }
|