Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question C# Function to print userAgent

Discussion in 'Web' started by Code_Skunk, Mar 24, 2024.

  1. Code_Skunk

    Code_Skunk

    Joined:
    Jun 23, 2015
    Posts:
    7
    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);


    Code (CSharp):
    1. using UnityEngine;
    2. using TMPro;
    3. using System.Runtime.InteropServices;
    4.  
    5. public class DeviceInfo : MonoBehaviour
    6. {
    7.     public TextMeshProUGUI userAgentText; // Reference to TextMeshPro Text object for user agent
    8.     public TextMeshProUGUI screenWidthText; // Reference to TextMeshPro Text object for screen width
    9.  
    10.     // Import JavaScript plug-in functions
    11.     [DllImport("__Internal")]
    12.     private static extern string GetUserAgent();
    13.  
    14.     [DllImport("__Internal")]
    15.     private static extern int GetScreenWidth();
    16.  
    17.     void Start()
    18.     {
    19.         // Call JavaScript plug-in functions to retrieve device and browser information
    20.         string userAgent = GetUserAgent();
    21.        int screenWidth = GetScreenWidth()
    22.         ;
    23.  
    24.         // Update TextMeshPro Text objects with retrieved information
    25.         userAgentText.text = "User Agent: " + userAgent;
    26.         screenWidthText.text = "Screen Width: " + screenWidth;
    27.     }
    28. }
    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:<unknown assembly> type:<unknown 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. upload_2024-3-24_1-8-33.png
     
  2. Code_Skunk

    Code_Skunk

    Joined:
    Jun 23, 2015
    Posts:
    7
    upload_2024-3-26_14-57-44.png
    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:
    Code (JavaScript):
    1. var DeviceInfo = {
    2.     // Function to get user agent string
    3.     GetUserAgent: function() {
    4.  
    5.     console.log(navigator.userAgent + "attempt 15");
    6.     var returnStr = navigator.userAgent;
    7.     var bufferSize = lengthBytesUTF8(returnStr) + 1;
    8.     var buffer = _malloc(bufferSize);
    9.     stringToUTF8(returnStr, buffer, bufferSize);
    10.         return buffer;
    11.     },
    12.     // Function to get screen width
    13.     GetScreenWidth: function() {
    14.     console.log(screen.width);
    15.         return screen.width;
    16.     },
    17. }
    18. autoAddDeps(DeviceInfo,'GetUserAgent')
    19. autoAddDeps(DeviceInfo,'GetScreenWidth')
    20. 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.
     
  3. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    278
    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
    https://docs.unity3d.com/Manual/webgl-memory.html
     
    Code_Skunk likes this.