Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question Same Unity Project for WebGL and Android

Discussion in 'Web' started by devrandomzero, Jan 24, 2024.

  1. devrandomzero

    devrandomzero

    Joined:
    Aug 2, 2020
    Posts:
    43
    Disclaimer:
    if there was a general Platform forum section this thread should have been created there because it's about two different platform. I posted here because with WebGL I've got quite less experience than with Android

    Hi,
    I'm working on a project that should be built for WebGL (run on a desktop browser only, not mobile) and Android.
    Knowing that the are some differences between those two platform about requirements and available functionalities (as explained in the docs), do you think that I can still make them coexist easly (maybe using conditional compilation) or do I need to have separate projects?
    For example for any REST api usually I use async/await, in this case can I still use it in WebGL being single thread or must I use coroutines?
    Are there other big difference I should now of that I could encounter down the line?
    For your information the project is not computationally/graphically demanding.

    Thank you as always for your help

    Bye
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,767
    You generally work with one Unity project that exports to various platforms. Whatever platform-specific stuff you need to have can be configured via Player Settings and there you can also add scripting define symbols to include/exclude code.

    In your case you only need to swap the method definition with an #if and you can do so by line-wrapping a method like so (from the top of my head, I hope it works):
    Code (CSharp):
    1. public void
    2. #if !PLATFORM_WEBGL
    3. async
    4. #endif
    5. MethodName(Parameters params)
    6. {
    7.     // method body ...
    8. }
    It's also common to create a custom build process to make scripted changes to each platform.
     
  3. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    534
    You can use async/await just fine on webgl builds. However, be mindful that even though Unity officially supports webgl, many things are just flat out broken or don't work as expected/has issues.

    I have many web assets that you can check.
     
  4. devrandomzero

    devrandomzero

    Joined:
    Aug 2, 2020
    Posts:
    43
    Thank you guys for the help