- Home /
XML loading not working for built game
I have recently been undergoing a project and have hit a stop. I finished making the game completely in Unity where the game loads mostly from XML and saves the data. It works fine within the Unity window but when the game has been built and run, after going through the main menu, nothing loads into the game and I am left with a blank blue background. Is there something I missed that is needed for the built version?
Below is the AssetLoader class I am using. I also have a SceneAsset class and SceneAssetAggregate class, if that needs to be posted I will do so if need be.
using UnityEngine;
using System.Collections;
using System.Xml.Serialization;
using System.IO;
public class AssetLoader : MonoBehaviour {
public string filename;
// Use this for initialization
void Start () {
//Terrain Loader
GroundAsset ground = new GroundAsset("New Terrain 1");
ground.position = new Vector3(-1213, 0, -1358);
ground.scale = new Vector3(1, 1, 1);
//Building Loader
BuildingAsset building = new BuildingAsset ("Building_11");
building.position = new Vector3 (-16, 0, -8);
building.rotation = new Quaternion (-90, 0, 0, 0);
building.scale = new Vector3 (3, 3, 3);
//Key Loaders
KeyAsset key1 = new KeyAsset ("key");
key1.position = new Vector3 (4, 1, 5);
key1.scale = new Vector3 (2, 2, 2);
KeyAsset key2 = new KeyAsset ("key");
key2.position = new Vector3 (-43, 1, -8);
key2.scale = new Vector3 (2, 2, 2);
KeyAsset key3 = new KeyAsset ("key");
key3.position = new Vector3 (3, 3, -22);
key3.scale = new Vector3 (2, 2, 2);
KeyAsset key4 = new KeyAsset ("key");
key4.position = new Vector3 (-27, 2, 10);
key4.scale = new Vector3 (2, 2, 2);
//Terminal Loader
TerminalAsset terminal = new TerminalAsset ("Term01");
terminal.position = new Vector3 (-16, 0, -14);
terminal.scale = new Vector3 (2, 2, 2);
//3rd person controller Loader
PlayerAsset player = new PlayerAsset ("3rd Person Controller Level1");
player.position = new Vector3 (-16, 1, -16);
player.scale = new Vector3 (1, 1, 1);
//Crate Loaders
CrateAsset crate1 = new CrateAsset ("crate_2");
crate1.position = new Vector3 (0, 2, 5);
crate1.scale = new Vector3 (5, 5, 5);
CrateAsset crate2 = new CrateAsset ("crate_2");
crate2.position = new Vector3 (-26, 2, 5);
crate2.scale = new Vector3 (5, 5, 5);
SceneAssetAggregate list = new SceneAssetAggregate();
list.assets.Add(ground);
list.assets.Add (building);
list.assets.Add (key1);
list.assets.Add (key2);
list.assets.Add (key3);
list.assets.Add (key4);
list.assets.Add (player);
list.assets.Add (terminal);
list.assets.Add (crate1);
list.assets.Add (crate2);
XmlSerializer serializer = new XmlSerializer(typeof(SceneAssetAggregate));
FileStream writefile = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
serializer.Serialize(writefile, list);
writefile.Close();
FileStream readfile = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Read);
SceneAssetAggregate readAssets = (SceneAssetAggregate)serializer.Deserialize(readfile);
foreach(SceneAsset asset in readAssets.assets)
{
GameObject newGO = null;
switch (asset.assetType)
{
case SceneAssetType.Ground:
{
TerrainData t = (TerrainData)Instantiate(Resources.Load(ground.filename));
newGO = Terrain.CreateTerrainGameObject(t);
break;
}
case SceneAssetType.Building:
{
newGO = (GameObject)Instantiate(Resources.Load(building.filename));
break;
}
case SceneAssetType.Key1:
{
newGO = (GameObject)Instantiate(Resources.Load (key1.filename));
break;
}
case SceneAssetType.Key2:
{
newGO = (GameObject)Instantiate(Resources.Load (key2.filename));
break;
}
case SceneAssetType.Key3:
{
newGO = (GameObject)Instantiate(Resources.Load (key3.filename));
break;
}
case SceneAssetType.Key4:
{
newGO = (GameObject)Instantiate(Resources.Load (key4.filename));
break;
}
case SceneAssetType.Player:
{
newGO = (GameObject)Instantiate(Resources.Load (player.filename));
break;
}
case SceneAssetType.Terminal:
{
newGO = (GameObject)Instantiate(Resources.Load(terminal.filename));
break;
}
case SceneAssetType.Crate1:
{
newGO = (GameObject)Instantiate(Resources.Load (crate1.filename));
break;
}
case SceneAssetType.Crate2:
{
newGO = (GameObject)Instantiate(Resources.Load (crate2.filename));
break;
}
default: throw new System.NotImplementedException ();
}
newGO.transform.position = asset.position;
newGO.transform.rotation.Set(asset.rotation.x, asset.rotation.y, asset.rotation.z, asset.rotation.w);
newGO.transform.localScale= asset.scale;
}
}
// Update is called once per frame
void Update () {
}
}
what does output_log.txt say? its in you build_Data folder.
Answer by JockShack · Jan 05, 2015 at 05:19 PM
I managed to solve this by placing the Unity built exe and data files into the actual Unity project files.. From there it was able to load the XML data and the game was playable.
Answer by Testman · Apr 27, 2015 at 11:17 AM
(Mac OS X) I overcame this by placing my XML folder into the build's Resources folder. Right click -> Show package contents -> Resources.
Didn't need to alter my code at all.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Why is my script not working like it should 3 Answers
Baking with lightmap in very low resolution. 1 Answer
I am not able to see any object in scene. 1 Answer
Load file on Android 1 Answer