本文主要是介绍专属撩妹开发之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 initializationIEnumerator 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("版本跟最新版保持一致不需要更新");}}#endregionStopAllCoroutines();}// Update is called once per framevoid Update () {}
}
这篇关于专属撩妹开发之AssetsBundle更新游戏场景内容的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!