Version: 2021.1
Using Unity as a Library in other applications
Standalone

Enabling deep linking

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:

  • Android
  • iOS
  • Universal Windows Platform (UWP)

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 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.

iOS

There are two possible ways to make your app react to deep links: URL schemes and universal links.

URL schemes

To add a URL scheme, follow these steps:

  1. Open the iOS Player Settings window (menu: Edit > Project Settings > Player Settings, then select iOS).
  2. Select Other, then scroll down to Configuration.
  3. Expand the Supported URL schemes section and, in the Element 0 field, enter the URL scheme associated with your app (for example, unitydl).

This makes your app open any links that start with unitydl:// and allows you to process the URL in the Application.deepLinkActivated event.

Universal links

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.

Android

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.

Universal Windows Platform (UWP)

To add a custom URI scheme to your app, follow these steps:

  1. Open the UWP Player Settings window (menu: Edit > Project Settings > Player Settings, then select UWP).
  2. Select Publishing Settings, then scroll down to Protocol.
  3. In the Name field, enter unitydl.

This makes your app open any links that start with unitydl://.

Testing deep links

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>
Using Unity as a Library in other applications
Standalone
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961