C# Function to print userAgent

I’ve been defeated here regarding my ability to problem-solve… So here I am. First I’ll post my code, and then ill give context so you don’t have to read everything.

var DeviceInfo = {
// Function to get user agent string
GetUserAgent: function() {
return navigator.userAgent;
},
// Function to get screen width
GetScreenWidth: function() {
return screen.width;
},
}
autoAddDeps(DeviceInfo,'GetUserAgent')
autoAddDeps(DeviceInfo,'GetScreenWidth')
mergeInto(LibraryManager.library, DeviceInfo);
using UnityEngine;
using TMPro;
using System.Runtime.InteropServices;

public class DeviceInfo : MonoBehaviour
{
    public TextMeshProUGUI userAgentText; // Reference to TextMeshPro Text object for user agent
    public TextMeshProUGUI screenWidthText; // Reference to TextMeshPro Text object for screen width

    // Import JavaScript plug-in functions
    [DllImport("__Internal")]
    private static extern string GetUserAgent();

    [DllImport("__Internal")]
    private static extern int GetScreenWidth();

    void Start()
    {
        // Call JavaScript plug-in functions to retrieve device and browser information
        string userAgent = GetUserAgent();
       int screenWidth = GetScreenWidth()
        ;

        // Update TextMeshPro Text objects with retrieved information
        userAgentText.text = "User Agent: " + userAgent;
        screenWidthText.text = "Screen Width: " + screenWidth;
    }
}

I was working on a Device Feedback project for WebGL. The project will run a Unity WebGL project in the web browser. I started by creating my unity asset folders, then moved to my next step by creating a .jslib File, where I used Visual Studios to make a textile that I just changed to .jslib and saved it right into the PlugIn directory I made in my asset folder and where I then used the unity inspector to set the Platform Settings (But, didn’t tamper with any of the other settings which is one thing I’m hoping to get advice on). Then I made the c# script. After this, I decided id just set everything up in the editor and press play before i build the project, however, I always get a EntryPointNotFoundException:
assembly: type: member: (null)
DeviceInfo.Start () error, so I just built the project and had some success with my GetScreenWidth(); printing to the screen. My problem is, the userAgent Did not. Does anyone know where I might have done something wrong, or what I might need to understand to solve this problem, i am currently looking up information on wasm, but today I was aggressively trying to cover Emscriten docs, and I have no clue what to do or change regarding getting this string to print.


Solved!
The primary problem… failure to read the Unity docs thoroughly.
my .jslib should have looked something like this instead of what I had initially:

var DeviceInfo = {
    // Function to get user agent string
    GetUserAgent: function() {

    console.log(navigator.userAgent + "attempt 15");
    var returnStr = navigator.userAgent;
    var bufferSize = lengthBytesUTF8(returnStr) + 1;
    var buffer = _malloc(bufferSize);
    stringToUTF8(returnStr, buffer, bufferSize);
        return buffer;
    },
    // Function to get screen width
    GetScreenWidth: function() {
    console.log(screen.width);
        return screen.width;
    },
}
autoAddDeps(DeviceInfo,'GetUserAgent')
autoAddDeps(DeviceInfo,'GetScreenWidth')
mergeInto(LibraryManager.library, DeviceInfo);

My confusion came from not understanding what UTF* or buffering was suggesting when I glanced over the doc the first time. I found that the Javascript plug-in wasn’t having a problem getting the userAgent after I put a line to print the browser user agent header to the console and saw this information there. However, where my Javascript and C# knowledge and understanding began to complicate this task was how C# or even C/C++ code expects data in a specific format or encoding if you’re interfacing with native Code Using Emscipten… I’m still not sure about what exactly makes this a necessary step, so I’ll do more reading regarding the matter. But it’s working now, so I’ll just bookmark the lesson for later. If anyone comes across this with knowledge that can share that can help me further understand this I’d love the help!
Otherwise Thanks for reading.

Your confusion is understandable. Running Unity on the web inherently means working with Emscripten, which means understanding how multiple technologies work.

A high-level explanation of what’s happening is: Emscripten compiles the Unity engine into WebAssembly, and uses a heap to represent its memory. So whenever you want to access data from your Unity program in your WebGL application, or pass data to Unity, you’ll be interfacing with this heap. Emscripten gives us methods to access this heap, and stringToUTF8 is one of them.

Here’s some reading to help you understand more:
https://emscripten.org/docs/porting/emscripten-runtime-environment.html#emscripten-memory-model

1 Like