「Unity/URLからアプリ起動/基本」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→OS別のシステム) |
(→Unity側にdeepLinkのスクリプト追加) |
||
| 行48: | 行48: | ||
Debug.Log("DeepLink Activated: " + url); | Debug.Log("DeepLink Activated: " + url); | ||
} | } | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | ==deepLinkないのユーザidなどを取得する場合== | ||
| + | <pre> | ||
| + | using UnityEngine; | ||
| + | using UnityEngine.UI; | ||
| + | using System.Text.RegularExpressions; | ||
| + | |||
| + | public class SampleScene : MonoBehaviour | ||
| + | { | ||
| + | [SerializeField] Text deepLinkText; | ||
| + | [SerializeField] Text userIdText; | ||
| + | string deepLinkURL; | ||
| + | void Start() | ||
| + | { | ||
| + | // アプリが起動中にリンクで開かれた場合のみ | ||
| + | Application.deepLinkActivated += HandleDeepLink; | ||
| + | // アプリがディープリンクで起動された場合のみ | ||
| + | if (!string.IsNullOrEmpty(Application.absoluteURL)) | ||
| + | { | ||
| + | deepLinkURL = Application.absoluteURL; | ||
| + | Debug.Log("DeepLink: " + deepLinkURL); | ||
| + | deepLinkText.text = "DeepLink URL:\n" + deepLinkURL; | ||
| + | userIdText.text = "UserId: " + GetUserIdByUrl(deepLinkURL); | ||
| + | } | ||
| + | } | ||
| + | // アプリが開いたままのときのディープリンクを受信したとき | ||
| + | void HandleDeepLink(string url) | ||
| + | { | ||
| + | deepLinkURL = url; | ||
| + | Debug.Log("DeepLink Activated: " + url); | ||
| + | deepLinkText.text = "DeepLink Activated URL:\n" + deepLinkURL; | ||
| + | userIdText.text = "UserId: " + GetUserIdByUrl(deepLinkURL); | ||
| + | } | ||
| + | |||
| + | string GetUserIdByUrl(string url) | ||
| + | { | ||
| + | Match m = Regex.Match(url, @"userId\/(\d+)$"); | ||
| + | if (m.Success) | ||
| + | { | ||
| + | return m.Groups[1].Value; | ||
| + | } | ||
| + | return ""; | ||
| + | } | ||
| + | |||
} | } | ||
</pre> | </pre> | ||
2025年12月5日 (金) 08:26時点における版
OS別のシステム
- iOS → Universal Links(ユニバーサルリンク)
- Android → App Links / Intent Filter(ディープリンク)
動作図解
[ユーザー] ──URLタップ──▶ [Android OS/ IOS]
│
├─ アプリある? → Yes → 直で起動
│
└─ アプリない → URL保存(パラメータ付)(アプリじゃなく端末OSがやる)
│
▼
[Google Play ストア / Appストア]
│
インストール&起動
│
▼
OS が保存していた DeepLink を渡す
│
▼
[アプリ]
Unity側にdeepLinkのスクリプト追加
using UnityEngine;
public class SampleScene : MonoBehaviour
{
string deepLinkURL;
void Start()
{
// アプリが起動中にリンクで開かれた場合のみ
Application.deepLinkActivated += HandleDeepLink;
// アプリがディープリンクで起動された場合のみ
if (!string.IsNullOrEmpty(Application.absoluteURL))
{
deepLinkURL = Application.absoluteURL;
Debug.Log("DeepLink: " + deepLinkURL);
}
}
// アプリが開いたままのときのディープリンクを受信したとき
void HandleDeepLink(string url)
{
deepLinkURL = url;
Debug.Log("DeepLink Activated: " + url);
}
}
deepLinkないのユーザidなどを取得する場合
using UnityEngine;
using UnityEngine.UI;
using System.Text.RegularExpressions;
public class SampleScene : MonoBehaviour
{
[SerializeField] Text deepLinkText;
[SerializeField] Text userIdText;
string deepLinkURL;
void Start()
{
// アプリが起動中にリンクで開かれた場合のみ
Application.deepLinkActivated += HandleDeepLink;
// アプリがディープリンクで起動された場合のみ
if (!string.IsNullOrEmpty(Application.absoluteURL))
{
deepLinkURL = Application.absoluteURL;
Debug.Log("DeepLink: " + deepLinkURL);
deepLinkText.text = "DeepLink URL:\n" + deepLinkURL;
userIdText.text = "UserId: " + GetUserIdByUrl(deepLinkURL);
}
}
// アプリが開いたままのときのディープリンクを受信したとき
void HandleDeepLink(string url)
{
deepLinkURL = url;
Debug.Log("DeepLink Activated: " + url);
deepLinkText.text = "DeepLink Activated URL:\n" + deepLinkURL;
userIdText.text = "UserId: " + GetUserIdByUrl(deepLinkURL);
}
string GetUserIdByUrl(string url)
{
Match m = Regex.Match(url, @"userId\/(\d+)$");
if (m.Success)
{
return m.Groups[1].Value;
}
return "";
}
}
