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

Unity .NET 4.x 相关知识-CSDN博客

 

posted @ 2024-02-07 22:53  yanghui01  阅读(102)  评论(0编辑  收藏  举报