Appearance
Quest3サンプルアプリ動作確認
Unityバージョン:2022.3.53f1
ダウンロードアセット:
Oculus Integration (Deprecated)
参考サイト:
ハンドトラッキング・パススルーの実装はこちらのサイトを参考:
【Meta Quest3】UnityでARお絵かきアプリの作り方を解説! |
ハンドトラッキングで立方体に触れると、触れた立方体によってテキストが変わるアプリ
使用したjsonデータ
json
{
"childObjectDataList": [
{ "name": "cube1", "comment": "Touching cube1" },
{ "name": "cube2", "comment": "Touching cube2" },
{ "name": "cube3", "comment": "Touching cube3" },
{ "name": "cube4", "comment": "Touching cube4" }
]
}上記のjsonデータをQuest3本体内に保存し、アプリから読み込む。
JsonReader.cs(デバッグ用のコードも含む)
csharp
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using TMPro;
[System.Serializable]
public class ChildObjectData
{
public string name;
public string comment;
}
[System.Serializable]
public class DataList
{
public List<ChildObjectData> childObjectDataList;
}
public class JsonReader : MonoBehaviour
{
public TextMeshPro centerText; // 中心テキスト
private string filePath = Path.Combine(Application.persistentDataPath, "testData.json"); // JSONファイルのパス
private DataList dataList;
void Start()
{
// 期待するファイルパスを確認
Debug.Log($"Expected File Path: {filePath}");
centerText.text = $"Expected Path: {filePath}";
// 読み込み権限のリクエスト
if (!UnityEngine.Android.Permission.HasUserAuthorizedPermission(UnityEngine.Android.Permission.ExternalStorageRead))
{
UnityEngine.Android.Permission.RequestUserPermission(
UnityEngine.Android.Permission.ExternalStorageRead
);
}
else
{
Debug.Log("Read Permission Granted");
LoadJson();
}
}
// ユーザー応答後の処理
void OnPermissionResult(bool granted)
{
if (granted)
{
Debug.Log("Read Permission Granted by user");
LoadJson();
}
else
{
Debug.LogError("Read Permission Denied by user");
centerText.text = "Permission Denied";
}
}
// JSONファイルの読み込み
void LoadJson()
{
centerText.text = "Checking file existence...";
if (File.Exists(filePath))
{
centerText.text = "File found. Loading...";
try
{
string json = File.ReadAllText(filePath);
Debug.Log($"JSON Content: {json}");
centerText.text = $"JSON Content:\n{json}"; // JSON内容をUIに表示
dataList = JsonUtility.FromJson<DataList>(json);
centerText.text = "JSON Loaded Successfully";
}
catch (IOException ex)
{
Debug.LogError($"IO Exception: {ex.Message}");
centerText.text = "Failed to read JSON file";
}
catch (System.Exception ex)
{
Debug.LogError($"Unexpected Exception: {ex.Message}");
centerText.text = $"Unexpected error: {ex.Message}";
}
}
else
{
Debug.LogError($"File not found at: {filePath}");
centerText.text = $"File not found at: {filePath}";
}
}
// 名前からコメントを取得
public string GetCommentByName(string objectName)
{
if (dataList == null || dataList.childObjectDataList == null)
return null;
foreach (var child in dataList.childObjectDataList)
{
if (child.name == objectName)
return child.comment;
}
return null;
}
}CubeInteractionJson.cs
csharp
using UnityEngine;
using TMPro;
public class CubeInteractionJson : MonoBehaviour
{
public TextMeshPro centerText; // 中心テキスト
public JsonReader jsonReader; // JsonReaderコンポーネント
private Renderer cubeRenderer; // キューブのRendererコンポーネント
private Color originalColor; // 元の色を保持する変数
void Start()
{
// Rendererを取得して元の色を保存
cubeRenderer = GetComponent<Renderer>();
if (cubeRenderer != null)
{
originalColor = cubeRenderer.material.color;
}
else
{
Debug.LogError($"Renderer not found on {gameObject.name}");
}
}
private void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.name == "Hand_Index2_CapsuleCollider")
{
// コメントを取得してテキストを更新
string comment = jsonReader.GetCommentByName(gameObject.name);
if (!string.IsNullOrEmpty(comment))
{
centerText.text = comment;
}
else
{
centerText.text=$"Comment not found for: {gameObject.name}";
}
// キューブの色を青色に変更
if (cubeRenderer != null)
{
cubeRenderer.material.color = Color.blue;
}
}
}
private void OnTriggerExit(Collider collider)
{
if (collider.gameObject.name == "Hand_Index2_CapsuleCollider")
{
// 手が離れたときに元の色に戻す
if (cubeRenderer != null)
{
cubeRenderer.material.color = originalColor;
}
}
}
}Author: 水上 | Source:
水上\Quest3サンプルアプリ動作確認 3cb209682acc4d249b45524b0a504812.md