Skip to content

Guidelines on working with Configuration File (WIP)

Goldenheaven edited this page Mar 2, 2014 · 1 revision

using UnityEngine; //Telling unity its going to use its functions

#if UNITY_EDITOR // From the start of the if to the end of the if, it will only run this code using UnityEditor; // if its running Unity Editor and not a build/release executable. #endif

using System.Collections; // This is telling unity to gather the collection types which are using System.Collections.Generic; // types of object that contain multiple objects of a user defined type

using Xml = System.Xml; // Since we want to write a xml file we will call a class

using ConfigWriter = System.Xml.XmlWriter; // Tells unity ConfigWriter is of System.Xml's XmlWriter type

using ConfigReader = System.Xml.XmlDocument; // Saves ConfigReader as type of XmlDocument

public class Config : MonoBehaviour { // Unity reads this as script config uses mono functionality begins

`private string configPath = "Assets/config.xml";` // Saves text in configPath for later use

`static public Dictionary<string, string[]> colorsDictionary = new Dictionary<string, string[]>();`
    //This creates a Dictionary that will be used to store colors.

`void Start () {`  // When the config script starts being used start doing the code bellow

             // Verifies if files exist on configPath (the folder path we set config file to save
             // and returns the answer if true or false, then if it is false executes the code
             // that is inside the {}
	`if (System.IO.File.Exists (configPath) == false){` 

                    // Creates a new xml file writer
		`Xml.XmlWriterSettings settings = new Xml.XmlWriterSettings();` 

		`settings.Indent = true;`//Set file indentation

		`string levelsPath = "Assets/scenes/";` //Set the path to where levels are stored

                    //Gets the name of each level and saves it
		`string[] loaderPath = System.IO.Directory.GetFiles (levelsPath);`

		`ConfigWriter cfgFile = ConfigWriter.Create(configPath,settings);`
		`cfgFile.WriteStartElement("Config");`
		`cfgFile.WriteStartElement("Levels");`
		`cfgFile.WriteAttributeString ("LevelsPath",levelsPath.ToString());`
		
		`foreach (string file in loaderPath){`
			
			`if (file.EndsWith(".meta") == false)`
			`{`
				`cfgFile.WriteElementString("Map",file.Remove(0,levelsPath.Length).Remove(file.Length-(levelsPath.Length+6)).ToString());`
			`}`
			
		`}`
		
		`cfgFile.WriteEndElement();`
		`cfgFile.WriteEndElement ();	`
		`cfgFile.WriteEndDocument();`
		`cfgFile.Flush ();`
		`cfgFile.Close ();`
	`}`
	`else`
	`{`
		`if ( colorsDictionary.Count > 0 )`
			`return;`

		`//load colors `
		`ConfigReader cfgFile = new ConfigReader ();`
		`cfgFile.Load (configPath);`

		`Xml.XmlNodeList colorNodes = cfgFile.GetElementsByTagName ("Color");`
		`foreach (Xml.XmlElement nodes in colorNodes)`
		`{`
			`colorsDictionary.Add( nodes.GetAttribute("colorName"), new string[]{`
															  `nodes.GetAttribute("one"),`
															  `nodes.GetAttribute("two"), `
															  `nodes.GetAttribute("three"), `
															  `nodes.GetAttribute("four")  });`
		`}`
	`}`

	`//Debug.Log ( "config says hello");`
`}`

`public void cfgLoadLevels(){`
	`ConfigReader cfgFile = new ConfigReader ();`
	`cfgFile.Load (configPath);`
	
	`LevelSelect levelSel = GetComponent<LevelSelect>();`
	`Xml.XmlNodeList mapNode = cfgFile.GetElementsByTagName ("Map");`
	
	`int mapCount = mapNode.Count;`
	
	`levelSel.levels = new string[mapCount];`
	`int itn = 0;`
	
	`foreach (Xml.XmlElement nodes in mapNode){`
		`levelSel.levels[itn] = nodes.InnerText;`
		`itn = itn+1;`
	`}`

#if UNITY_EDITOR /*

	`string[] levelsPath = new string[levelSel.levels.Length];`


	`EditorBuildSettingsScene[] newSettings = new EditorBuildSettingsScene[levelSel.levels.Length];`
	
	`for (int i = 0; i < levelsPath.Length; i++) {`
		`levelsPath[i] = levelSel.pathToLevels+"/"+levelSel.levels[i]+".unity";	`
		`EditorBuildSettingsScene sceneL = new EditorBuildSettingsScene(levelsPath[i],true);`
		`newSettings[i]=sceneL;`
	`}`
	
	`EditorBuildSettings.scenes = newSettings;`

*/ #endif

`}`

}