Search Unity

Add deep links to your Unity mobile apps for better user experience

July 16, 2020 in Engine & platform | 4 min. read
Topics covered
Share

Is this article helpful for you?

Thank you for your feedback!

Adding deep links within your Unity project is a simple way to drive users directly to specific content in a mobile app – no navigation required. That content becomes easily shareable and more interactive, bringing new users, guiding existing users to new content, and more.

A deep link in a mobile app is a URL that brings a user directly to a specific in-app location. For example, deep links in a music app allow friends to share their favorite playlists, which automatically open in the music app. Embedded in search results, a deep link can take users directly to rich content in a particular app, rather than sending them to a website where they might have to enter search details again.

In addition to taking users directly to the content they’re looking for, deep links give developers the ability to add logic to the link. For example, you can drive engagement in your game by letting users share rewards or gifts via a deep link URL in SMS (or another messaging system) or allow users to jump from one game to specific content in another game, creating seamless user experiences between them. This increases user engagement and reduces friction in your mobile apps.

We introduced deep link support in Unity 2019.2 and it’s available for Android, iOS, and Universal Windows Platform (UWP). This blog post explains how you can get up and running with deep links in your Unity app.

Setting up deep links

In essence, deep linking allows you to launch, and pass parameters to, your application via a single link. You can then direct users to a specific area in your application from various sources like a web page or an SMS text message. 

To start, 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:

For example, you can attach the code below to a “deep link manager” 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);
	}
}

Configuring for different platforms

Configuring an app to react to specific URLs is platform-specific: 

  • iOS: You configure the URL scheme from the Player Settings window in the Supported URL schemes section. 
  • Android: You set up an intent filter that overrides the standard app manifest to include a specific intent-filter section for Activity.
  • Universal Windows Platform (UWP): You configure a custom URI scheme from the Player Settings window in the Protocol section.

For more information about platform-specific setups, see the Enabling deep linking documentation page.

Testing your deep links

The easiest way to test a deep link is to create a simple HTML page, host it on a local web server, and access it from a web browser on your device. For example:

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

</body>
</html>

Check out our sample project

We have created a sample project to demonstrate how you can implement deep linking to start your app from somewhere other than the main menu. You can find the project source here. The end result looks like this:

July 16, 2020 in Engine & platform | 4 min. read

Is this article helpful for you?

Thank you for your feedback!

Topics covered
Related Posts