0
\$\begingroup\$

Currently, we have a project that runs on Windows and uses an Excel spreadsheet to save/retrive some data/information. Now we want to build that project for WebGL. The problem is that file reading support is not available on the WebGL platform. How do I solve this problem? Do I need to write a backend or there another way to use excel in WebGL platform even though the file reading API of the .NET framework is not supported on WebGL?

\$\endgroup\$
0
\$\begingroup\$

When your WebGL game needs to read an arbitrary file, then you can add it to the game build by turning it into a text asset. You need to rename it to give it the file extension .bytes, so Unity recognizes it as a binary file (for example: items.xlsx.bytes). To ensure that the file is included in the WebGL build, you need to store it in an Assets/Resources folder or in an asset bundle which you then export to your WebGL build.

If you decided to use resources:

TextAsset txt = Resources.Load("items.xlsx") as TextAsset;

If you decided to use an asset bundle:

AssetBundle bundle = myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "myassetBundle"));
TextAsset txt = bundle.Load("items.xlsx", typeof(TextAsset)) as TextAsset;

Now you can get the binary data from that text asset with

byte[] bytes = txt.bytes;

and feed it into the code you already have for parsing Excel workbooks.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You can also reference your TextAsset directly via a public or serialized member variable on a MonoBehaviour or ScriptableObject, to access it without the Resources.Load or AssetBundle steps. In the past, I've also exported the Excel files to .csv to parse in the game as text strings from TextAsset.text \$\endgroup\$ – DMGregory Jul 17 '20 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.