- Published on
Runtime import of VRM files in Unity Game
- Authors
- EvelynFull-time IndieDev.I'm Japanese, so please forgive me if my English is strange.
Hello! I'm Evelyn GameDev and full-time indie developer.
In such cases, it is recommended that VRM files be imported into the game.
In this article, we will implement a runtime import of VRM files in a Unity-made game.
I explain about this, in my youtube video. So if you have any unclear points in my blog post, please watch this video.
Table of Contents
Importing VRMRuntimeImporter
To make it easier for everyone to implement, I've created a package.
Please visit this GitHub link and download VRMRuntimeImporter.
VRMRuntimeImporterDownload the UnityPackage file from the release page and import the package into your project.
Demo Scene
Open the demo scene to see the runtime loading of your VRM file.
There is a VRMRuntimeImporter in the Prefab folder.
Customize for your project
Also, the function registered in Callback will be executed after the VRM file is loaded. You can accept the character's game object and vrm file path as arguments.
[System.Serializable]
public class LoadCallbackEvent : UnityEvent<GameObject, string> { }
I have prepared a sample script that reads VRM files, so please customize and use this script.
using UnityEngine;
using VRMRuntimeImporter;
public class Demo : MonoBehaviour
{
[SerializeField] VRMRuntimeImporter VRMRuntimeImporter;
private readonly string VRM_FILE_PATH_KEY = "VRM_FILE_PATH_KEY";
// This is a sample of loading the previously selected VRM file when the game is launched.
private void Awake()
{
if (!PlayerPrefs.HasKey(VRM_FILE_PATH_KEY)) return;
string path = PlayerPrefs.GetString(VRM_FILE_PATH_KEY);
if (System.IO.File.Exists(path))
{
VRMRuntimeImporter.LoadVrm(path);
}
}
// You can receive the game object and VRM path by registering it in the VRMRuntimeImporter's Callback.
public void HandleVrmGameObject(GameObject go, string vrmFilepath)
{
Debug.Log("HandleVrmGameObject");
Debug.Log(go);
Debug.Log(vrmFilepath);
PlayerPrefs.SetString(VRM_FILE_PATH_KEY, vrmFilepath);
PlayerPrefs.Save();
// TODO: Your game's own code here
}
// Using the UseVRM method of the VRMRuntimeImporter, the file browser will open and the VRM can be selected; if a VRM file is selected, the loading process will be executed.
public void OpenFileBrowser()
{
VRMRuntimeImporter.UseVRM();
}
}
Conclusion
As the metaverse becomes more popular, demand for the use of one's own avatar is growing.
Thanks for reading this article untile the end.
I hope your gamedev work will success.