专属撩妹开发之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 () {
       
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41995872/article/details/84107029
今日推荐