1. 程式人生 > >專屬撩妹開發之AssetsBundle更新遊戲場景內容

專屬撩妹開發之AssetsBundle更新遊戲場景內容

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
//using UnityEngine.Networking;
//如何快速建立一個測試資源Web伺服器及非同步獲取資源(Unity3D)
//https://www.cnblogs.com/IlidanStormRage/p/6102279.html
//有個小想法專屬妹子APP,如果想要直接更換APP裡面的內容。伺服器這邊之前更換AssetsBundle包就直接載入新場景
//新場景怎麼設計都可以
public class LoadAssetsBundles : MonoBehaviour {
    string path1;
    string path2 ;
    string path3;
    string path4;
    Texture texture;
    public static string version = "version1";
    void Start()
    {
        //需要在這裡賦值
        path1 = "Assets/AssetsBundles/object.unity3d";//包內容一個Cube和Sphere預設   包名+字尾
        path2 = "Assets/AssetsBundles/material.unity3d";//包內容一個材質球
        path3 = "Assets/AssetsBundles/texture.assetsbundle";//包內容一張名為timg的圖片
        path4 = "Assets/AssetsBundles/scene.unity3d";//包內容  一個場景
        StartCoroutine(StartAB());
      
    }

    // Use this for initialization
    IEnumerator StartAB () {

        //第一種方式從記憶體中載入AB包
        #region 非同步載入
        //非同步載入
        // AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path1));
        // yield return request;
        // //載入共同依賴資源,如貼圖  材質
        // AssetBundleCreateRequest request2 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path2));
        // yield return request2;

        // AssetBundleCreateRequest request3 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path3));
        // yield return request3;

        // AssetBundleCreateRequest request4 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path4));
        // yield return request4;

        // AssetBundle ab = request.assetBundle;//獲取包
        // AssetBundle ab2 = request2.assetBundle;//獲取包
        // AssetBundle ab3 = request3.assetBundle;
        // AssetBundle ab4 = request4.assetBundle;

        // //使用裡面的資源
        // GameObject cube = ab.LoadAsset<GameObject>("Cube");
        // GameObject sphere = ab.LoadAsset<GameObject>("Sphere");

        //  texture = ab3.LoadAsset<Texture>("timg");//獲取圖片

        // Debug.Log(texture.width + texture.height+texture.name);
        // //場景中新建一個RawImage
        // GameObject.Find("Canvas/RawImage").GetComponent<RawImage>().texture = texture;

        // //獲得場景包之後可以直接
        //SceneManager.LoadScene("AssetBundleScene");

        // Instantiate(cube);
        // Instantiate(sphere);
        #endregion


        //下載軟體 MyWebServer伺服器 https://www.cnblogs.com/IlidanStormRage/p/6102279.html
        #region 同步載入
        WWW www = new WWW("http://192.168.0.70/scene.unity3d");
        yield return www;
        WWW www1 = new WWW("http://192.168.0.70/1.jpg");
        yield return www1;
        WWW www2 = new WWW("http://192.168.0.70/version.txt");//版本號   注意txt的格式需要為ASCII格式
        yield return www2;
        //版本號的確認
        if (www2!=null)
        {
           
            //版本不一致才需要更新
            if (www2.text!= version)
            {
                if (www1 != null)
                {
                    //更新圖片
                    texture = www1.texture;
                    GameObject.Find("Canvas/RawImage").GetComponent<RawImage>().texture = texture;
                }
                else
                {
                    Debug.Log("666");
                }
                AssetBundle ab1 = www.assetBundle;
                if (ab1 != null)
                {
                    //載入新場景(初始場景)
                    SceneManager.LoadScene("AssetBundleScene");
                  
                }
                //記錄當前版本
                version = www2.text;
            }
            else
            {
                Debug.Log("版本跟最新版保持一致不需要更新");
            }
        }

        
      
        #endregion

        StopAllCoroutines();
    }

    // Update is called once per frame
    void Update () {
       
    }
}