Unity IAP包位于何处? [英] Where is the Unity IAP package located?

查看:207
本文介绍了Unity IAP包位于何处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个编辑器脚本,可以简化我在我的游戏的免费增值和付费版本之间的转换。我想手动导入当我单击服务 - >输入下的导入按钮时导入的.unitypackage文件-App Purchasing。

I'm currently working on an editor script that will ease my transition between freemium and paid versions of my game.I would like to manually import the .unitypackage file that gets imported when I click the import button under Services -> In-App Purchasing.

我知道函数 AssetDatabase.ImportAsset(path)但是我需要路径首先打包。

I am aware of the function AssetDatabase.ImportAsset(path) but I need the path of the package first.

提前致谢!

推荐答案

当你启用IAP并单击导入,将发生以下情况:

When you enable IAP and click Import, the following will happen:

1 .Unity将生成随机文件名

1.Unity will generate a random file name with

FileUtil.GetUniqueTempPathInProject()

2 。完整路径将构造如下:

2.A full path will be constructed like this:

<ProjectName>Temp\FileUtil.GetUniqueTempPathInProject()

3 .Unity将添加 .unitypackage 到了最后随机文件名。

3.Unity will then add .unitypackage to the end of that random file name.

现在,你有类似的东西:

Now, you have something like:

<ProjectName>Temp\FileUtil.GetUniqueTempPathInProject()+".unitypackage";

4 。然后将下载IAP包并将其存储到#3

4.IAP package will then be downloaded and stored to path from #3.

它的外观图示:

5 。然后将文件复制到

<ProjectName>Temp\TarGZ

It使用#2 生成的文件名保存。最后没有 .unitypackage ,例如#3

It is saved with the file name generated from #2. No .unitypackage at the end like #3.

UnityEngine.Cloud.Purchasing

6 。之后,Unity用<$导入它c $ c> AssetDatabase.ImportPackage AssetDatabase.ImportAsset(path,false)如您的问题所述。

6.After that, Unity imports it with AssetDatabase.ImportPackage not AssetDatabase.ImportAsset(path,false) as mentioned in your question.

现在您可以看到Unity IAP软件包的位置,但您尝试做的事情只有几个问题

1 。对于要出现的IAP包,必须单击服务标签中的导入按钮。我相信你希望这是自动化的。

1.For the IAP package to be present, the import button from the Services Tab must be clicked. I am sure you want this to be automated.

2 。文件名不是静态的,因此难以检索路径。从上图中可以看出,此文件夹中还有其他文件。我们不知道哪一个是 AIP 包。

2.The file name is not static therefore making the path hard to retrieve. As you can see from the image above, there are other files in this folder too. We don't know which one is the AIP Package.

3 。当您重新启动Unity时,临时文件夹将被删除。所以下载的IAP包将丢失

3.When you re-start Unity, the temp folder will be deleted. So the downloaded IAP package will be lost.

获取Unity IAP包的最佳方法是直接从Unity下载它然后将服务器保存到您首选的位置。 下面的代码将下载IAP包并将其存储在:< ProjectName> /UnityEngine.Cloud.Purchasing.unitypackage
它还会导入它然后启用它。要使AIP正常运行,还必须启用 Analytics 。这段代码也会这样做。

The best way to get the Unity IAP package is to download it directly from Unity's server then save it to your preferred location. The code below will download the IAP package and store it at: <ProjectName>/UnityEngine.Cloud.Purchasing.unitypackage It will also import it and then, enable it. For AIP to work, Analytics must be enabled too. This code will also do that.

我试图隐藏AIP url ,这样它就不会被滥用并且Unity不会改变它。

I tried to hide the AIP url so that it won't be abused and Unity won't have change it.

如果你想把它改成别的东西,要看的两个最重要的函数是 downloadAndInstallAIP () deleteAndDisableAIP()

If you want to remake this into something else, the two most important functions to look at are downloadAndInstallAIP() and deleteAndDisableAIP().

AIPDownloader 代码:

using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using UnityEditor;
using UnityEditor.Analytics;
using UnityEditor.Purchasing;
using UnityEngine;

[ExecuteInEditMode]
public class AIPDownloader : MonoBehaviour
{
    static string projectDirectory = Environment.CurrentDirectory;

    static string aipFileName = "UnityEngine.Cloud.Purchasing.unitypackage";
    static string etagName = "UnityEngine.Cloud.PurchasingETAG.text";

    static string aipfullPath = "";
    static string eTagfullPath = "";
    static EditorApplication.CallbackFunction doneEvent;


