[Unity 3d] 怎么使用第三方工具下载 AssetStore 资源

在本文,笔者将教大家怎么使用第三方工具下载 AssetStore 的资源,适用到Unity 2019。

前言:

一直没怎么在 Assetstore下载大体量的资源,故而也没发现 Unity 这个只能使用Unity 中 下载资源有什么不好的。
最近公司买了Lisence 送了一个 11.3GB 的资源,就像下载看看,结果下载到 85% 就归零重新下载,然后下载到97%又归零。

[Unity 3d] 怎么使用第三方工具下载 AssetStore 资源_第1张图片

所以笔者就纠结了,要是能使用第三方下载工具下载就好啦。

发现 :

困惑中,发现了这篇文章 : 使用外部下载器下载 unity assetstore 资源 - ,嗯很酷!
但是没想到的是,从Unity 5.5 开始上述文章中发现的 DecryptFile 方法不见了。
于是笔者想:如果Unity 5.4 还能用AssetStore,那就表明这个解密算法肯定没变!
但是,把 5.3 版本的UnityEditor.dll 直接扔到Unity 工程中怕有啥类型重复的报错,所以把关键代码反编译给大家好了:

using UnityEditor;
using Babybus.Framework.ExtensionMethods;
using UnityEngine;
using System;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;

class DecryptUtility
{
    [MenuItem("Utility/DecryptFile")]
    static void DecryptFile()
    {
        var inputFile =Path.Combine( Application.dataPath , "../7140748a-ecf3-4635-938c-bb902e51d8b9");
        var key = "3f7f98ead6782cf120a86bbca6c1f19a08ca7c2717b6227cb7237df7cadb2ede89cc1676a2bbe679d4cbb8f89761c1c2";
        DecryptFile(inputFile, inputFile + ".unitypackage", key);
    }
    private static void HexStringToByteArray(string hex, byte[] array, int offset)
    {
        if (offset + array.Length * 2 > hex.Length)
        {
            throw new ArgumentException("Hex string too short");
        }
        for (int i = 0; i < array.Length; i++)
        {
            string s = hex.Substring(i * 2 + offset, 2);
            array[i] = byte.Parse(s, NumberStyles.HexNumber);
        }
    }

    public static void DecryptFile(string inputFile, string outputFile, string keyIV)
    {
        if (!File.Exists(inputFile))
        {
            Debug.Log("文件不存在");
        return;
        }

        byte[] array = new byte[32];
        byte[] array2 = new byte[16];
        HexStringToByteArray(keyIV, array, 0);
        HexStringToByteArray(keyIV, array2, 64);
        EditorUtility.DisplayProgressBar("Decrypting", "Decrypting package", 0f);
        FileStream fileStream = File.Open(inputFile, FileMode.Open);
        FileStream fileStream2 = File.Open(outputFile, FileMode.CreateNew);
        long length = fileStream.Length;
        long num = 0L;
        AesManaged aesManaged = new AesManaged();
        aesManaged.Key = array;
        aesManaged.IV = array2;
        CryptoStream cryptoStream = new CryptoStream(fileStream, aesManaged.CreateDecryptor(aesManaged.Key, aesManaged.IV), CryptoStreamMode.Read);
        try
        {
            byte[] array3 = new byte[40960];
            int num2;
            while ((num2 = cryptoStream.Read(array3, 0, array3.Length)) > 0)
            {
                fileStream2.Write(array3, 0, num2);
                num += (long)num2;
                if (EditorUtility.DisplayCancelableProgressBar("Decrypting", "Decrypting package", (float)num / (float)length))
                {
                    throw new Exception("User cancelled decryption");
                }
            }
        }
        finally
        {
            cryptoStream.Close();
            fileStream.Close();
            fileStream2.Close();
            EditorUtility.ClearProgressBar();
        }
    }
}

使用:

  1. 工程中创建 Editor 文件夹。
  2. 拷贝代码新建脚本到上述 Editor 文件夹中。
  3. 代码中配置下载好的文件路径和从那个json 中拿到的KEY 。
  4. 菜单栏 Utility → DecryptFile 开始解密。
  5. 解密OK的文件与源文件在同一目录下。

效果:

这个速度有点不可思议,不科学。


[Unity 3d] 怎么使用第三方工具下载 AssetStore 资源_第2张图片

结语:

  • 软件体验真的是一个很大的课题,下载体验不好还搞垄断就太不应该了。
  • 软件都是慢慢成长的,这么重要的 API 早期的 Unity 还不是可以直接反编译看到。
  • 锁君子不锁小人,像我们这么正直的人,安全的很。

你可能感兴趣的:([Unity 3d] 怎么使用第三方工具下载 AssetStore 资源)