After packaging WEBGL in Unity3D, read local file data + network request

PS: The blogger wrote in the previous article that webgl reads local file data and internal network requests, and I will add it in detail here

First of all, the error reported by the previous blogger after running the html: Insufficient memory! ! ! !
1. First check the size of the webgl package, which cannot exceed 2G.

2. F12 to view the specific error, the blogger here is:

       a: The configuration file in StreamingAssets is read locally, and the serialization fails.

       b: The network request method cannot use JsonConvert.SerializeObject to re-serialize the object. WebGL only supports Unity's own serialization, and does not support other external serialization methods.

PS: 1.webgl does not support IO

     2. webgl cannot directly use Unity's StreamingAssets reading method

PS: If the error reported by the small partner is as shown in the following article, don't worry, the blogger will explain the solution in detail below. (7 messages) WebGL Google prompts that the memory is not enough (RuntimeError: memory access out of bounds, Firefox prompts that the index is out of bounds) (RuntimeError: index out of bounds)

 The following takes the project as an example to describe in detail the reading of local file data + network requests after packaging WEBGL:

     1. Create a unity project

     2. Import the network method RestClient and video player AVPro into the project, and the blogger will attach the source code later;

     3. Create a configuration file, which can be txt json, etc. Here, the json used by bloggers,

4. Create a Canvas empty object, add the mediaplayer component, select the path "Path", "AbsolutePathOrURL" (ps: For details on playing the m3u8 video stream, please refer to the blogger's article Unity_Webgl: unity+webgl+m3u8+ read the configuration file in streamingAssets (gitee.com )

http://t.csdn.cn/OwKcGhttp://t.csdn.cn/OwKcG)

 5. Create the script DataBase.cs to deserialize the data of the configuration file,

PS: The blogger stepped on a thunder here, which is to use {get;set;} when declaring data variables, but {get;set;} cannot be used here, because "it seems to be in .net" (friends can search and search, but bloggers can't -_-;)

using System;

using UnityEngine;

[Serializable]
public class DataBase
{
    public override string ToString()
    {
        return JsonUtility.ToJson(this, true);
    }
}

/// <summary>
/// 数据壳
/// </summary>
[Serializable]
public class DataShells<T> : DataBase where T : DataBase
{
    public int code;
    public string msg;
    public T[] data;
}

/// <summary>
/// 数据壳
/// </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>
/// 数据壳
/// </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>
/// 配置文件数据类型
/// </summary>
[Serializable]
public class ConfigURLData : DataBase
{
    /// <summary>
    /// url
    /// </summary>
    public string url1;
    //public string url1{get;set;}//这个会报空,不建议使用
}

6. Now start to read the configuration file, and play the video stream in m3u8 format, upload the code directly, and hang the script at will, here I hang it directly on the camera

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. Source code link : Unity_Webgl: unity+webgl+m3u8+ Read the configuration file in streamingAssets Link: https://pan.baidu.com/s/1hCAWxyLbgL4iC4gWAxO7ZQ 
Extraction code: 1234 
--Share from Baidu Netdisk super member V5

PS: The compatibility of webgl is really bad! ! !

Follow the blogger's method, you will succeed, if not, you can send a private message to the blogger, and the blogger will reply when he sees it

Guess you like

Origin blog.csdn.net/weixin_39348384/article/details/131309877