I am trying to create a small recycling game while off University for summer.
I have a script (mainMenu.cs) that will create an XML file when one isn’t detected, once it it created it will then create a new directory on the current platforms local path.
After that it will copy a series of images from “Resources/Images” to the new directory to then load into a List (in the imageBuffer.cs) to then be used in the game.
Now the code seems to work fine in windows but once exported over to iOS the game object that is supposed to have an image from the list, has nothing. I have taken into consideration the fact that it is on iOS but I don’t know any way to debug on the phone once my project is on there.
I know that the XML is being created fine because it reads some data to the GUIText on the main menu screen.
mainMenu.cs
using UnityEngine;
using System;
using System.Xml;
using System.Collections;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.IO;
public class PlayerData
{
public int ID {get;set;}
public string Name {get;set;}
public int Score {get;set;}
}
public class mainMenu : MonoBehaviour
{
private GameObject loadImages;
private imageBuffer ib;
public GUIText player1, player2, player3;
public Texture btnPlay,btnOptions,btnQuit;
void OnGUI()
{
//load the game
if(GUI.Button(new Rect(Screen.width-150,50,100,30),"Play Game"))
{
Application.LoadLevel(1);
}
//delete the XML file(if it exists) for the current device
if(GUI.Button(new Rect(200,100,100,30),"Delete XML"))
{
#if UNITY_STANDALONE_WIN
File.Delete(Environment.CurrentDirectory+"/PlayerData.xml");
#endif
#if UNTIY_IPHONE
File.Delete(Application.persistantDataPath+"/PlayerData.xml");
#endif
}
}
void Awake()
{
loadImages = GameObject.Find("dontDestroyOnLoad");
ib = (imageBuffer)loadImages.GetComponent(typeof(imageBuffer));
CheckXML();
}
void CheckXML()
//pre: Game is started
//post: Checks to see if XML exists, if not, create it
{
string path = null;
//check if windows EXE(works in play mode also) or iPhone, then path becomes devices local path
#if UNITY_STANDALONE_WIN
path = Environment.CurrentDirectory;
#endif
#if UNITY_IPHONE
path = Application.persistentDataPath;
#endif
if(!File.Exists(path + "/PlayerData.xml"))
{
//file doesn't exist, create XML file, Move start off images to folder, read the XML data
CreateNewXML(path);
MoveBasicGameImages(path);
ReadPlayerXML(path);
ib.LoadImages();
}
else
{
//file existsm read the XML
Debug.Log("Already Created");
ReadPlayerXML(path);
}
}
void CreateNewXML(string pathIn)
//pre: XML file has not been created
//post: will create XML file and move basic game images
{
//list of all the player scores and default data to add
List<PlayerData> allPlayers = new List<PlayerData>();
allPlayers.Add(new PlayerData() {ID = 1,Name = "AJC",Score = 50});
allPlayers.Add(new PlayerData() {ID = 2,Name = "AAA",Score = 35});
allPlayers.Add(new PlayerData() {ID = 3,Name = "BBB",Score = 20});
//make new XML file writer and store in device local path
XmlWriter writer = new XmlTextWriter(pathIn + "/" + "PlayerData.xml",new System.Text.UTF8Encoding(false));
if(writer != null)
{
//serialize the data from above and close
XmlSerializer serializer = new XmlSerializer(typeof(List<PlayerData>));
serializer.Serialize(writer, allPlayers);
writer.Close();
}
}
void MoveBasicGameImages(string pathIn)
//pre: XML has just been created
//post: copy basic game images to destination folder
{
string imagePath = null;
for(int i=0; i<=4;i++)
{
switch(i)
{
case 0:
imagePath = "Metal/";
break;
case 1:
imagePath = "Plastic/";
break;
case 2:
imagePath = "Paper/";
break;
case 3:
imagePath = "None-Recyclable/";
break;
case 4:
imagePath = "Glass/";
break;
}
//create the folders in for the images to copy to
Directory.CreateDirectory(pathIn+"/Images/"+imagePath);
string currentPath = pathIn+"/Assets/Resources/Images/"+imagePath;
string targetPath = pathIn+"/Images/"+imagePath;
//make string array of the path of all the images
string[] picList = Directory.GetFiles(currentPath);
//copy the images
foreach(string f in picList)
{
//Remove path from filename
string fName = f.Substring(currentPath.Length);
//Copy files, combime the fileName to the path's, overwrite if exists
File.Copy(Path.Combine(currentPath,fName),Path.Combine(targetPath,fName),true);
}
}
}
void ReadPlayerXML(string pathIn)
//pre: Either XML has just been created or it is already created
//post: Just print xml data for now
{
StreamReader readXML = new StreamReader(pathIn+"/PlayerData.xml");
if(readXML != null)
{
XmlSerializer playerReader = new XmlSerializer(typeof(List<PlayerData>));
List<PlayerData> allPlayers = (List<PlayerData>)playerReader.Deserialize(readXML);
readXML.Close();
//just a test before real game
foreach(PlayerData player in allPlayers)
{
if(player.ID == 1)
player1.text = "Name: " + player.Name + " Score: " + player.Score;
if(player.ID == 2)
player2.text = "Name: " + player.Name + " Score: " + player.Score;
if(player.ID == 3)
player3.text = "Name: " + player.Name + " Score: " + player.Score;
}
}
}
}
imageBuffer.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
public class imageBuffer : MonoBehaviour
{
#region: image paths
public WWW www = null;
private string imagePath = null;
public List<Texture2D> metalImages;
public List<Texture2D> plasticImages;
public List<Texture2D> paperImages;
public List<Texture2D> glassImages;
public List<Texture2D> noneImages;
#endregion
void Awake()
{
DontDestroyOnLoad(this);
metalImages = new List<Texture2D>();
plasticImages = new List<Texture2D>();
paperImages = new List<Texture2D>();
glassImages = new List<Texture2D>();
noneImages = new List<Texture2D>();
//find what system we are on
FindImagePath();
//load the images
//StartCoroutine(LoadImages());
}
void FindImagePath()
{
#if UNITY_STANDALONE_WIN
imagePath = Environment.CurrentDirectory + "/Images/";
#endif
#if UNITY_IPHONE
imagePath = Application.persistentDataPath + "/Images/";
#endif
}
public IEnumerator LoadImages()
{
string pictureFolder = null;
for(int i=0; i<=4;i++)
{
switch(i)
{
case 0:
pictureFolder = "Metal/";
break;
case 1:
pictureFolder = "Plastic/";
break;
case 2:
pictureFolder = "Paper/";
break;
case 3:
pictureFolder = "None-Recyclable/";
break;
case 4:
pictureFolder = "Glass/";
break;
}
string currentPath = imagePath+pictureFolder;
string[] picList = Directory.GetFiles(currentPath);
//copy the images
foreach(string f in picList)
{
//Remove path from filename
string fName = f.Substring(currentPath.Length);
string fullFileName = "file:///"+currentPath + fName;
www = new WWW(fullFileName);
yield return www;
Texture2D texTemp = www.texture;
//add to List<>
switch(i)
{
case 0:
metalImages.Add(texTemp);
break;
case 1:
plasticImages.Add(texTemp);
break;
case 2:
paperImages.Add(texTemp);
break;
case 3:
glassImages.Add(texTemp);
break;
case 4:
noneImages.Add(texTemp);
break;
}
}
}
}
}
And the gameObject’s script that is trying to use the image from the List
rubbishScript.cs
using UnityEngine;
using System.Collections;
using System.Linq; //for using .Cast
public class RubbishScript : MonoBehaviour
{
public int rotateSpeed = 100;
private Camera mainCam;
private GameObject myImageBuffer;
private gameLogic gl;
private imageBuffer ib;
// Use this for initialization
void Start ()
{
mainCam = Camera.mainCamera;
myImageBuffer = GameObject.Find("dontDestroyOnLoad");
gl = (gameLogic)mainCam.GetComponent(typeof(gameLogic));
ib = (imageBuffer)myImageBuffer.GetComponent(typeof(imageBuffer));
GetTexture();
}
void GetTexture()
{
Texture2D myTexture = null;
switch(gameObject.tag.ToString())
{
case "redBin":
myTexture = ib.metalImages[Random.Range(0, ib.metalImages.Count())];
break;
case "blueBin":
myTexture = ib.paperImages[Random.Range(0, ib.paperImages.Count())];
break;
case "greenBin":
myTexture = ib.glassImages[Random.Range(0, ib.glassImages.Count())];
break;
case "purpleBin":
myTexture = ib.plasticImages[Random.Range(0, ib.plasticImages.Count)];
break;
case "default":
Debug.Log("err");
break;
}
gameObject.renderer.material.mainTexture = myTexture;
}
void OnTriggerEnter(Collider other)
{
if(gl.currentBinActive.tag == gameObject.tag)
gl.ScoreUpdate(1);
else
gl.ScoreUpdate(-1);
DestroyObject(gameObject);
}
}