- Home /
Stuck on splash screen on Android only
I have a very weird problem that I've been trying to resolve for hours. I get stuck on the Unity splashscreen when running the game in an Android device. I've tried standalone platforms and WebPlayer and it worked fine.
Since June, I stopped building on platforms and focused on testing in the editor only. But before that, I can build just fine in Android. I am actively updating the project to the newest Unity 5.x (if that matters)
Here's the "technical" explanation of what's happening whenever the game is loaded:
The game only has one scene. No need for Application.LoadLevel
On Awake of the objects that are preloaded in the scene, some of my scripts are creating instances of prefabs. This instances will contain initial/default data.
I have a script called GameLoader which is the entry point of all my code (like a bootstrap)
On Start of GameLoader, it starts a coroutine which initializes the modules of my game step-by-step.
The first process is to load the save file. The save file is in JSON stored in PlayerPrefs as string. This will be parsed into a JToken (Json.NET). So far, the game still loads up to this point.
The next process in loading the save file is to deserialize the json into Unity objects. I wrote my own script for this. In this process, I use System.Type.GetType and System.Type.GetInterface to check if a gameobject has a component which implements my serialization interface. This process will update ALL existing objects.
Once everything's deserialized, I initialize all modules of the game since they already have data to work on.
Lastly, I enable the UI (main canvas).
I tested this by interrupting the code one by one (using return) until the game loads. Whenever I comment out or skip out the function which loads the save file, the game loads. However, like I said earlier, this was working before.
I'm thinking if Android has the watchdog timer (in iOS) which kicks the app out whenever you're doing something heavy on startup. Although it takes less than a second to load the save file and deserialize it. I'm not sure if something changed in 5.1 and 5.2 that turned my code into an undocumented bug or if it's related to the android SDK I've installed.
Here's my serialization code which bridge the Json parser and Unity API. It might ring a bell. If you need more information, please say so. Thanks.
public static GameObject Deserialize(GameObject go, JObject json, bool includeChildren = false)
{
go.name = (string)json["go_name"];
IEnumerable<string> keys = json.Properties().Select(p => p.Name).ToList();
foreach (string key in keys)
{
try
{
// Check if type exists
Type type = Type.GetType(key, true);
// Check if type is serializable
if (type.GetInterface(typeof(IJsonSerializable).ToString()) == null) continue;
// Create component and add it to the GameObject if it's not created yet
IJsonSerializable component = go.GetComponent(type) as IJsonSerializable;
if (component == null) component = go.AddComponent(type) as IJsonSerializable;
// Deserialize
if (json[key] != null) component.Deserialize(json[key]);
}
catch
{
// Type does not exist, continue to the next json object
continue;
}
}
if (includeChildren)
{
// Deserialize child game objects
JToken children = json["go_children"];
if (children != null)
{
foreach (JObject child in children)
{
GameObject childGo = Deserialize(child);
childGo.transform.SetParent(go.transform);
}
}
}
return go;
}