2017-01-01 96 views
1

我目前正在編寫一個編輯器腳本,它將簡化我的遊戲免費增值和付費版本之間的轉換。我想手動導入當我單擊導入按鈕時導入的.unitypackage文件,應用程序採購。Unity IAP包位於何處?

我知道函數AssetDatabase.ImportAsset(path),但我需要首先包的路徑。

在此先感謝!

+0

你還需要這方面的幫助?當然是 – Programmer

+0

。我還沒有找到這個難題的答案。 –

+0

在一小時內回覆。我有一個答案,這是一個有趣的東西。你只想訪問AIP包?這是一個編輯器的東西? – Programmer

回答

1

當您啓用IAP並單擊導入,將發生以下情況:

.Unity將生成

FileUtil.GetUniqueTempPathInProject() 

。一個完整路徑一個隨機文件名會建造像這樣:

<ProjectName>Temp\FileUtil.GetUniqueTempPathInProject() 

.Unity然後將.unitypackage添加到該隨機文件名的末尾。

現在,你有這樣的:那麼

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

.IAP包將被下載並從#3存儲到路徑。

插圖是什麼樣子:

enter image description here

.The文件,然後複製到

<ProjectName>Temp\TarGZ 

它保存與#生成的文件名2 。沒有.unitypackage在最後像#3

UnityEngine.Cloud.Purchasing 

。之後,統一與AssetDatabase.ImportPackageAssetDatabase.ImportAsset(path,false)進口它在你的問題中提到。

現在你可以看到統一IAP包所在,但也有在你正在嘗試做只是幾個問題:

。對於IAP包存在,從導入按鈕服務選項卡必須單擊。我相信你希望這是自動化的。

。文件名不是靜態的,因此使路徑難以檢索。正如你從上面的圖片中看到的,這個文件夾中還有其他文件。我們不知道哪一個是AIP包。

。當你重新啓動統一,臨時文件夾都將被刪除。所以下載的IAP包將是丟失

獲得Unity IAP包的最好方法是直接從Unity的服務器下載它,然後將它保存到您的首選位置。下面的代碼將下載IAP包並將它存儲在:<ProjectName>/UnityEngine.Cloud.Purchasing.unitypackage 它也會導入它,然後啓用它。要使AIP正常工作,必須啓用Analytics(分析)。這段代碼也會這樣做。

我試圖隱藏AIP url,以便它不會被濫用,並且Unity不會改變它。

如果您想將其重新制作成別的東西,最重要的兩個功能是看看downloadAndInstallAIP()deleteAndDisableAIP()

enter image description here

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); 
    } 
} 
+0

我只想說非常感謝你!最後有人爲此得到了答案。 –

+0

不客氣。很高興你覺得它有幫助。 – Programmer