DEV Community

Cover image for Alternate way to implement DeepLink in Unity3D without Firebase
Kiran Jodhani
Kiran Jodhani

Posted on

Alternate way to implement DeepLink in Unity3D without Firebase

Deep linking
Deep links are URL links outside of your application that direct users to a location in your application. When the user clicks a deep link for an application, the operating system opens the Unity application at a specified place (for example, a specific scene). Unity uses the Application.absoluteURL property and Application.deepLinkActivated event to support deep links on the following platforms:

  • iOS
  • Android
  • Universal Windows Platform(UWP)
  • macOS

In this post I will cover Android platform. I will add separate blog for each platforms.

Enabling deep linking for Android applications

Before you can process deep links, you need to configure your application to react to them. This section contains instructions on how set up deep links for Android.

To enable deep linking for Android applications, use an intent filter. An intent filter overrides the standard Android App Manifest to include a specific intent filter section for Activity. To set up an intent filter:

  1. In the Project window, go to Assets > Plugins > Android.
  2. Create a new file and call it AndroidManifest.xml. Unity automatically processes this file when you build your application.
  3. Copy the following code sample into the new file and save it.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<application>
<activity android:name="com.unity3d.player.UnityPlayerActivity" android:theme="@style/UnityThemeSelector" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="unitydl" android:host="mylink" />
</intent-filter>
</activity>
</application>
</manifest>

Your Android application now opens when the device processes any link that starts with unitydl://.

Using deep links

To process deep links, you can either:

  • Check Application.absoluteURL when the application starts.
  • Subscribe to the Application.deepLinkActivated event while the application is running. When the device opens an application from a deep link URL, Unity raises the Application.deepLinkActivated event.

The following code sample shows you how to process a deep URL and load a scene depending on the URL.

using UnityEngine;
using UnityEngine.SceneManagement;

public class ProcessDeepLinkMngr : MonoBehaviour
{
public static ProcessDeepLinkMngr Instance { get; private set; }
public string deeplinkURL;
private void Awake()
{
if (Instance == null)
{
Instance = this;

Application.deepLinkActivated += onDeepLinkActivated;
if (!string.IsNullOrEmpty(Application.absoluteURL))
{
onDeepLinkActivated(Application.absoluteURL);
}
else deeplinkURL = "[none]";
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}

private void onDeepLinkActivated(string url)
{
    deeplinkURL = url;
    string sceneName = url.Split('?')[1];
    bool validScene;
    switch (sceneName)
    {
        case "scene1":
            validScene = true;
            break;
        case "scene2":
            validScene = true;
            break;
        default:
            validScene = false;
            break;
    }
    if (validScene) SceneManager.LoadScene(sceneName);
}
Enter fullscreen mode Exit fullscreen mode

}

To test a deep link:

  • Create an HTML file that includes the deep link to test.
  • Host it on a local web server.
  • Access it from a web browser on your device and click the link.

Example HTML file
This is an example HTML file that you can use to test deep links. To redirect the link, change the href attribute in one of the elements.

<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
</head>
<body >
<h1>My Deep Link Test page</h1>
<p><a href="unitydl://mylink">Launch</a></p>
<p><a href="unitydl://mylink?parameter">Launch with Parameter</a></p>
</body>
</html>

Top comments (0)