Import VRM Model at Runtime
Mon, Apr 16, 2018Examples of importing the VRM model with the latest version can be found here.
The followings are the methods to import a VRM model at runtime in Unity:
Open VRM from a file path
var path="sample.vrm";
var go=VRM.VRMImporter.LoadFromPath(path);
Debug.LogFormat("loaded {0}", go.name);
Open VRM asynchronously from a file path
var path="sample.vrm";
VRMImporter.LoadVrmAsync(path, go => {
Debug.LogFormat("loaded {0}", go.name);
});
Open VRM from a byte array
var path="sample.vrm";
var bytes = File.ReadAllBytes(path);
var go=VRMImporter.LoadFromBytes(bytes);
Open VRM asynchronously from a byte array
VRMImporter.LoadVrmAsync(bytes, go => {
Debug.LogFormat("loaded {0}", go.name);
});
Get the information form VRM
#if UNITY_STANDALONE_WIN
var path = FileDialogForWindows.FileDialog("open VRM", ".vrm");
#else
var path = Application.dataPath + "/default.vrm";
#endif
if (string.IsNullOrEmpty(path))
{
return;
}
// Get a byte array
var bytes = File.ReadAllBytes(path);
var context = new VRMImporterContext();
// Get JSON in GLB format and parse it
context.ParseGlb(bytes);
// Get the meta
var meta = context.ReadMeta();
Debug.LogFormat("meta: title:{0}", meta.Title);
// You can access the entire parsed GLTF here
var vrm = context.GLTF;
// Convert the parsed JSON to the Scene Object
if (m_loadAsync)
{
// Run asynchronously
var now = Time.time;
VRMImporter.LoadVrmAsync(context, go=> {
var delta = Time.time - now;
Debug.LogFormat("LoadVrmAsync {0:0.0} seconds", delta);
OnLoaded(go);
});
}
else
{
// Run synchronously
VRMImporter.LoadFromBytes(context);
OnLoaded(context.Root);
}
Get the thumbnail (From v0.37)
A thumbnail texture can be created by passing arguments to ReadMeta.
var meta = context.ReadMeta(true); // Make a thumbnail texture
Texture2D thumbnail=meta.Thumbnail;