Deep links are links that point directly to content within your application. Unity uses the Application.absoluteURL
property and Application.deepLinkActivated
event to support deep links on the following platforms:
Unity calls the Application.deepLinkActivated
event when an application is activated from a deep link URL. To process deep links in this scenario, you can:
Application.absoluteURL
when the application starts.Application.deepLinkActivated
event while the application is running.For example, you can attach the code below to a GameObject in your startup Scene:
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))
{
// Cold start and Application.absoluteURL not null so process Deep Link.
onDeepLinkActivated(Application.absoluteURL);
}
// Initialize DeepLink Manager global variable.
else deeplinkURL = "[none]";
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
private void onDeepLinkActivated(string url)
{
// Update DeepLink Manager global variable, so URL can be accessed from anywhere.
deeplinkURL = url;
// Decode the URL to determine action.
// In this example, the app expects a link formatted like this:
// unitydl://mylink?scene1
string sceneName = url.Split("?"[0])[1];
bool validScene;
switch (sceneName)
{
case "scene1":
validScene = true;
break;
case "scene2":
validScene = true;
break;
default:
validScene = false;
break;
}
if (validScene) SceneManager.LoadScene(sceneName);
}
}
The process to configure an application to react to specific URLs is platform-specific.
There are two possible ways to make your app react to deep links: URL schemes and universal links.
To add a URL scheme, follow these steps:
unitydl
).This makes your app open any links that start with unitydl://
and allows you to process the URL in the Application.deepLinkActivated
event.
You can’t set up an app to use universal links from Unity Editor, because this requires an external website. For more information, see Apple documentation on Enabling Universal Links.
To enable deep linking, you need to set up an intent filter that overrides the standard app manifest to include a specific intent-filter
section for Activity.
The simplest way is to place the following AndroidManifest.xml
file into your Project’s Assets/Plugins/Android
folder. Unity automatically processes this file when you build your app:
<?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>
This makes your app open any links that start with unitydl://
and allows you to process the URL in the Application.deepLinkActivated
event.
To add a custom URI scheme to your app, follow these steps:
unitydl
.This makes your app open any links that start with unitydl://
.
To test a deep link, you can create an HTML file, host it on a local web server, and access it from a web browser on your device:
<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>