Skip to main content

Set UserAgent

Example 1

public class TestScript : MonoBehaviour
{
public EdgeCanvas m_edgeCanvas;

void OnGUI()
{
if (GUILayout.Button("SetUserAgent"))
{
// Method 1
m_edgeCanvas.SetUserAgent("Unity UWebView2");

// Method 2
m_edgeCanvas.SetUserAgent(m_edgeCanvas.GetUserAgent() + " Unity");
}
}
}

Result:

Unity UWebView2
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0 Unity

Example 2

tip

This is a global modification method. When each EdgeCanvas is created, it will set its UserAgent using the returned string. This approach is very useful for appending your own identifier to the original UserAgent without altering it.

void Awake()
{
Edge.ModifyUserAgent += SetWebViewUserAgent;
}

void OnDestroy()
{
Edge.ModifyUserAgent -= SetWebViewUserAgent;
}

string SetWebViewUserAgent(EdgeCanvas canvas, string userAgent)
{
return userAgent + " Hello UWebView";
}

Result

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0 Hello UWebView