- Home /
Managing Scenes and reusable code
I think I've been too clever for my own good.
I've created a basic sliding cube game and in an attempt to make it reusable I have tried instantiate a lot of the blocks based on a text file with the intention of being able to generate different scenes using the same code but swapping out the config files.
var fileText = File.ReadAllText("Assets/Config/ObjectEditor.txt");
line = fileText.Split("\n"[0]);
var savedObj:GameObject = null;
var objCount = 0;
var randCube = new Array ();
for(var i = 0; i < (line.Length); i++) {
dataArray = line[i].Split(","[0]);
var posX = (parseFloat(dataArray[1]));
var posY = (parseFloat(dataArray[2]));
var posZ = (parseFloat(dataArray[3]));
var objRotationX = (parseFloat(dataArray[4]));
var objRotationY = (parseFloat(dataArray[5]));
var objRotationZ = (parseFloat(dataArray[6]));
var objID = ((dataArray[7]));
if ((dataArray[0] == "Cube")) {
savedObj = Cube;
}
if ((dataArray[0] == "Blank")) {
savedObj = Blank;
}
// Spawn Object
// Debug.Log('pong ' + posX + posY + posZ + objRotationX + objRotationY + objRotationZ);
var obj = Instantiate(savedObj, Vector3(posX, posY, posZ), Quaternion.Euler(objRotationX, objRotationY, objRotationZ));
objCount++;
obj.name = (dataArray[0] + "");
if(savedObj == Cube){
var pepper = "9x9_ct_" + objID;
var flib = parseInt(objID)-1;
var textures : Object[] = UnityEngine.Resources.LoadAll("Textures", Texture2D);
var texture : Texture2D = textures[flib];
obj.GetComponent(CubeScript).CubeID = (parseInt(objID));
obj.renderer.material.mainTexture = texture;
Debug.Log(flib);
}
Now I have started to implement other scenes I noticed that the asset folder is shared. As far as I can tell this means I will need to have to rename the files and edit a new copy of the script for each folder, which seems wasteful and calls for a lot of duplication.
Is there another way I can manage the scenes so I only need to use new copies of config files rather than duplicating all the main code?
I'm afraid I didn't quite understand the question... if your script reads the config file, why would you need to duplicate the code?
I do have a few pointers:
You can create the scenes in Unity, then save them, and load them with Application.LoadLevel.. seems a lot easier to me.
You can use TextAsset to handle text files ins$$anonymous$$d of the C# built-in libraries, I find this to be simpler at times.
The first line of the above code points to the config file.
var fileText = File.ReadAllText("Assets/Config/ObjectEditor.txt");
I (incorrectly) presumed that the scene file and config files would have their own folder structures so they would relate to each other and there would be no problems having multiple, different, ObjectEditor.txt files as they would be in different locations. As they are in a flat folder structure I can only have one file called ObjectEditor.txt.
Well you don't have to have a flat folder structure... just create folders called Scene1, Scene2, Scene3 etc. and place the relevant ObjectEditor.txt file in each. Doesn't that solve it?
Answer by jdyeager · Oct 15, 2013 at 09:35 PM
If I understand what you're trying to do, I think you would just need to:
Make the path to the config file a public variable in your script
Place the rest of your code in the Awake method of your script
Attach your script to an object in each of your scenes
Change the config file path in the Inspector on that object in each scene