GLTF介绍
简单来说就是 方便实时的外部加载模型 尤其用在webGL上 从服务器获取模型进行加载
官方介绍
下载
GLTFUtility插件下载
Newtonsoft.Json-for-Unity 下载 :插件需要此dll
使用
using Siccity.GLTFUtility;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 加载GLB模型单例
/// </summary>
public class GLTFUtilityScript
{
private static GLTFUtilityScript instance;
public static GLTFUtilityScript Instance
{
get
{
if (instance == null)
{
instance = new GLTFUtilityScript();
}
return instance;
}
}
Action action;
public GameObject obj;
bool isCreate = true;
/// <summary>
/// 同步加载 gltf and glb (不建议使用)
/// </summary>
/// <param name="filepath">路径</param>
/// <param name="action">回调函数</param>
public void ImportGLTF(string filepath, Action action)
{
UnloadAndDestroy(obj);
obj = Importer.LoadFromFile(filepath);
action = this.action;
}
/// <summary>
/// 异步加载 gltf and glb
/// </summary>
/// <param name="filepath">路径</param>
/// <param name="action">回调函数</param>
public void ImportGLTFAsync(string filepath, Action action)
{
if (!isCreate) return;
isCreate = false;
UnloadAndDestroy(obj);
Importer.LoadFromFileAsync(filepath, new ImportSettings(), OnFinishAsync);
action = this.action;
}
public void ImportGLTFAsync(string filepath)
{
if (!isCreate) return;
isCreate = false;
UnloadAndDestroy(obj);
Importer.LoadFromFileAsync(filepath, new ImportSettings(), OnFinishAsync);
}
public void ImportGLTAsync()
{
obj = new GameObject ();
}
/// <summary>
/// 加载完后的回调
/// </summary>
/// <param name="result">加载出来的物体</param>
/// <param name="clip"></param>
void OnFinishAsync(GameObject result, AnimationClip[] clip)
{
obj = result;
isCreate = true;
Debug.Log("Finished importing " + result.name);
if (action != null)
action();
}
/// <summary>
/// 删除卸载 所有加载过的模型 清除缓存
/// </summary>
public void UnloadAndDestroy(GameObject obj)
{
if (obj != null)
{
GameObject.Destroy(obj);
}
Resources.UnloadUnusedAssets();
}
///// <summary>
///// 删除卸载 所有加载过的模型 清除缓存
///// </summary>
//public void UnloadAndDestroyALL()
//{
// Resources.UnloadUnusedAssets();
//}
}
问题
unity 直接加载没问题 打包会加载不出来 报错:着色器
需要把插件里的shader 放入unity 的图形设置里