//Assigned a StellarObjectTemplate to the dictionary according to a string. public static void SetTemplate(string templateName, StellarObjectTemplate template) { //As long as the dictionary isn't null, continue. if (stellarObjectTemplateDict != null) { //Check if the dictionary already contains the given string. if (stellarObjectTemplateDict.ContainsKey(templateName)) { //If it does, replace it with the given template. stellarObjectTemplateDict[templateName] = template; } else { //Else, assign the template normally. stellarObjectTemplateDict.Add(templateName, template); } } else { //If the dictionary is null, the class hasn't been initialized yet. Debug.Log("Attempting to set material in uninitialized StellarObjectTemplateDict."); } }
//Builds the StellarObjectTemplate dictionary from the XML database. public static void BuildStellarObjectTemplateDict() { //First check if the dictionary is already built. if (!stellarObjectTemplateDictBuilt) { //Try to open StellarObjectDefs.xml try { reader = new XmlTextReader("Assets/Resources/Defs/StellarObjectDefs.xml"); } catch { //Catch the file not being found. Debug.Log("Could not find StellarObjectDefs.xml"); return; } //Continue reading the file until the end. while (reader.Read()) { //Check if the read data is a start element. if (reader.IsStartElement()) { //If it is, determine what variable it's referring to, and assign it. switch (reader.Name.ToString()) { //Start building a new StellarObjectTemplate. case "StellarObjectTemplate": workStellarObjectTemplate = new StellarObjectTemplate(); break; //Assign the graphicName. case "graphicName": workStellarObjectTemplate.SetGraphicName(reader.ReadString()); break; //Build the template's material. case "graphicPath": workMat = new Material(Shader.Find("Sprites/Default")); workMat.mainTexture = Resources.Load(reader.ReadString()) as Texture2D; workMat.mainTexture.filterMode = FilterMode.Bilinear; MatDict.SetMaterial(workStellarObjectTemplate.graphicName, workMat); break; } } else { //Otherwise, check if the node is an end element. if (reader.NodeType == XmlNodeType.EndElement) { //Check if this is the end of the template. if (reader.Name.Equals("StellarObjectTemplate")) { //If it is, finalize the template and add it to the dictionary. workStellarObjectTemplate.Finalized(); StellarObjectTemplateDict.SetTemplate(workStellarObjectTemplate.graphicName, workStellarObjectTemplate); } } } } //Ensure the file is closed after reading, and mark the StellarObjectTemplate dictionary as built. reader.Close(); stellarObjectTemplateDictBuilt = true; } else { //If it's already built, something went wrong. Debug.Log("Attempting to build already built StellarObjectTemplateDict."); } }