Skip to main content

Custom Methods For XR Experience

As we know, XRMOD engine is divided into a launcher and a content package(XR-Experience).

So XR-Experience script cannot directly call third-party methods or developer-defined script methods. Because the methods has no ahead of time (AOT) code was generated. For example, if you have embedded uniwebview in your application, you will not be able to call the appropriate method in the XRMOD Experience code to launch the browser. However, we also provide a way to do this.

Custom Method

Example

using System;
using UnityEngine;
using System.Collections;
using com.Phantoms.ARMODAPI.Runtime;
using com.Phantoms.ActionNotification.Runtime;
using UnityEngine.Assertions;

namespace APITest.Runtime
{
public class APITestMainEntry
{
//XRMOD API
internal static API ARMODAPI = new API(nameof(APITest));

public async void OnLoad()
{
// we can not directly call this method.
Uniwebview.OpenWebPage("https://phantomsxr.cn");
}
}
}

Custom Bridge

In here we will using XRMOD Action Notification package to create a bridge.

Guides:

  1. Add com.Phantoms.ActionNotification.Runtime to your ssembly definitions

    Add Reference
    Add References

    When you use Assembly definitions - ASMDEF must add the references for it.

  2. Register the action in your code. Calling ActionNotificationCenter.DefaultCenter.AddObserver to create an action

    danger
    • Your script must be persistent. You can add DontDestroyOnLoad attrbute for your class.
    • AddObserver the first parameter is the response Action, the second parameter is the key of the Action, the key must be globally unique.
    PcxmPage
    namespace Runtime.UI
    {
    using UnityEngine;
    using Runtime.Core;
    using Runtime.Utils;
    using Runtime.Models;
    using Runtime.Config;
    using SDKEntry.Runtime;
    using System.Threading.Tasks;
    using com.phantoms.models.Runtime;
    using UnityEngine.SceneManagement;
    using com.Phantoms.ActionNotification.Runtime;

    [DontDestroyOnLoad]
    public class PcxmPage : UIBase
    {
    public void OnExitClicked()
    {
    var tmp_SDKEntryPoint = UnityEngine.Object.FindObjectOfType<SDKEntryPoint>();
    tmp_SDKEntryPoint.Dispose();
    StartCoroutine(GameEnvironment.Singleton.SceneM.LoadSceneAsync(2));
    }

    private void OnEnable()
    {
    ActionNotificationCenter.DefaultCenter.AddObserver(OpenH5Page, "OpenH5");
    }

    // Custom Action for XR-Experience to open webpage using Uniwebview
    private void OpenH5Page(BaseNotificationData _data)
    {
    var tmp_data = JsonUtility.FromJson<H5BridgeData>(_data.BaseData);
    Debug.Log(tmp_data.link);
    }
    }
    }
  3. XRMOD XR-Experience scripting trigger the bridge. When we call PostNotification must passing a BaseNotificationData. Similarly PostNotification also has two parameters, the first parameter is the key, that is, the action id to be executed; the second parameter is the data transmitted to the action.

    Trigger
    ActionNotificationCenter.DefaultCenter.PostNotification("OpenH5", new BaseNotificationData()
    {
    BaseData = "{\"title\":\"PostNotification\", \"link\":\"https://phantomsxr.cn\"}"
    });
    caution

    All actions that have been registered to the ActionNotificationCenter will be released after the XRMOD module is closed(Calling Dispose).

Summary

Although we cannot call third-party or user-defined methods directly in XR-Experience scripts, we can do so through ActionNotificationCenter.