obsidian/笔记文件/2.笔记/安卓依赖库 导入 后处理.md

83 lines
2.9 KiB
Markdown
Raw Permalink Normal View History

2025-04-03 18:45:21 +08:00
#unity/日常积累
2025-04-04 11:47:20 +08:00
#unity/代码缓存
2025-04-03 18:45:21 +08:00
假如只安装了安卓的组件没安装ios的组件直接导入安卓依赖库会报错手动优化方式
![[img_v3_02ka_061a6f72-0b46-44ab-8088-5c65d3fb5chu.jpg]]
![[img_v3_02ka_220e4ace-e9f4-4967-b07d-51f318df5ehu.jpg]]
代码优化方式参考:
2025-04-04 11:47:20 +08:00
![[img_v3_02ka_c0698d1e-30c7-48e4-bab6-58bc51fe4dhu.jpg]]
最终实现,逻辑参考:
2025-04-09 19:06:25 +08:00
其中优化点思考:
1、DidReloadScripts这个标签是不是过重因为是每次编译代码都会跑一遍这里
2、如果没有打开unity是否就不会触发这里另外是否不使用DidReloadScripts而是在切换编译平台的时候
2025-04-04 11:47:20 +08:00
## ExternalDependencyManagerResolver
``` cs
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");
}
}
}
```