I use a ScriptableObject asset to hold the references of textures.
public class Bundle : ScriptableObject {
public Texture2D[] textures;
}
And I create an asset at “Resources/Bundle.asset”.
When in play mode, I load the “Bundle.asset” dynamicly. Then all textures could be referenced.
bundle = Resources.Load("Bundle") as Bundle;
I have a large number of textures in it. (Over 2000).
It works well in Unity Editor. But when I put it onto an android phone (I have tried 3 different type of phones), it crashes and gives this error by “adb logcat”
D/Unity (29832): NativeFile: fopen('/data/app/com.xd.LoadBundle-2.apk', rb) failed - Too many open files(24)
It’s strange that if I referenced the “bundle” in an Monobehavior in the scene. It won’t crash.
I guest there is something wrong during “Resources.Load”. It must work differently then “Application.LoadLevel”.
I have been struggling on this issue for days and could not find a clue.
Could anyone help me on this?
I made a testcase in the attachment that you might reproduce the issue.
You need:
- Menu->Create->BatchMakeTextures to create 2000 64x64 textures.
- Select Resources/Bundle and click “Reload Textures” in inspector. It assigns the textures in bundle.textures
- Debug on android with scene “LoadBundle1”
- When you click the “load” button on the phone, it should crash.
- adb logcat -s Unity could tell you the error.
BTW:
I prefer scriptableObject for 3 reasons:
- ScriptableObject is simple to serialize/deserialize in both edit mode & play mode. I don’t have to use xml or json.
- ScriptableObject saves runtime references. Ex. I could put a texture or a prefab in it.
- ScriptableObject could be nested to create complicated data structure.