using System; using System.Collections.Generic; using Oculus.Avatar; using UnityEngine; public class OvrAvatarSDKManager : MonoBehaviour { private static OvrAvatarSDKManager _instance; private Dictionary> specificationCallbacks; private Dictionary> assetLoadedCallbacks; private Dictionary combinedMeshLoadedCallbacks; private Dictionary assetCache; public static OvrAvatarSDKManager Instance { get { if (_instance == null) { _instance = UnityEngine.Object.FindObjectOfType(); if (_instance == null) { GameObject gameObject = new GameObject("OvrAvatarSDKManager"); _instance = gameObject.AddComponent(); _instance.Initialize(); } } return _instance; } } private void Initialize() { string text = GetAppId(); if (text == string.Empty) { text = "0"; } // CAPI.ovrAvatar_Initialize(text); // CAPI.SendEvent("initialize", text, string.Empty); specificationCallbacks = new Dictionary>(); assetLoadedCallbacks = new Dictionary>(); combinedMeshLoadedCallbacks = new Dictionary(); assetCache = new Dictionary(); } private void OnDestroy() { // CAPI.ovrAvatar_Shutdown(); } private void Update() { // IntPtr intPtr = CAPI.ovrAvatarMessage_Pop(); // if (intPtr == IntPtr.Zero) // { // return; // } // ovrAvatarMessageType ovrAvatarMessageType2 = CAPI.ovrAvatarMessage_GetType(intPtr); // switch (ovrAvatarMessageType2) // { // case ovrAvatarMessageType.AssetLoaded: // { // ovrAvatarMessage_AssetLoaded ovrAvatarMessage_AssetLoaded2 = CAPI.ovrAvatarMessage_GetAssetLoaded(intPtr); // IntPtr asset = ovrAvatarMessage_AssetLoaded2.asset; // ulong assetID = ovrAvatarMessage_AssetLoaded2.assetID; // ovrAvatarAssetType ovrAvatarAssetType2 = CAPI.ovrAvatarAsset_GetType(asset); // IntPtr key = IntPtr.Zero; // OvrAvatarAsset ovrAvatarAsset; // switch (ovrAvatarAssetType2) // { // case ovrAvatarAssetType.Mesh: // ovrAvatarAsset = new OvrAvatarAssetMesh(assetID, asset, ovrAvatarAssetType.Mesh); // break; // case ovrAvatarAssetType.Texture: // ovrAvatarAsset = new OvrAvatarAssetTexture(assetID, asset); // break; // case ovrAvatarAssetType.Material: // ovrAvatarAsset = new OvrAvatarAssetMaterial(assetID, asset); // break; // case ovrAvatarAssetType.CombinedMesh: // key = CAPI.ovrAvatarAsset_GetAvatar(asset); // ovrAvatarAsset = new OvrAvatarAssetMesh(assetID, asset, ovrAvatarAssetType.CombinedMesh); // break; // default: // throw new NotImplementedException(string.Format("Unsupported asset type format {0}", ovrAvatarAssetType2.ToString())); // } // if (ovrAvatarAssetType2 == ovrAvatarAssetType.CombinedMesh) // { // if (!assetCache.ContainsKey(assetID)) // { // assetCache.Add(assetID, ovrAvatarAsset); // } // combinedMeshLoadedCallback value2; // if (combinedMeshLoadedCallbacks.TryGetValue(key, out value2)) // { // value2(asset); // combinedMeshLoadedCallbacks.Remove(key); // } // } // else // { // HashSet value3; // if (!assetLoadedCallbacks.TryGetValue(ovrAvatarMessage_AssetLoaded2.assetID, out value3)) // { // break; // } // assetCache.Add(assetID, ovrAvatarAsset); // foreach (assetLoadedCallback item in value3) // { // item(ovrAvatarAsset); // } // assetLoadedCallbacks.Remove(ovrAvatarMessage_AssetLoaded2.assetID); // } // break; // } // case ovrAvatarMessageType.AvatarSpecification: // { // ovrAvatarMessage_AvatarSpecification ovrAvatarMessage_AvatarSpecification2 = CAPI.ovrAvatarMessage_GetAvatarSpecification(intPtr); // HashSet value; // if (!specificationCallbacks.TryGetValue(ovrAvatarMessage_AvatarSpecification2.oculusUserID, out value)) // { // break; // } // foreach (specificationCallback item2 in value) // { // item2(ovrAvatarMessage_AvatarSpecification2.avatarSpec); // } // specificationCallbacks.Remove(ovrAvatarMessage_AvatarSpecification2.oculusUserID); // break; // } // default: // throw new NotImplementedException("Unhandled ovrAvatarMessageType: " + ovrAvatarMessageType2); // } // CAPI.ovrAvatarMessage_Free(intPtr); } public void RequestAvatarSpecification(ulong userId, specificationCallback callback, bool useCombinedMesh, ovrAvatarAssetLevelOfDetail lod, bool forceMobileTextureFormat, ovrAvatarLookAndFeelVersion lookVersion, ovrAvatarLookAndFeelVersion fallbackVersion, bool enableExpressive) { CAPI.ovrAvatar_SetForceASTCTextures(forceMobileTextureFormat); HashSet value; if (!specificationCallbacks.TryGetValue(userId, out value)) { value = new HashSet(); specificationCallbacks.Add(userId, value); IntPtr specificationRequest = CAPI.ovrAvatarSpecificationRequest_Create(userId); CAPI.ovrAvatarSpecificationRequest_SetLookAndFeelVersion(specificationRequest, lookVersion); CAPI.ovrAvatarSpecificationRequest_SetFallbackLookAndFeelVersion(specificationRequest, fallbackVersion); CAPI.ovrAvatarSpecificationRequest_SetLevelOfDetail(specificationRequest, lod); CAPI.ovrAvatarSpecificationRequest_SetCombineMeshes(specificationRequest, useCombinedMesh); CAPI.ovrAvatarSpecificationRequest_SetExpressiveFlag(specificationRequest, enableExpressive); CAPI.ovrAvatar_RequestAvatarSpecificationFromSpecRequest(specificationRequest); CAPI.ovrAvatarSpecificationRequest_Destroy(specificationRequest); } value.Add(callback); } public void BeginLoadingAsset(ulong assetId, ovrAvatarAssetLevelOfDetail lod, assetLoadedCallback callback) { HashSet value; if (!assetLoadedCallbacks.TryGetValue(assetId, out value)) { value = new HashSet(); assetLoadedCallbacks.Add(assetId, value); } CAPI.ovrAvatarAsset_BeginLoadingLOD(assetId, lod); value.Add(callback); } public void RegisterCombinedMeshCallback(IntPtr sdkAvatar, combinedMeshLoadedCallback callback) { combinedMeshLoadedCallback value; if (!combinedMeshLoadedCallbacks.TryGetValue(sdkAvatar, out value)) { combinedMeshLoadedCallbacks.Add(sdkAvatar, callback); return; } throw new Exception("Adding second combind mesh callback for same avatar"); } public OvrAvatarAsset GetAsset(ulong assetId) { OvrAvatarAsset value; if (assetCache.TryGetValue(assetId, out value)) { return value; } return null; } public string GetAppId() { return (Application.platform != RuntimePlatform.Android) ? OvrAvatarSettings.AppID : OvrAvatarSettings.MobileAppID; } }