Unity WebGL (Mobile Phantom Browser) – How to Open Twitter/Telegram Links in a New Tab from Game

Unity WebGL (Mobile Phantom Browser) – How to Open Twitter/Telegram Links in a New Tab from Game

I have a Unity WebGL game that is Solana-based and runs inside the Phantom browser on mobile. I want to add two links (Twitter and Telegram) that open in a new tab when the player presses a button in the game. I tried using a .jslib file to implement this, but it's not working.

Can you please help me with this?

Answer

✅ Step 1: Create the .jslib file

Create a file named, for example, OpenLink.jslib inside the Assets/Plugins/WebGL folder of your Unity project. Inside, add the following code:

js

CopiarEditar

mergeInto(LibraryManager.library, { OpenExternalLink: function (urlPtr) { var url = UTF8ToString(urlPtr); var newWindow = window.open(url, '_blank'); if (newWindow) { newWindow.focus(); } else { console.log("Could not open the window. Possibly blocked by the browser?"); } } });

This script opens the URL in a new tab. On mobile browsers (and embedded browsers like Phantom wallet), window.open() may be blocked if not triggered directly by a user action (e.g., a button click).


✅ Step 2: Create a C# wrapper in Unity

Create a C# script (e.g., ExternalLink.cs) and place it inside Assets/Scripts/:

csharp

CopiarEditar

usingSystem.Runtime.InteropServices; using UnityEngine; public class ExternalLink : MonoBehaviour { [DllImport("__Internal")] private static extern void OpenExternalLink(string url); public void OpenTwitter() { #if UNITY_WEBGL && !UNITY_EDITOR OpenExternalLink("https://twitter.com/YOUR_USERNAME"); #else Application.OpenURL("https://twitter.com/YOUR_USERNAME"); #endif } public void OpenTelegram() { #if UNITY_WEBGL && !UNITY_EDITOR OpenExternalLink("https://t.me/YOUR_CHANNEL"); #else Application.OpenURL("https://t.me/YOUR_CHANNEL"); #endif } }


✅ Step 3: Connect the script to a button

  1. Create a button in your Unity scene.

  2. Attach the ExternalLink.cs script to a GameObject in the scene (e.g., an empty object named LinkManager).

  3. In the button’s OnClick() panel, drag the GameObject that holds the script and select ExternalLink -> OpenTwitter() or OpenTelegram() based on the desired button.


⚠️ Important Notes for Phantom and Mobile Browsers

  • The link must be triggered directly by a user interaction (click/tap). If the method is called via code without direct user interaction, window.open will be blocked.

  • Some mobile wallets (like Phantom) open dApps in a custom webview browser. In those cases, the link might not open in a new tab, but rather redirect the same view. There’s no universal workaround, but you can try forcing _system via window.open(url, '_system'), although this is not standard and may not work across all platforms.

Enjoyed this question?

Check out more content on our blog or follow us on social media.

Browse more questions