LoginSignup
1
0

More than 3 years have passed since last update.

OculusQuestのランタイムロード

Last updated at Posted at 2020-03-07

はじめに

OculusQuestでランタイム時に端末からVRMファイルをロードし、動的に生成する方法を記述します。

環境など(開発環境、実行環境)

コード

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRM;

public class LoadFile : MonoBehaviour
{
    GameObject VRM;
    string AndroidFilePath;
    string VRMDirectory = "/VRM/";
    string filename = "AliciaSolid.vrm";

    void Start()
    {
        AndroidFilePath = Application.persistentDataPath + VRMDirectory + filename;
        //Application.persistentDataPath = 
        //storage/emulated/0/Android/data/AppPackageName/files
        //AndroidFilePath =
        //storage/emulated/0/Android/data/AppPackageName/files/VRM/AliciaSolid.vrm

        var context = new VRMImporterContext();
        context.Parse(AndroidFilePath);
        context.LoadAsync(() =>
        {
            VRM = context.Root;
            context.ShowMeshes();
        });
    }
}

解説

AndroidはStreamingAssets(Application.streamingAssetPathで取得できる)からファイルをランタイムロードできないので、Androidの永続ファイルを格納するパスであるApplication.persistentDataPathに、事前にVRMファイルを入れておいて、ランタイムロードを実現しています。
コード中にもコメントで記載していますが、今回は、storage/emulated/0/Android/data/AppPackageName/files以下にVRMというディレクトリを作成し、ニコニ立体さんを入れて読み込んでいます。

余談というかメモのようなもの

  • OculusQuestに任意のファイルを入れる方法は、以下のコマンドでできます。

adb push 任意のファイル名 /storage/emulated/0/Android/data/PackageName/files/

任意のファイル名は、拡張子を含むファイル名で、実行するディレクトリからの相対パスで指定します。
PackageNameは、対象のアプリのPackageNameです。(com.Company.AppName的な)

  • DirectoryInfoで指定したディレクトリのファイル一覧を取得して選択することも可能です。
  • Application.persistentDataPath以外に入れたディレクトリはWebRequestで一旦Application.persistentDataPathに読み込んで取得する方法があるみたいです。今度試してみます。

参考記事

VRMファイルのゲーム中での生成は上記記事を参考にしました。以前のバージョンだと生成のコードがだいぶ変わっていてうまくできなかったので、ここに明記しておきます。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0