2.9 KiB
2.9 KiB
#unity/日常积累 #unity/代码缓存
假如只安装了安卓的组件,没安装ios的组件,直接导入安卓依赖库,会报错,手动优化方式:
代码优化方式参考:
最终实现,逻辑参考: 其中优化点思考: 1、DidReloadScripts这个标签,是不是过重,因为是每次编译代码,都会跑一遍这里 2、如果没有打开unity,是否就不会触发这里,另外是否不使用DidReloadScripts,而是在切换编译平台的时候,
ExternalDependencyManagerResolver
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");
}
}
}