hololens的asstbundle打包与读写

发表于2017-12-24
评论0 2.1k浏览

 

hololensasstbundle打包与读写

 

一、遇到的问题

 

1. asstbundle打包文件丢失shader

 

2.hololens读取服务器下载的asstbundle文件

 

二、assetbundle打包文件丢失shader解决方法

1步,AssetsClient工程中Edit->Project Setting->GraphicsSettings设置需要的shader

 

 

2步,CodeClient中跟第一步一样操作一遍。

 

三、hololens读取服务器下载的asstbundle文件解决方法

 

hololens只支持Storage读写

 

1.注意await FileIO.WriteBytesAsync(prefab, b);

2.协同不能调用被异步掉用

3.初始化模型需要在主线程中执行

四、实现方法如下代码所示

 

using UnityEngine;

using System.Collections;

using System.IO;

using System;

using System.Xml;

using System.Collections.Generic;

using UnityEngine.SceneManagement;

using System.Text;

using Windows.Storage;

using System.Linq;

using System.Threading.Tasks;

using Windows.Storage.Streams;

using System.Runtime.InteropServices.WindowsRuntime;

 

public class DownLoadAssetBundle : MonoBehaviour

    AssetBundle myAssetBundle;

    private const string DownLoadPath = "http://192.168.50.66/hololens/air_model.assetbundle";

 

    StorageFolder localFolder = null;

 

    StorageFolder StreamingAssetsFolder = null;

 

    bool isDate = false;

 

    string filename = "air_model.assetbundle";

 

    byte[] bytes;

    void Start()

    {

        localFolder = ApplicationData.Current.LocalCacheFolder;

 

        StartCoroutine(DownLoadPrefab(DownLoadPath));

    }

 

    void Update()

    {

        if (isDate)

        {

            isDate = false;

            StartCoroutine(LoadPrefab());

        }

    }

 

    IEnumerator LoadPrefab()

    {

        myAssetBundle = AssetBundle.LoadFromMemory(bytes);

        yield return myAssetBundle;

 

        var request = myAssetBundle.LoadAllAssets(typeof(GameObject));

        yield return request;

 

        GameObject modle = request[0] as GameObject;

 

        //*********默认加载第一个*********测试所用

        GameObject obj = Instantiate(modle);

        obj.transform.localPosition = new Vector3(0, 0, 2);

        obj.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);

 

    }

    IEnumerator DownLoadPrefab(string url)

    {

        WWW w = new WWW(url);

 

        yield return w;

 

        if (w.isDone)

        {

            byte[] b = w.bytes;

            getAndWriteToSD(b);

        }

    }

    async void getAndWriteToSD(byte[] b)

    {

        try

        {

            StreamingAssetsFolder = await localFolder.GetFolderAsync("cache");

        }

        catch (Exception e)

        {

            StreamingAssetsFolder = await localFolder.CreateFolderAsync("cache");

        }

        StorageFile prefab = await StreamingAssetsFolder.CreateFileAsync(

            filename, CreationCollisionOption.ReplaceExisting);

 

        await FileIO.WriteBytesAsync(prefab, b);

 

        StorageFile prefabfind = await StreamingAssetsFolder.GetFileAsync(filename);

 

        if (prefabfind != null)

        {

            IBuffer fi = await FileIO.ReadBufferAsync(prefabfind);

            bytes = WindowsRuntimeBufferExtensions.ToArray(fi, 0, (int)fi.Length);

            isDate = true;

        }

 

    }

 

 

 

 

 

 

 

 

 

 

 

 

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引