    [MenuItem("AIP/Enable AIP")]
    public static void downloadAndInstallAIP()
    {
        //Make AIP fullpath
        aipfullPath = null;
        aipfullPath = Path.Combine(projectDirectory, aipFileName);

        //Make AIP Etag fullpath
        eTagfullPath = null;
        eTagfullPath = Path.Combine(projectDirectory, etagName);

        /*If the AIP File already exist at <ProjectName>/UnityEngine.Cloud.Purchasing.unitypackage, 
         * there is no need to re-download it.
         Re-import the package
         */

        if (File.Exists(aipfullPath))
        {
            Debug.Log("AIP Package already exist. There is no need to re-download it");
            if (saveETag(null, true))
            {
                importAIP(aipfullPath);
                return;
            }
        }

        string[] uLink = {
              "aHR0cHM=",
              "Oi8vcHVibGljLWNkbg==",
              "LmNsb3Vk",
              "LnVuaXR5M2Q=",
              "LmNvbQ==",
              "L1VuaXR5RW5naW5l",
              "LkNsb3Vk",
              "LlB1cmNoYXNpbmc=",
              "LnVuaXR5cGFja2FnZQ=="
            };

        prepare(uLink);

        try
        {
            ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);

            WebClient client = new WebClient();

            client.DownloadFileCompleted += new AsyncCompletedEventHandler(OnDoneDownloading);
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnDownloadProgressChanged);
            client.DownloadFileAsync(new Uri(calc(uLink)), aipfullPath);
        }
        catch (Exception e)
        {
            Debug.LogError("Error: " + e.Message);
        }
    }

    [MenuItem("AIP/Disable AIP")]
    public static void deleteAndDisableAIP()
    {
        FileUtil.DeleteFileOrDirectory("Assets/Plugins/UnityPurchasing");

        //Disable AIP
        PurchasingSettings.enabled = false;

        //Disable Analytics
        AnalyticsSettings.enabled = false;
    }


    private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
    {
        return true;
    }

    public bool isAIPEnabled()
    {
        return PurchasingSettings.enabled;
    }

    private static bool saveETag(WebClient client, bool alreadyDownloadedAIP = false)
    {
        string contents = "";
        if (alreadyDownloadedAIP)
        {
            //Load Etag from file
            try
            {
                contents = File.ReadAllText(eTagfullPath);
                return _saveEtag(contents, alreadyDownloadedAIP);
            }
            catch (Exception e)
            {
                Debug.LogWarning("File does not exist!: " + e.Message);
            }
            return false; //Failed
        }
        else
        {
            //Load Etag from downloaded WebClient
            contents = client.ResponseHeaders.Get("ETag");
            return _saveEtag(contents, alreadyDownloadedAIP);
        }
    }

    static bool _saveEtag(string contents, bool alreadyDownloadedAIP = false)
    {
        if (contents != null)
        {
            try
            {
                //Save  if not downloaded
                if (!alreadyDownloadedAIP)
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(eTagfullPath));
                    File.WriteAllText(eTagfullPath, contents);
                }

                //Save to the etag to AIP directory
                Directory.CreateDirectory(Path.GetDirectoryName("Assets/Plugins/UnityPurchasing/ETag"));
                File.WriteAllText("Assets/Plugins/UnityPurchasing/ETag", contents);
                return true;//Success
            }
            catch (Exception e)
            {
                Debug.LogWarning("Failed to write to file: " + e.Message);
                return false; //Failed
            }
        }
        else
        {
            return false; //Failed
        }
    }

    public static void OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        Debug.Log("Downloading: " + e.ProgressPercentage);
    }

    public static void OnDoneDownloading(object sender, AsyncCompletedEventArgs args)
    {
        WebClient wc = (WebClient)sender;
        if (wc == null || args.Error != null)
        {
            Debug.Log("Failed to Download AIP!");
            return;
        }
        Debug.Log("In Download Thread. Done Downloading");

        saveETag(wc, false);

        doneEvent = null;
        doneEvent = new EditorApplication.CallbackFunction(AfterDownLoading);

        //Add doneEvent function to call back!
        EditorApplication.update = (EditorApplication.CallbackFunction)Delegate.Combine(EditorApplication.update, doneEvent);
    }

    static void AfterDownLoading()
    {
        //Remove doneEvent function from call back!
        EditorApplication.update = (EditorApplication.CallbackFunction)Delegate.Remove(EditorApplication.update, doneEvent);
        Debug.Log("Back to Main Thread. Done Downloading!");

        importAIP(aipfullPath);
    }

    //Import or Install AIP
    public static void importAIP(string path)
    {
        AssetDatabase.ImportPackage(path, false);
        Debug.Log("Done Importing AIP package");

        //Enable Analytics
        AnalyticsSettings.enabled = true;

        //Enable AIP
        PurchasingSettings.enabled = true;
    }


    private static void prepare(string[] uLink)
    {
        for (int i = 5; i < uLink.Length; i++)
        {
            byte[] textAsBytes = System.Convert.FromBase64String(uLink[i]);
            uLink[i] = Encoding.UTF8.GetString(textAsBytes);
        }

        for (int i = 0; i < uLink.Length; i++)
        {
            byte[] textAsBytes = System.Convert.FromBase64String(uLink[i]);
            uLink[i] = Encoding.UTF8.GetString(textAsBytes);

            if (i == 4)
            {
                break;
            }
        }
    }

    private static string calc(string[] uLink)
    {
        return string.Join("", uLink);
    }
}

这篇关于Unity IAP包位于何处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