一、消息監聽
1.創建一個腳本命名為 “URISchemeListener” ,用于接收URI消息(代碼如下)。
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;public class URISchemeListener : MonoBehaviour
{void Start(){if (!string.IsNullOrEmpty(Application.absoluteURL)){handleDeepLink(Application.absoluteURL);}string[] args = Environment.GetCommandLineArgs();if (args.Length > 1){string uri = args[1];handleDeepLink(uri);}Application.deepLinkActivated += handleDeepLink;}private void handleDeepLink(string url){Debug.Log("MyApp Protocol activated with URL: " + url);}}
2.將?“URISchemeListener” 腳本掛載在 GameObject 對象上。
二、添加注冊表
Windows平臺
1.創建一個“**.reg” 文件,內如下。
Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\myapp]
@="URL:myapp Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\editor\DefaultIcon]
@=""C:\\Deskdop\\test.exe",1"
[HKEY_CLASSES_ROOT\editor\shell]
@=""
[HKEY_CLASSES_ROOT\editor\shell\open]
@=""
[HKEY_CLASSES_ROOT\editor\shell\open\command]
@="\"C:\\Deskdop\\test.exe\" \"%1\""
- myapp 是你和 uri 約定好的名稱。
- @="\"C:\\Deskdop\\test.exe\" \"%1\"" 是你exe 的地址
2.雙擊注冊你所創建的注冊表。
Mac平臺
1.找到 “Info.plist” 文件,并添加下面配置。
<!-- 新增的 URI Scheme 配置 --><key>CFBundleURLTypes</key><array><dict><key>CFBundleURLSchemes</key><array><string>myapp</string></array></dict></array>