unity3d端监听 uri scheme
一、消息监听
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>