Events Module for Unity

With the now.gg Events Module for Unity, developers can conveniently control their apps by sending commands/events from their apps on now.gg Cloud to their websites where now.gg-powered embedded game is running.

To implement now.gg Events module:

  • Download and import the now.gg Events module.
  • Use now.gg Events Module.

Download and Import the Plugin

The now.gg Events Module is included as a Unity package file nowgg-events.unitypackage.

Add the plugin to your Unity project:

  1. Download the package containing the latest version of now.gg Events module for Unity.
  2. After you have downloaded the module, import it into your Unity project.
    To do this:

    • Click on Assets > Import Package > Custom Package.
    • Browse to Modules > Events directory within the downloaded package.
      • Select nowgg-events.unitypackage file.
      • Select all the listed files and click on Import.

Important

  • Once all the module files have been imported, a folder named ‘NowGGSdk‘ will be added to your project. You can find this folder at the root of the Assets folder.
  • Please do not modify the NowGGSdk folder, as it contains all the assets related to now.gg Events Module.

Use now.gg Events Module

Once the now.gg Events module has been imported; you can now send commands/events to your Android app/game.

We have included some of the sample commands/events as a reference for your implementation.

You can use the NowGGEventsSdkManager class to trigger events as illustrated below:

1. Open URL

You can call the OpenURL function of the NowGGEventsSDKManager class to open the provided URL in a new browser window, as illustrated below:

string url = "https://example.com";

 NowGGEventsSdkManager.Instance.OpenURL(url);

Reference – Additional information associated with OpenURL() can be found here.

2. Send Events and Data to the HTML Page

You can call the SendEventToBrowser function of the NowGGEventsSDKManager class, along with the required parameters, to send the events and data to your HTML page and handle it there.

a. Send Events to HTML Page

string eventName = "test-event";
 string eventData = "test-data";

 NowGGEventsSdkManager.Instance.SendEventToBrowser(eventName, eventData);

Reference – Additional information associated with SendEventToBrowser() can be found here.

b. Handle Events on HTML Page

After the data has been sent to your HTML, you can handle it there, as illustrated below:

const eventCallbackFunction = (event) => {
   console.log(event.eventName); // event1
   console.log(event.eventData); // eventData1
  
   // Perform event-based handling here
 }; 

3. Send Events and Data to the Android App

You can call the sendEventToApp function of the now.gg Embed Module with the required arguments to send events and data to your Android app and handle them.

a. Send Events to the Android App

The sendEventToApp sends a custom event and associated data to your app using the NowIfp interface of now.gg Embed module, as illustrated below:

const sendEventToApp = () => {
     const eventName = 'CustomEvent';
     const eventData = JSON.stringify({
         sample: 'case'
     });
     NowIfp.sendEventToApp(eventName, eventData);
 };

b. Handle Events in Your Android App

After the data has been sent to your Android app, you can handle it by implementing the following steps:

1. Declare an Event Callback and Register a Listener

To handle incoming events, define a callback function and register it with the NowGGEventsSdkManager. This will ensure your app responds to events as they occur.

 // Register the event listener
 NowGGEventsSdkManager.Instance.OnEventReceivedFromBrowser += OnEventReceived;

 // Initialize the event system
 NowGGEventsSdkManager.Instance.InitializeEvents();
 
 // Define the callback function to handle incoming events
 private void OnEventReceived(String eventName, String eventData) {
     // Perform actions based on the eventName and eventData
 }
2. Disposing of the Event Listener

When discarding the parent activity, it is essential to dispose of the event listener to avoid memory leaks and unnecessary callbacks. This should typically be done during cleanup, such as in your app’s OnDestroy() method.

NowGGEventsSdkManager.Instance.DestroyEventsReceiver();

Note: The DestroyEventsReceiver method unregisters the listener and releases associated resources.


4. Retrieve app Launch Data in your App

We have provided two methods to retrieve the app launch data you sent to your Android App from the HTML page.

a. Use the dictionary to retrieve all app launch data parameters

You can call the GetAppLaunchDataDictionary() function of the NowGGEventsSdkManager class, as illustrated below:

//Retrieves a dictionary of all app launch data params

 Dictionary<string, string> AppLaunchData = NowGGEventsSdkManager.Instance.GetAppLaunchDataDictionary(); 

Reference – Additional information associated with GetAppLaunchDataDictionary() can be found here.

b. Use property name to retrieve a specific app launch data parameter

You can call the GetAppLaunchDataProperty() function of the NowGGEventsSdkManager class, along with the property name, to retrieve a specific app launch data parameter, as illustrated below:

//Retrieves an app launch data property by name

 string property = NowGGEventsSdkManager.Instance.GetAppLaunchDataProperty("property_name");

Note: Returns an empty string if the property is not found.
Reference – Additional information associated with GetAppLaunchDataProperty() can be found here.

×

Events Module

Events Module for Unity

Events Module

Document Rev. 1.0

Text copied to clipboard
Link copied to clipbord
Questions? Please reach out to us at dev-support@now.gg