Comments (10)
Looked at the Slippy
example.
First I got: InvalidTokenException: Please configure your access token in the menu!
But StreamingAssets
exists in the generated output.
I applied this patch:
diff --git a/sdkproject/Assets/Mapbox/Unity/MapboxAccess.cs b/sdkproject/Assets/Mapbox/Unity/MapboxAccess.cs
index ff6e2a7..611fe9a 100644
--- a/sdkproject/Assets/Mapbox/Unity/MapboxAccess.cs
+++ b/sdkproject/Assets/Mapbox/Unity/MapboxAccess.cs
@@ -93,7 +93,7 @@ namespace Mapbox.Unity
private void ValidateMapboxAccessFile()
{
-#if !UNITY_ANDROID
+#if !UNITY_ANDROID && !UNITY_WEBGL
if (!Directory.Exists(Application.streamingAssetsPath) || !File.Exists(_accessPath))
{
throw new InvalidTokenException("Please configure your access token in the menu!");
@@ -107,7 +107,7 @@ namespace Mapbox.Unity
/// </summary>
private void LoadAccessToken()
{
-#if UNITY_EDITOR || !UNITY_ANDROID
+#if UNITY_EDITOR || (!UNITY_ANDROID && !UNITY_WEBGL)
AccessToken = File.ReadAllText(_accessPath);
#else
AccessToken = LoadMapboxAccess();
and the token seems to load fine now.
However the WebGL page has been loading for several minutes (+8) now.
Chrome's task manager shows (CPU constantly changing between ~26% and 33%):
and I'm not even able to close the tab.
Will try a lighter example next.
from mapbox-unity-sdk.
Playground/RasterTile
example:
- Same behavior as above.
- Tried a development build.
- same behavior as above, except that the tab now uses almost 1.3GB memory
- last message I see in the console:
warning: 2 FS.syncfs operations in flight at once, probably just doing extra work
from mapbox-unity-sdk.
I think the problem is with WWW.isDone
in LoadMapboxAccess()
.
isDone
never becomes true and also the backup timeout doesn't seem to kick in, so the app is caught in that loop.
If I remove the isDone
check I get an UnityException: WWW is not ready downloading yet
.
Even copying Unity's own Application.streamingAssetsPath
example I get the same exception:
public class script1 : MonoBehaviour
{
private string _accessPath;
void Start()
{
_accessPath = Path.Combine(Application.streamingAssetsPath, "MapboxAccess.text");
#if !UNITY_ANDROID && !UNITY_WEBGL
Debug.Log("called on (almost) all players");
#else
Debug.Log("called on Android and WebGL");
#endif
#if UNITY_EDITOR || (!UNITY_ANDROID && !UNITY_WEBGL)
Debug.Log("UNITY_EDITOR || (!UNITY_ANDROID && !UNITY_WEBGL)");
#else
Debug.Log("ELSE - UNITY_EDITOR || (!UNITY_ANDROID && !UNITY_WEBGL)");
#endif
Debug.Log("_accessPath:" + _accessPath);
IEnumerator e = GetToken();
e.MoveNext();
e.MoveNext();
}
IEnumerator GetToken()
{
#if UNITY_EDITOR || (!UNITY_ANDROID && !UNITY_WEBGL)
yield return null;
Debug.Log("token loaded (via 'File'): " + File.ReadAllText(_accessPath));
#else
WWW www = new WWW(_accessPath);
yield return www;
Debug.Log("token loaded (via 'WWW'): " + www.text);
#endif
}
}
from mapbox-unity-sdk.
I think isDone
and how we use it is the root of our problem.
Unity docs about WWW.isDone
:
You should not write loops that spin until download is done; use coroutines instead.
While our approach works on Android it does not work with WebGL.
With the above patch in place I just hard coded my token and now WebGL works 🎉
So far tried:
- RasterTile
- Slippy
It's great to see memory caching working nicely with those two examples.
from mapbox-unity-sdk.
@BergWerkGIS Did you try StartCoroutine(GetToken())
?
from mapbox-unity-sdk.
Also, now that I see this example, we might be able to greatly simplify this class (logic).
from mapbox-unity-sdk.
Did you try StartCoroutine(GetToken())?
Yes, tiles start loading before token is availabe -> 401s - Not authenticated.
I based GetToken
on the example you linked.
from mapbox-unity-sdk.
After fiddling around with this for a while, I think we have at least three possible paths forward:
- Cache requests until the file has been read then submit through the properly configured
FileSource
- Add an
Initialized
event toMapboxAccess
and expect clients to subscribe to that before making requests - Instruct developers to hardcode their tokens in
MapboxAccess
, specifically for WebGL builds
The first option is more difficult, because geocoding and directions requests would need separate queueing systems. The second is less friendly, but generally safer.
Do you see other options?
from mapbox-unity-sdk.
As the Unity docs (WebGL Networking) explicitly state, we shouldn't use !isDone()
:
Do not block on WWW or WebRequest downloads
Do not use code which blocks on a WWW or WebRequest download, like this:
while(!www.isDone) {}
Blocking on WWW or WebRequest downloads does not work on Unity WebGL. Because WebGL is single threaded, and because the XMLHttpRequest class in JavaScript is asynchronous, your download never finishes unless you return control to the browser; instead, your content deadlocks. Instead, use a Coroutine and a yield statement to wait for the download to finish.
I tried everything I could think of to make that WWW
or UnityWebRequest
call within a coroutine and make it sync
, but failed.
Another hint from @david-rhodes 🙏 seems more promising: Resources.Load()
.
I tried Slippy
example successfully with
- Unity Editor
- Windows native x64
- WebGL
- Android
Branch: https://github.com/mapbox/mapbox-unity-sdk/tree/BergWerkGIS-fix-mapboxaccess-for-webgl
Changes
- moved
StreamingAssets/MapboxAccess.text
toResources/MapboxAccess/
- renamed
Resources/MapboxAccess/MapboxAccess.text
toMapboxAccess.txt
to make Unity recognize it as aTextAsset
- simplified
MapboxAccess.cs
TODO
- haven't commited the new
.meta
files as I'm on a Unity Beta version - change Editor dialog to save to
Resources/MapboxAccess/MapboxAccess.txt
from mapbox-unity-sdk.
Done: #109
Let's create dedicated issues if single examples fail.
from mapbox-unity-sdk.
Related Issues (20)
- Unexpected behaviour on SetZoom HOT 1
- The 3D models on the map change their position when locating is activated.
- Setting the Map Extent Options to CameraBounds at runtime doesn't work HOT 1
- Spawn prefabs upwards with a fixed gap
- why only mapbox street v7 can edit POI prefab
- Mapbox + Unity Cloud Build
- Error: Cannot Initialize non-default texture with negative or zero width HOT 1
- suddort URP ? HOT 2
- How to Apply Texture to Tiles
- Swapped North/South and West/East coordinates in Tile Id To Bounds?
- This project is dead? HOT 12
- Your hardware does not support this application. Failed to load 'libmain.so' java.lang.UnsatisfiedLinkError: dopen failed: library "libmain.so" not found HOT 1
- Can I make changes to AbstractMap's Features?
- Can I make changes to AbstractMap's Map Layers Tileset ID via code during runtime?
- heatmap rendering issue
- Meta Quest 3 Build Issues
- Some Building Meshes get "cutoff"
- Mapbox building:levels property support of Unity SDK HOT 3
- language support for raster tile
- Unity - Mapbox environment "Profiles"
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mapbox-unity-sdk.