add ready player me POC

This commit is contained in:
2024-11-22 07:08:52 +02:00
parent 855639487b
commit 7c4e48f388
1033 changed files with 224048 additions and 28 deletions
@@ -0,0 +1,15 @@
using UnityEngine;
using UnityEngine.UI;
namespace ReadyPlayerMe.Samples.WebGLSample
{
public class DebugLogger : MonoBehaviour
{
[SerializeField] private Text debugText;
public void LogMessage(string messages)
{
debugText.text = messages;
}
}
}
@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: c41050fe3b94d4f41a0a95a6fd0f33fc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 259814
packageName: Ready Player Me Avatar and Character Creator
packageVersion: 7.3.1
assetPath: Assets/Ready Player Me/Core/Samples/WebGLSample/Scripts/DebugLogger.cs
uploadId: 704624
@@ -0,0 +1,25 @@
using UnityEngine;
using UnityEngine.UI;
namespace ReadyPlayerMe.Samples.WebGLSample
{
public class DemoCanvas : MonoBehaviour
{
[SerializeField] private Button createAvatarButton;
private void Start()
{
if (createAvatarButton != null)
{
createAvatarButton.onClick.AddListener(OnCreateAvatar);
}
}
public void OnCreateAvatar()
{
#if !UNITY_EDITOR && UNITY_WEBGL
WebInterface.SetIFrameVisibility(true);
#endif
}
}
}
@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 2141f9b83133a1241b4130d10971734d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 259814
packageName: Ready Player Me Avatar and Character Creator
packageVersion: 7.3.1
assetPath: Assets/Ready Player Me/Core/Samples/WebGLSample/Scripts/DemoCanvas.cs
uploadId: 704624
@@ -0,0 +1,87 @@
using System;
using Newtonsoft.Json;
using ReadyPlayerMe.Core;
using ReadyPlayerMe.Core.WebView;
using UnityEngine;
namespace ReadyPlayerMe.Samples.WebGLSample
{
public enum AutoInitialize
{
OnStart,
OnAwake,
None
}
public class WebFrameHandler : MonoBehaviour
{
private string TAG = nameof(WebFrameHandler);
public Action<string> OnAvatarExport;
public Action<string> OnUserSet;
public Action<string> OnUserUpdated;
public Action<string> OnUserAuthorized;
public Action onUserLogOut;
public Action<AssetRecord> OnAssetUnlock;
[SerializeField] private AutoInitialize autoInitialize = AutoInitialize.OnStart;
[SerializeField] private UrlConfig urlConfig;
private void Awake()
{
if (autoInitialize == AutoInitialize.OnAwake)
{
Setup();
}
}
private void Start()
{
if (autoInitialize == AutoInitialize.OnStart)
{
Setup();
}
}
public void Setup(string loginToken = "")
{
WebInterface.SetupRpmFrame(urlConfig.BuildUrl(loginToken), name);
}
/// <summary>
/// This message is received from the RPM iFrame Javascript in the RPM WebGL Template.
/// </summary>
/// <param name="message">The message will contain data pass from the iFrame as JSON</param>
// ReSharper disable once UnusedMember.Global
public void FrameMessageReceived(string message)
{
var webMessage = JsonConvert.DeserializeObject<WebMessage>(message);
switch (webMessage.eventName)
{
case WebViewEvents.AVATAR_EXPORT:
SDKLogger.Log(TAG, webMessage.eventName);
OnAvatarExport?.Invoke(webMessage.GetAvatarUrl());
WebInterface.SetIFrameVisibility(false);
break;
case WebViewEvents.USER_SET:
SDKLogger.Log(TAG, webMessage.eventName);
OnUserSet?.Invoke(webMessage.GetUserId());
break;
case WebViewEvents.USER_AUTHORIZED:
SDKLogger.Log(TAG, webMessage.eventName);
OnUserAuthorized?.Invoke(webMessage.GetUserId());
break;
case WebViewEvents.USER_UPDATED:
SDKLogger.Log(TAG, webMessage.eventName);
OnUserUpdated?.Invoke(webMessage.GetUserId());
break;
case WebViewEvents.USER_LOGOUT:
SDKLogger.Log(TAG, webMessage.eventName);
onUserLogOut?.Invoke();
break;
case WebViewEvents.ASSET_UNLOCK:
OnAssetUnlock?.Invoke(webMessage.GetAssetRecord());
break;
}
}
}
}
@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 7deab5f9ccb5efa4ebb6eaf779bd3255
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 259814
packageName: Ready Player Me Avatar and Character Creator
packageVersion: 7.3.1
assetPath: Assets/Ready Player Me/Core/Samples/WebGLSample/Scripts/WebFrameHandler.cs
uploadId: 704624
@@ -0,0 +1,63 @@
using ReadyPlayerMe.Core;
using UnityEngine;
using BodyType = ReadyPlayerMe.Core.BodyType;
namespace ReadyPlayerMe.Samples.WebGLSample
{
[RequireComponent(typeof(WebFrameHandler))]
public class WebGLAvatarLoader : MonoBehaviour
{
private const string TAG = nameof(WebGLAvatarLoader);
private GameObject avatar;
private string avatarUrl = "";
private WebFrameHandler webFrameHandler;
private void Start()
{
webFrameHandler = GetComponent<WebFrameHandler>();
webFrameHandler.OnAvatarExport += HandleAvatarLoaded;
webFrameHandler.OnUserSet += HandleUserSet;
webFrameHandler.OnUserAuthorized += HandleUserAuthorized;
}
private void OnAvatarLoadCompleted(object sender, CompletionEventArgs args)
{
if (avatar) Destroy(avatar);
avatar = args.Avatar;
if (args.Metadata.BodyType == BodyType.HalfBody)
{
avatar.transform.position = new Vector3(0, 1, 0);
}
}
private void OnAvatarLoadFailed(object sender, FailureEventArgs args)
{
SDKLogger.Log(TAG, $"Avatar Load failed with error: {args.Message}");
}
public void HandleAvatarLoaded(string newAvatarUrl)
{
LoadAvatarFromUrl(newAvatarUrl);
}
public void HandleUserSet(string userId)
{
SDKLogger.Log(TAG,$"User set: {userId}");
}
public void HandleUserAuthorized(string userId)
{
SDKLogger.Log(TAG,$"User authorized: {userId}");
}
public void LoadAvatarFromUrl(string newAvatarUrl)
{
var avatarLoader = new AvatarObjectLoader();
avatarUrl = newAvatarUrl;
avatarLoader.OnCompleted += OnAvatarLoadCompleted;
avatarLoader.OnFailed += OnAvatarLoadFailed;
avatarLoader.LoadAvatar(avatarUrl);
}
}
}
@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 579002ecbbdf8cd45928e7a0dae34a2d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 259814
packageName: Ready Player Me Avatar and Character Creator
packageVersion: 7.3.1
assetPath: Assets/Ready Player Me/Core/Samples/WebGLSample/Scripts/WebGLAvatarLoader.cs
uploadId: 704624
@@ -0,0 +1,43 @@
using System.Runtime.InteropServices;
namespace ReadyPlayerMe.Samples.WebGLSample
{
public static class WebInterface
{
[DllImport("__Internal")]
private static extern void SetupRpm(string partner, string targetGameObjectName = "");
[DllImport("__Internal")]
private static extern void ShowReadyPlayerMeFrame();
[DllImport("__Internal")]
private static extern void HideReadyPlayerMeFrame();
[DllImport("__Internal")]
private static extern void ReloadUrl(string url);
public static void SetIFrameVisibility(bool isVisible)
{
if (isVisible)
{
ShowReadyPlayerMeFrame();
return;
}
HideReadyPlayerMeFrame();
}
public static void SetupRpmFrame(string url, string targetGameObjectName)
{
#if !UNITY_EDITOR && UNITY_WEBGL
SetupRpm(url, targetGameObjectName);
#endif
}
public static void ReloadWithUrl(string url)
{
ReloadUrl(url);
}
}
}
@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: ffa2fd7e3689d084eb7734af76971e18
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 259814
packageName: Ready Player Me Avatar and Character Creator
packageVersion: 7.3.1
assetPath: Assets/Ready Player Me/Core/Samples/WebGLSample/Scripts/WebInterface.cs
uploadId: 704624