#unity/日常积累 #unity/代码缓存
假如只安装了安卓的组件,没安装ios的组件,直接导入安卓依赖库,会报错,手动优化方式:
![[img_v3_02ka_061a6f72-0b46-44ab-8088-5c65d3fb5chu.jpg]]
![[img_v3_02ka_220e4ace-e9f4-4967-b07d-51f318df5ehu.jpg]]
代码优化方式参考:
![[img_v3_02ka_c0698d1e-30c7-48e4-bab6-58bc51fe4dhu.jpg]]
最终实现,逻辑参考: 其中优化点思考: 1、DidReloadScripts这个标签,是不是过重,因为是每次编译代码,都会跑一遍这里 2、如果没有打开unity,是否就不会触发这里,另外是否不使用DidReloadScripts,而是在切换编译平台的时候,
using System.Reflection;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
public class ExternalDependencyManagerResolver
{
private static bool hasProcessed = false; //确保只执行一次
private static PluginImporter pluginImporter;
[DidReloadScripts]
private static void OnScriptsReloaded()
{
if (hasProcessed) return;
hasProcessed = true;
string IOSResolverPath = "Assets/A_PLink_Public/Scripts/Tools/ExternalDependencyManager/Editor/1.2.182/Google.IOSResolver.dll";
string JarResolverPath = "Assets/A_PLink_Public/Scripts/Tools/ExternalDependencyManager/Editor/1.2.182/Google.JarResolver.dll";
if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS)
{
SetPluginImporter(IOSResolverPath, true);
SetPluginImporter(JarResolverPath, false);
}
else if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
{
SetPluginImporter(IOSResolverPath, false);
SetPluginImporter(JarResolverPath, true);
}
else
{
SetPluginImporter(IOSResolverPath, false);
SetPluginImporter(JarResolverPath, false);
}
}
private static void SetPluginImporter(string path,bool isSet)
{
pluginImporter = AssetImporter.GetAtPath(path) as PluginImporter;
if (pluginImporter != null)
{
var type = typeof(PluginImporter);
var property = type.GetProperty("ValidateReferences", BindingFlags.NonPublic | BindingFlags.Instance);
if (property != null)
{
// AssetDatabase.DisallowAutoRefresh();
property.SetValue(pluginImporter, isSet);
// pluginImporter.SaveAndReimport();
// AssetDatabase.AllowAutoRefresh();
Debug.Log($"{path}设置完成");
}
else
{
Debug.LogError("无法找到 ValidateReferences 属性");
}
Debug.Log("插件导入设置已更新");
}
else
{
Debug.LogError("未找到插件Importer");
}
}
}