MapboxAccess does not work on Android
@david-rhodes
Problem 1
https://docs.unity3d.com/Manual/StreamingAssets.html
On Android, the files are contained within a compressed .jar file (which is essentially the same format as standard zip-compressed files). This means that if you do not use Unity’s WWW class to retrieve the file, you need to use additional software to see inside the .jar archive and obtain the file.
Currently ValidateMapboxAccessFile() does not use WWW to check if the token file exists
if (!Directory.Exists(Application.streamingAssetsPath) || !File.Exists(_accessPath)) {
throw new InvalidTokenException("Please configure your access token in the menu!");
}
and fails, even if it is there, and LoadAccessToken() never gets called.
Problem 2
LoadMapboxAccess() is "async" and by the time the token has been retrieved all tile requests (18 in my test app, based on example MeshGenerationStyles) have already been fired (without a token) and fail.
Commenting validation and hard coding the token works:

~~Although there seem to be other problems too as buildings and their styles are still missing.~~
EDIT
After commenting ValidateMapboxAccessFile() and hard coding the token the rest works as expected:

@BergWerkGIS Yeah, since I don't have Android I was unable to test myself. I thought that @MateoV had tested to ensure everything was working as expected, though.
The actual loading of the text file is done with WWW, but as you mentioned ValidateMapboxAccessFile fails because the "file is not found" on Android. I think we should just #if !UNITY_ANDROID the contents of this method and do some extra error checking in IEnumerator LoadMapboxAccess().
@BergWerkGIS do you know if the directory is not able to be found, or if the file does not exist?
if (!Directory.Exists(Application.streamingAssetsPath) || !File.Exists(_accessPath)) {
throw new InvalidTokenException("Please configure your access token in the menu!");
}
Regarding Problem 2, we may have to hold a queue of requests until the async read is done, then fire them off. This seems like a simple fix, but I won't be able to test. Do you have a better idea?
do you know if the directory is not able to be found, or if the file does not exist?
I think Directory.Exists() and File.Exists() don't work with the url that points into the jar file.
Regarding
Problem 2, we may have to hold a queue of requests until the async read is done, then fire them off. This seems like a simple fix, but I won't be able to test. Do you have a better idea?
I would prefer to somehow make loading of the token sync. Otherwise all code that requests anything would have to check and wait till the token is available.
Will have a look tomorrow.
How about not using IEnumerator and yield but something like:
void LoadMapboxAccess() {
var request = new WWW(_accessPath);
while (!request.isDone) { Thread.Sleep(10); }
AccessToken = request.text;
}
How about not using IEnumerator and yield?
I never thought to use WWW like that, but should be possible. We would have to include a timeout or error handling to prevent freezing the entire application. Android is so fragmented, you never know what device or OS this will not work for :smirk:.
@david-rhodes and I tried a couple of solutions wanted to get your thoughts on these (sorry the diffs aren't super helpful here) Tested on and Android device and the styles and buildings demo loads. https://mapbox.slack.com/files/isiyu/F53553D55/image_uploaded_from_ios.mp4
@BergWerkGIS wanted to get your thoughts on which solution to go with for now
option A: cache requests https://github.com/mapbox/mapbox-unity-sdk/commit/3ffc117f9cffb3203feb59f4fdd22200f9fd9810
What the logs look like for a successful run, with a valid token:
- https://docs.google.com/a/mapbox.com/document/d/1zvyOtyqmvtUpcRL_FHoh_Wv_e-s1dqaiXFSmz_CAZTE/edit?usp=sharing
option B: thread sleep https://github.com/mapbox/mapbox-unity-sdk/commit/5125bcec42e2964012d7f7ab47b07fef42a905de
with no token:

For clarity, Option B works. Please note: #if !_UNITY_ANDROID should be #if !UNITY_ANDROID. Testing without a token was just to see what exception was thrown. The correct InvalidTokenException was thrown. 👍
@BergWerkGIS this was your suggestion, and it seems to work as intended. Any idea why your implementation did not work earlier today? In any case, we would probably want to implement a timeout for the while loop and throw an exception in the event the WWW never finishes.
As mentioned in slack, Option A is likely "safer," but it is less efficient and does not currently allow cached requests to be cancelled (but this is seriously an edge case). We can fix this, but I'd like to know your thoughts, first.
@BergWerkGIS this was your suggestion, and it seems to work as intended. Any idea why your implementation did not work earlier today?
No idea, maybe some error I missed during deploy and an old version was used on the device. Works today. 🎉
I'd like to know your thoughts, first.
I'd prefer Option B to caching at that level.
My PR for MapboxAccess.cs, including:
- Android fix
- timeout for
LoadMapboxAccess() - UWP support
ServerCertificateValidationCallbackto make HttpWebRequest on Mono work with HTTPS requests
Fixed: #26