NuGetForUnity用不了时的一个折衷方案
如果NuGetForUnity网络访问不了或者下载速度非常慢,导致无法正常使用,那可以试试下面的方法。
1) 先用vs的NuGet包管理器下载,vs下貌似没遇到网络问题,下载很快
注意,vs不是下载在Unity的Assets文件夹下的,而是和他同层级,所以此时Unity并不会加载所下载的dll文件的
2) 然后我们copy对应版本的dll文件到Unity的Assets文件夹下
菜单 -> File -> Build Settings -> Player Settings下查看用哪个版本
下面是一个自动拷贝的工具脚本,把要拷贝的dll写在配置文件里,然后执行下就自动copy了。
#if UNITY_EDITOR using System; using System.IO; using System.Text; using UnityEditor; using UnityEngine; public class NuGetTool { private const string Config_File_Path = "Assets/Editor/NuGetSync.txt"; private const string MenuItemPath_SyncExistDlls = "NuGet/Sync Dlls"; [MenuItem(MenuItemPath_SyncExistDlls)] private static void MenuItem_SyncExistDlls() { if (!File.Exists(Config_File_Path)) { Debug.LogWarning($"config file not exist: {Config_File_Path}"); return; } using (var fs = new StreamReader(Config_File_Path, Encoding.UTF8)) { var seperator = new string[] { ">>>" }; string line = null; while (null != (line = fs.ReadLine())) { if (string.IsNullOrEmpty(line) || line.StartsWith("#")) continue; var splitArr = line.Split(seperator, StringSplitOptions.RemoveEmptyEntries); var srcDir = splitArr[0]; var dstDir = splitArr[1]; SyncDlls(line, srcDir.Trim(), dstDir.Trim()); } } AssetDatabase.Refresh(); Debug.Log("finish !!!"); } private static void SyncDlls(string line, string srcDir, string dstDir) { if (!dstDir.StartsWith("Assets", StringComparison.OrdinalIgnoreCase)) { Debug.LogWarning($"dstDir: not in Assets/ folder, {line}"); return; } if (!Directory.Exists(srcDir)) { Debug.Log($"srcDir not exist: {line}"); return; } if (!Directory.Exists(dstDir)) Directory.CreateDirectory(dstDir); if (!Directory.Exists(dstDir)) { Debug.LogError($"dstDir create fail: {dstDir}"); return; } var filePaths = Directory.GetFiles(srcDir, "*.*", SearchOption.AllDirectories); foreach (var filePath in filePaths) { var dstFilePath = Path.Combine(dstDir, filePath); var parentDirPath = Path.GetDirectoryName(dstFilePath); if (!Directory.Exists(parentDirPath)) Directory.CreateDirectory(parentDirPath); if (!Directory.Exists(parentDirPath)) { Debug.LogError($"folder create fail: {parentDirPath}"); continue; } bool isCopy = true; if (File.Exists(dstFilePath)) { if (File.GetLastWriteTime(filePath) == File.GetLastWriteTime(dstFilePath)) { //Debug.Log($"no change: {dstFilePath}"); isCopy = false; } else { File.Delete(dstFilePath); } } if (isCopy) { File.Copy(filePath, dstFilePath); Debug.Log($"updated: {dstFilePath}"); } } } } #endif
配置文件格式,默认路径:Assets/Editor/NuGetSync.txt
# LitJson Packages/LitJson.0.19.0/lib/netstandard2.0 >>> Assets # NPOI Packages/NPOI.2.2.1/lib/net40 >>> Assets/Editor # EPPlus Packages/EPPlus.4.1.1/lib/net40 >>> Assets/Editor
执行后的效果
tips: Editor和逻辑代码都会用到,放在Assets/Packages下。只有Editor会用到,放在Assets/Editor/Packages下。
参考
在 Unity 中使用 .NET 4 和更高版本 | Microsoft Learn
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术