- Home /
Android Streaming asset path won't open
Hello, I am working on a mobile platformer game, which going to have a lot of levels. So I decided to find a way where I could draw the level, and base on the color of the pixels a certain prefab would spawn in at that spot. So I used quill18's script, which suited my needs perfectly https://www.youtube.com/watch?v=5RzziXSwSsg So this works just fine in the Editor, but once I test it on my android phone, the map doesn't load. So I'm unsure as to why it can't find the image. I'm placing the images in a StreamingAssets folder as well.
public void LoadMap()
{
EmptyMap();
manager.currentLevel += 1;
levelFileName = manager.levels[manager.currentLevel].levelString;
// Read the image data from the file in StreamingAssets
string filePath = Application.streamingAssetsPath + "\\" + levelFileName;
byte[] bytes = System.IO.File.ReadAllBytes(filePath);
Texture2D levelMap = new Texture2D(2, 2);
levelMap.LoadImage(bytes);
// Get the raw pixels from the level imagemap
Color32[] allPixels = levelMap.GetPixels32();
int width = levelMap.width;
int height = levelMap.height;
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
SpawnTileAt(allPixels[(y * width) + x], x, y);
}
}
}
Any help at all would be greatly appreciated!
Answer by lamphung719 · Dec 22, 2016 at 03:39 AM
Have you checked whether the file is exist in that folder?
@lamphung719 Yes I have checked and the file I am trying to open does exsist. As I said it works just fine in the Editor, but it can't be found on the Android device. Perhaps I am wrong, but maybe when I build it as an apk, the file may get deleted. But I have also tested that by placing a UI image with the source as that map image. So it's still there, but the script can't find it for some reason
i mean in the device, did you check the file on the device, not the editor
@lamphung719 Yes, like I said I made a UI image with the source image as the pixel map, so I know the map image is still there because if it wasn't the Image with the map on it would be blank. But if you know of another way I could check please do share! I also think I need to use another way of opening the image on android, but don't know how to yet.
string filePath = Application.strea$$anonymous$$gAssetsPath + "\\" + levelFileName;
pretty sure this is my problem, I think I need to find the image another way for android.
Answer by edcx · Dec 23, 2016 at 07:27 AM
Streaming Assets documentation says
For Android:
path = "jar:file://" + Application.dataPath + "!/assets/";
Can you check with this path?
Still no luck. Do you know of another way I could try to do this, Ins$$anonymous$$d of using the strea$$anonymous$$g assets folder.
You should read the documentation more carefully. It clearly says that strea$$anonymous$$g assets are placed inside the AP$$anonymous$$ which essentially is a ZIP archive. The "jar:file:" URL scheme only works when using the WWW class. You can't use simply .NET file IO.
To quote the documentation:
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.
If you just want to include the image directly in your game, Just create a public variable of type Texture2D like he did in the first place. You can't edit the image once the AP$$anonymous$$ is deployed as it resides inside the AP$$anonymous$$. You could however load custom maps from other paths on the device.
Oh silly me, I wasn't really thinking at the time, yes putting all the textures into an array makes more sense and is much easier! Thanks for help!!!
Ins$$anonymous$$d of getting the actual files from Android, it's check for our PC ins$$anonymous$$d (debuggin using Unity Remote on my Android). Application.dataPath is pointing to my projects.
Is there any workaround to this?
Your answer
Follow this Question
Related Questions
Always ask to updating android SDK 4 Answers
One Signal dependecies issue 0 Answers
Can't install openJDK 1 Answer
Multiple errors: Gradle/SDK/JDK related. Unity android build 5 Answers
Issue building my game for Android 0 Answers