unity3d下載Obb分包檔案

星塵發表於2014-04-21

下載OBB外掛包

http://pan.baidu.com/s/1c0ouRZE

 

1、匯入外掛

注意事項:

如果專案中已經存在Android 外掛,需要merge匯入的xml檔案例如 AndroidManifest.xml和strings.xml檔案,否則編譯或執行的時候可能會報錯.

2、修改GooglePlayDownloader.cs 檔案中的key改成你的key.

3、勾選 Player Settings ->Publishing Settings中的split Application Binary.

4、編譯釋出

 

以下是結合Ngui進度條程式碼示例:

using UnityEngine;
using System.Collections;

public class DownLoadObb : MonoBehaviour {

    private string expPath;
    private string logtxt;
    private bool alreadyLogged = false;
    
    private bool downloadStarted = false;
    private string uri;
    //    private string mainPath;
    
    public GameObject downloadButton;

    public UILabel progress;
    
    public UISlider progressBar;
    
    void Awake(){
        
    }
    void Start(){
        if (!GooglePlayDownloader.RunningOnAndroid())
        {
        
            return;
        }
        
        expPath = GooglePlayDownloader.GetExpansionFilePath();
        if (expPath == null)
        {

            return ;
        }
        else
        {
            string mainPath = GooglePlayDownloader.GetMainOBBPath(expPath);
            //            string patchPath = GooglePlayDownloader.GetPatchOBBPath(expPath);
            if (mainPath == null ){
                downloadButton.SetActive(true);
                
            }
            StartCoroutine(loadLevel());
        }
    }
    
    public void DownLoad(){
        GooglePlayDownloader.FetchOBB();
    }

    void Update (){
        if(!www.isDone){
            progress.text = "Loading data file... "+(www.progress*100).ToString("f2")+"%";
            progressBar.sliderValue = www.progress;
        }
    }
    
    
    WWW www;
    IEnumerator loadLevel(){
        string mainPath;
        do{
            yield return new WaitForSeconds(0.5f);
            mainPath = GooglePlayDownloader.GetMainOBBPath(expPath); 
        }
        while(mainPath == null);
        
        progress.gameObject.SetActive(true);
        progressBar.gameObject.SetActive(true);
        downloadButton.SetActive(false);
    
        if(downloadStarted == false){
            downloadStarted = true;
            
            uri = "file://" + mainPath;
            
            www = WWW.LoadFromCacheOrDownload(uri,0);
            
            yield return www;
            
            if(www.error == null){
                Application.LoadLevel("Menu");
            }
        }
    }
}