Unity导入Android Studio项目:App Widget点击跳转深度链接实现咨询
Hey there! Let's walk through exactly how to make your app widget open a specific screen in your Unity-built Android app using deep links. Since you're learning on the fly, I'll break this down into straightforward, actionable steps:
First, you need to tell Android that your app can handle custom deep link URLs. This goes in your AndroidManifest.xml (either modify it directly in Android Studio, or set up a manifest template in Unity to avoid overwrites when re-packaging):
Add an <intent-filter> to your Unity main activity (usually UnityPlayerActivity) like this:
<activity android:name="com.unity3d.player.UnityPlayerActivity" android:exported="true" <!-- Ensure this exists for Android 12+ compatibility --> ...> <!-- Keep your existing intent-filters (like the LAUNCHER one) --> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- Define your custom deep link scheme --> <data android:scheme="yourappscheme" <!-- Pick a unique scheme for your app, e.g., mygame:// --> android:host="deeplink" android:path="/targetscreen" <!-- Identifies the specific screen to open --> /> </intent-filter> </activity>
Pro tip: Adjust scheme, host, and path to match your needs—use different path values if you want to link to multiple screens later.
Next, update your AppWidgetProvider class to trigger the deep link when the widget is clicked. In the onUpdate method, set up a PendingIntent that launches the deep link:
@Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { for (int appWidgetId : appWidgetIds) { // Build your deep link URI Uri deepLinkUri = Uri.parse("yourappscheme://deeplink/targetscreen"); // Create an intent to handle the deep link Intent intent = new Intent(Intent.ACTION_VIEW, deepLinkUri); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); // Create a PendingIntent (use FLAG_IMMUTABLE for Android 12+) PendingIntent pendingIntent = PendingIntent.getActivity( context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE ); // Link the PendingIntent to your widget's layout RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.your_widget_layout); // Replace R.id.widget_root with the ID of the clickable view in your widget layout views.setOnClickPendingIntent(R.id.widget_root, pendingIntent); // Update the widget appWidgetManager.updateAppWidget(appWidgetId, views); } }
Important: For Android 12 and above, you must include FLAG_IMMUTABLE or FLAG_MUTABLE in your PendingIntent flags—FLAG_IMMUTABLE is safe for most cases.
Finally, you need to listen for the deep link in your Unity code and navigate to the correct screen. Here's a C# example:
using UnityEngine; using UnityEngine.SceneManagement; public class DeepLinkHandler : MonoBehaviour { void Start() { // Check for a deep link when the app starts ProcessDeepLink(Application.absoluteURL); // Register a callback for future deep links (if app is already running) Application.deepLinkCallback += ProcessDeepLink; } void ProcessDeepLink(string url) { if (string.IsNullOrEmpty(url)) return; // Parse the deep link URL Uri uri = new Uri(url); // Check if this is the deep link we care about if (uri.Scheme == "yourappscheme" && uri.Host == "deeplink" && uri.Path == "/targetscreen") { // Navigate to your target screen—replace with your own logic SceneManager.LoadScene("YourTargetScene"); // Or if opening a UI panel: UIManager.Instance.OpenTargetPanel(); } } void OnDestroy() { // Clean up the callback to avoid memory leaks Application.deepLinkCallback -= ProcessDeepLink; } }
Attach this script to a persistent GameObject in your Unity project (like a GameManager) so it runs when the app starts.
Before relying on the widget, test your deep link directly with adb to make sure it works:
adb shell am start -a android.intent.action.VIEW -d "yourappscheme://deeplink/targetscreen" com.your.app.package
If this opens your app and navigates to the correct screen, your manifest and Unity setup are good to go.
内容的提问来源于stack exchange,提问作者Chris Brown

