Unity的WebGL平台动态加载glb/gltf测试
使用的是GLTFUtility https://github.com/Siccity/GLTFUtility.git
gltf样品库 https://github.com/KhronosGroup/glTF-Sample-Models.git
我的测试模型 https://www.xuefei.net.cn/unitywebgl/WaterBottle.glb
我的webgl测试地址 https://www.xuefei.net.cn/unitywebgl/index.html
主要代码
using System.Collections;
using UnityEngine;
using Siccity.GLTFUtility;
using UnityEngine.UI;
using UnityEngine.Networking;
public class GLTFTest : MonoBehaviour
{
public Button button;
public GameObject loadGo = null;
// Start is called before the first frame update
void Start()
{
button.onClick.AddListener(delegate
{
StartCoroutine(LoadGo());
});
}
private IEnumerator LoadGo()
{
if (loadGo != null)
{
DestroyImmediate(loadGo);
Resources.UnloadUnusedAssets();
}
var request = UnityWebRequest.Get("https://www.xuefei.net.cn/unitywebgl/WaterBottle.glb");
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
Debug.LogError(request.error);
}
else
{
Debug.Log(request.downloadHandler.text);
loadGo = Importer.LoadFromBytes(request.downloadHandler.data);
}
}
}
效果图