PS:博主在上編文章中寫道webgl讀取本地文件數(shù)據(jù)以及內部網(wǎng)絡請求,在這里詳細補充一下
首先上一編博主運行html之后報的錯誤:提示內存不足?。。。?br> 1.首先排查一下webgl包的大小,不能超過2G。
2.F12查看具體錯誤,在這里博主的是:
? ? ? ?a:本地讀取StreamingAssets里的配置文件,序列化失敗。
? ? ? ?b:網(wǎng)絡請求方法不能使用JsonConvert.SerializeObject將對象重新序列化,WebGL僅僅支持Unity自身的序列化,不支持外部的其他序列化方式。
PS:1.webgl不支持IO
? ? ?2.webgl不能直接使用unity的StreamingAssets讀取方式
PS:如果小伙伴報的錯誤如下文章中所示,別急,博主下方會詳細說一下解決方案。(7條消息) WebGL谷歌提示內存不夠(RuntimeError:memory access out of bounds,火狐提示索引超出界限(RuntimeError:index out of bounds)_先生沉默先的博客-CSDN博客
?下面以項目為例,詳細講述打包WEBGL后讀取本地文件數(shù)據(jù)+網(wǎng)絡請求:
? ? ?1.創(chuàng)建unity項目
? ? ?2.將網(wǎng)絡方法RestClient和視頻播放器AVPro導入項目,后面博主會附上源碼;
? ? ?3.創(chuàng)建配置文件,可以是txt? ?json等,在這里博主使用的json,
4.創(chuàng)建Canvas空物體,添加mediaplayer組件,路徑選擇“Path”,“AbsolutePathOrURL”(ps:詳細播放m3u8視頻流請參考博主這邊文章Unity_Webgl: unity+webgl+m3u8+讀取streamingAssets里的配置文件 (gitee.com)
http://t.csdn.cn/OwKcGhttp://t.csdn.cn/OwKcG)
?5.創(chuàng)建腳本DataBase.cs用來反序列化配置文件的數(shù)據(jù),
PS:在這里博主踩了一個雷,就是聲明數(shù)據(jù)變量時用{get;set;}但是{get;set;}在這里不能使用,因為“貌似是.net里面的”(小伙伴可以搜搜查查,奈何博主不會啊-_-;)
using System;
using UnityEngine;
[Serializable]
public class DataBase
{
public override string ToString()
{
return JsonUtility.ToJson(this, true);
}
}
/// <summary>
/// 數(shù)據(jù)殼
/// </summary>
[Serializable]
public class DataShells<T> : DataBase where T : DataBase
{
public int code;
public string msg;
public T[] data;
}
/// <summary>
/// 數(shù)據(jù)殼
/// </summary>
/// <typeparam name="T"></typeparam>
[Serializable]
public class DataShell<T> : DataBase where T : DataBase
{
public T data;
public int errorcode;
public string msg;
public int ret;
}
/// <summary>
/// 數(shù)據(jù)殼
/// </summary>
/// <typeparam name="T"></typeparam>
[Serializable]
public class DatasShell<T> : DataBase where T : DataBase
{
public T[] data;
public int errorcode;
public string msg;
public int ret;
}
/// <summary>
/// 配置文件數(shù)據(jù)類型
/// </summary>
[Serializable]
public class ConfigURLData : DataBase
{
/// <summary>
/// url
/// </summary>
public string url1;
//public string url1{get;set;}//這個會報空,不建議使用
}
6.現(xiàn)在開始讀取配置文件,并播放m3u8格式的視頻流,直接上代碼,腳本隨意掛,在這里我直接掛在相機上了
using System.IO;
using Proyecto26;
using RenderHeads.Media.AVProVideo;
using UnityEngine;
public class MainManager : MonoBehaviour
{
public MediaPlayer player;
void Awake()
{
string path = Path.Combine(Application.streamingAssetsPath, "configURL.json");
RestClient.Request(new RequestHelper
{
Method = "GET",
Uri = path,
}).Then(mresponse =>
{
DataShell<ConfigURLData> configURL = JsonUtility.FromJson<DataShell<ConfigURLData>>(mresponse.Text);
player.OpenMedia(MediaPathType.AbsolutePathOrURL, configURL.data.url1);
}).Catch(err => { Debug.LogError("--------------" + err.Message); });
}
}
7.源碼鏈接:Unity_Webgl: unity+webgl+m3u8+讀取streamingAssets里的配置文件鏈接:https://pan.baidu.com/s/1hCAWxyLbgL4iC4gWAxO7ZQ?
提取碼:1234?
--來自百度網(wǎng)盤超級會員V5的分享
PS:webgl的兼容真的很坑?。。?mark hidden color="red">文章來源:http://www.zghlxwxcb.cn/news/detail-721305.html
按照博主的方法做,你一定會成功,不會的可以給博主私信,博主看到會回復文章來源地址http://www.zghlxwxcb.cn/news/detail-721305.html
到了這里,關于Unity3D中打包WEBGL后讀取本地文件數(shù)據(jù)+網(wǎng)絡請求的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!