private DeserializedLevels.Level getCurLevel(string xmlfilename) { deserializedLevels = XmlIO.LoadXml <DeserializedLevels>(xmlfilename); int startLevel = int.Parse(deserializedLevels.developer.startLevel); return(deserializedLevels.levels[startLevel - 1]);; }
private DeserializedLevels.Level getCurLevel() { deserializedLevels = XmlIO.LoadXml <DeserializedLevels>("Levels"); // if startlevel is in the XML i.e. <Developer StartLevel="3" /> then get level from there // otherwise start with level 1 int startLevel = int.Parse(deserializedLevels.developer.startLevel); return(deserializedLevels.levels[startLevel - 1]);; }
private DeserializedLevels.Level getCurLevel() { deserializedLevels = XmlIO.LoadXml<DeserializedLevels>("Levels"); // if startlevel is in the XML i.e. <Developer StartLevel="3" /> then get level from there // otherwise start with level 1 int startLevel = int.Parse(deserializedLevels.developer.startLevel); return deserializedLevels.levels[startLevel - 1]; ; }
private static void getLevelPrefabs(HashSet <string> xmlPrefabSet) { DeserializedLevels deserializedLevels = XmlIO.LoadXml <DeserializedLevels>("Levels"); foreach (DeserializedLevels.Level level in deserializedLevels.levels) { foreach (DeserializedLevels.Item item in level.items) { xmlPrefabSet.Add(item.prefab); } } }
public void saveExportItems() { GameObject xmlItemsToExportGO; // Create XmlItemsToExport or find existing if (GameObject.Find(xmlItemsToExportGOName) == null) { xmlItemsToExportGO = new GameObject(xmlItemsToExportGOName); //we have nothing to save so skip execution return; } else { xmlItemsToExportGO = GameObject.Find(xmlItemsToExportGOName); } Transform[] xmlItemsToExportGOchildren = xmlItemsToExportGO.GetComponentsInChildren <Transform>(); // Check if there isn't any Transform components except parent's if (xmlItemsToExportGOchildren.Length == 1) { Debug.LogError("Add the prefabs to " + xmlItemsToExportGOName); return; } //create list of items List <DeserializedLevels.Item> itemList = new List <DeserializedLevels.Item>(); foreach (Transform item in xmlItemsToExportGOchildren) { if (item.parent == xmlItemsToExportGO.transform) { itemList.Add(new DeserializedLevels.Item(item)); } } //copy list of items to the raw array DeserializedLevels.Level levelXml = new DeserializedLevels.Level(); levelXml.items = new DeserializedLevels.Item[itemList.Count]; itemList.CopyTo(levelXml.items); // Export just one level DeserializedLevels levelsXmlToExport = new DeserializedLevels(); levelsXmlToExport.levels = new DeserializedLevels.Level[1]; levelsXmlToExport.levels[0] = levelXml; string outputFilePath = "./Assets/Resources/" + xmlItemsToExportGOName + ".xml"; XmlIO.SaveXml <DeserializedLevels>(levelsXmlToExport, outputFilePath); }
public void SaveExportItems() { GameObject xmlItemsToExportGO; // Create XmlItemsToExport or find existing if (GameObject.Find(xmlItemsToExportGOName) == null) { xmlItemsToExportGO = new GameObject(xmlItemsToExportGOName); //we have nothing to save so skip execution return; } else { xmlItemsToExportGO = GameObject.Find(xmlItemsToExportGOName); } Transform[] xmlItemsToExportGOchildren = xmlItemsToExportGO.GetComponentsInChildren <Transform>(); // Check if there are actually any objects childed to our export game object if (xmlItemsToExportGOchildren.Length == 1) { Debug.LogError("Add the prefabs to " + xmlItemsToExportGOName); return; } List <DeserializedLevels.Item> itemList = new List <DeserializedLevels.Item>(); foreach (Transform item in xmlItemsToExportGOchildren) { if (item.parent == xmlItemsToExportGO.transform) { itemList.Add(new DeserializedLevels.Item(item)); } } DeserializedLevels.Level levelXml = new DeserializedLevels.Level(); levelXml.items = new DeserializedLevels.Item[itemList.Count]; itemList.CopyTo(levelXml.items); // Export just one level, this is a simple XML level loading pipeline for now DeserializedLevels levelsXmlToExport = new DeserializedLevels(); levelsXmlToExport.levels = new DeserializedLevels.Level[1]; levelsXmlToExport.levels[0] = levelXml; string outputFilePath = "./Assets/Resources/" + xmlItemsToExportGOName + ".xml"; XmlIO.SaveXml <DeserializedLevels>(levelsXmlToExport, outputFilePath); }
public void saveExportItems() { GameObject xmlItemsToExportGO; // Create XmlItemsToExport or find existing if (GameObject.Find(xmlItemsToExportGOName) == null) { xmlItemsToExportGO = new GameObject(xmlItemsToExportGOName); //we have nothing to save so skip execution return; } else { xmlItemsToExportGO = GameObject.Find(xmlItemsToExportGOName); } Transform[] xmlItemsToExportGOchildren = xmlItemsToExportGO.GetComponentsInChildren<Transform>(); // Check if there isn't any Transform components except parent's if (xmlItemsToExportGOchildren.Length == 1) { Debug.LogError("Add the prefabs to " + xmlItemsToExportGOName); return; } //create list of items List<DeserializedLevels.Item> itemList = new List<DeserializedLevels.Item>(); foreach (Transform item in xmlItemsToExportGOchildren) { if (item.parent == xmlItemsToExportGO.transform) { itemList.Add(new DeserializedLevels.Item(item)); } } //copy list of items to the raw array DeserializedLevels.Level levelXml = new DeserializedLevels.Level(); levelXml.items = new DeserializedLevels.Item[itemList.Count]; itemList.CopyTo(levelXml.items); // Export just one level DeserializedLevels levelsXmlToExport = new DeserializedLevels(); levelsXmlToExport.levels = new DeserializedLevels.Level[1]; levelsXmlToExport.levels[0] = levelXml; string outputFilePath = "./Assets/Resources/" + xmlItemsToExportGOName + ".xml"; XmlIO.SaveXml<DeserializedLevels>(levelsXmlToExport, outputFilePath); }
// cross check /Resources/Prefabs and Levels.xml if there are any item prefabs that exist only in one but not the other public void crossCheck() { // create a list of /Resources/Prefabs for resources and Levels.Xml List <string> resPrefabList = new List <string>(); List <string> xmlPrefabList = new List <string>(); // Get prefabs from Levels.xml DeserializedLevels deserializedLevels = XmlIO.LoadXml <DeserializedLevels>("Levels"); foreach (DeserializedLevels.Level level in deserializedLevels.levels) { foreach (DeserializedLevels.Item item in level.items) { if (!xmlPrefabList.Contains(item.prefab)) { xmlPrefabList.Add(item.prefab); } } } // Get prefabs from the /Resources/Prefabs folder // get all child items in the /Resources/Prefabs folder DirectoryInfo dir = new DirectoryInfo("Assets/Resources/Prefabs"); FileInfo[] fileInfos = dir.GetFiles("*.prefab"); fileInfos.Select(f => f.FullName).ToArray(); // Add each prefab's file name to prefabList and truncate the .prefab extension from the end foreach (FileInfo fileInfo in fileInfos) { resPrefabList.Add(fileInfo.Name.Substring(0, fileInfo.Name.Length - ".prefab".Length)); } // Cross checks foreach (string prefab in xmlPrefabList.Except(resPrefabList).ToList()) { Debug.LogError(prefab + " is missing in the /Resorces/Prefabs folder but used in Levels.xml"); } foreach (string prefab in resPrefabList.Except(xmlPrefabList).ToList()) { Debug.Log(prefab + " exists in the /Resorces/Prefabs folder but not used in Levels.xml"); } Debug.Log("Cross Check Done"); }
public void generateItems(int startLevel = -1) { prefabPool = new Dictionary <string, GameObject>(); sceneItemsList = new List <GOProperties>(); // if the XmlItems gameobject folder remained in the Hierarcy, then delete it while (GameObject.Find(xmlItemsGameObjectName) != null) { MonoBehaviour.DestroyImmediate(GameObject.Find(xmlItemsGameObjectName)); } parentOfXmlItems = new GameObject(xmlItemsGameObjectName).transform; parentOfXmlItems.parent = GameObject.Find("GameContext").transform; deserializedLevels = XmlIO.LoadXml <DeserializedLevels>("Levels"); // if startlevel is in the XML i.e. <Developer StartLevel="3" /> then get level from there // otherwise start with level 1 if (startLevel == -1) { startLevel = int.Parse(deserializedLevels.developer.startLevel); } DeserializedLevels.Level currentLevel = deserializedLevels.levels[startLevel - 1]; // spagetti coding set player, camera position and level bounds setPos2D( GameObject.Find("PlayerView"), new Vector2(toFloatZeroIfNull(currentLevel.playerx), toFloatZeroIfNull(currentLevel.playery))); setPos2D( GameObject.Find("CameraView"), new Vector2(toFloatZeroIfNull(currentLevel.playerx), toFloatZeroIfNull(currentLevel.playery))); // set level's bounds #if UNITY_EDITOR try { #endif setLevelBounds( parentOfXmlItems, float.Parse(currentLevel.bound_x_min), float.Parse(currentLevel.bound_y_min), float.Parse(currentLevel.bound_x_max), float.Parse(currentLevel.bound_y_max)); #if UNITY_EDITOR } catch { Debug.LogError("Level Bounds must be set in levels.xml"); } #endif // <Item prefab="Chair" x="1" y="10" rot="90" /> foreach (DeserializedLevels.Item deserializedItem in currentLevel.items) { // caching prefabString i.e. "phone" string prefabString = deserializedItem.prefab; // if the prefab in the item XmlNode has not been loaded then add it to the prefabsDict dictionary, if (!prefabPool.ContainsKey(prefabString)) { // load prefab GameObject prefabObject = Resources.Load(prefabsFolder + prefabString, typeof(GameObject)) as GameObject; // if unsuccesful, error message and jump to next in the foreach loop if (prefabObject == null) { Debug.LogError("Prefab \"" + prefabString + "\" does not exists."); continue; } // otherwise add to dictionary prefabPool.Add(prefabString, prefabObject); } GOProperties item = new GOProperties(); item.prefab = prefabPool[prefabString]; item.x = toFloatZeroIfNull(deserializedItem.x); item.y = toFloatZeroIfNull(deserializedItem.y); item.rot = toFloatZeroIfNull(deserializedItem.rot); item.scale_x = toFloatOneIfNull(deserializedItem.scale_x); item.scale_y = toFloatOneIfNull(deserializedItem.scale_y); item.speed = toFloatOneIfNull(deserializedItem.speed); item.spotmedianangle = toFloatZeroIfNull(deserializedItem.spotmedianangle); sceneItemsList.Add(item); } // Finally instantiate all items foreach (GOProperties item in sceneItemsList) { // TODO load height coordinate from a directory GameObject newGameObject = MonoBehaviour.Instantiate(item.prefab) as GameObject; // set position setPos2D(newGameObject, new Vector2(item.x, item.y)); // set rotation setRot2D(newGameObject, item.rot); // set scale newGameObject.transform.localScale = new Vector3(item.scale_x, item.scale_y, 1); // set speed if (newGameObject.GetComponent <ShipPatrolView>() != null) { newGameObject.GetComponent <ShipPatrolView>().speed = item.speed; } if (newGameObject.GetComponent <SoldierWalkingView>() != null) { newGameObject.GetComponent <SoldierWalkingView>().speed = item.speed; } // set spotlight median angle if (newGameObject.GetComponentInChildren <SpotLightView>() != null) { newGameObject.GetComponentInChildren <SpotLightView>().spotMedianAngle = item.spotmedianangle; } // set parent newGameObject.transform.parent = parentOfXmlItems; } }
public ItemStruct(GameObject prefabGO, DeserializedLevels.Item deserializedItem) { prefab = prefabGO; x = toFloatZeroIfNull(deserializedItem.x); y = toFloatZeroIfNull(deserializedItem.y); rot = toFloatZeroIfNull(deserializedItem.rot); scale_x = toFloatOneIfNull(deserializedItem.scale_x); scale_y = toFloatOneIfNull(deserializedItem.scale_y); }
public void saveExportItems() { // Create XmlItemsToExport if does not exist yet if (GameObject.Find(xmlItemsToExportGOName) == null) { new GameObject(xmlItemsToExportGOName); } GameObject xmlItemsToExportGO = GameObject.Find(xmlItemsToExportGOName); var xmlItemsToExportGOchildren = xmlItemsToExportGO.GetComponentsInChildren <Transform>(); // Check if any children exist if (xmlItemsToExportGOchildren.Length == 0) { Debug.LogError("Add the prefabs to " + xmlItemsToExportGOName); } DeserializedLevels.Level levelXml = new DeserializedLevels.Level(); int n = 0; // count number of children skipping sub-items foreach (Transform item in xmlItemsToExportGOchildren) { if (item.parent == xmlItemsToExportGO.transform) { n++; } } // the items array should have that many elements levelXml.items = new DeserializedLevels.Item[n]; // use i for counting items, i would be equal (one more to be precise) to n at the end of the cycle int i = 0; // cycle through the children again and add them to items foreach (Transform item in xmlItemsToExportGOchildren) { // skip sub-items if (item.parent != xmlItemsToExportGO.transform) { continue; } levelXml.items[i] = new DeserializedLevels.Item(); levelXml.items[i].prefab = item.name; levelXml.items[i].x = toStringNullIfZero(item.transform.position.x); levelXml.items[i].y = toStringNullIfZero(item.transform.position.y); levelXml.items[i].rot = toStringNullIfZero(item.localRotation.eulerAngles.x); levelXml.items[i].scale_x = toStringNullIfOne(item.localScale.x); levelXml.items[i].scale_y = toStringNullIfOne(item.localScale.y); if (item.GetComponentInChildren <SpotLightView>() != null) { levelXml.items[i].spotmedianangle = toStringNullIfOne(item.GetComponentInChildren <SpotLightView>().spotMedianAngle); } if (item.GetComponent <ShipPatrolView>() != null) { levelXml.items[i].speed = toStringNullIfOne((float)item.GetComponent <ShipPatrolView>().speed); } if (item.GetComponent <SoldierWalkingView>() != null) { levelXml.items[i].speed = toStringNullIfOne((float)item.GetComponent <SoldierWalkingView>().speed); } // increase i for the next cycle i++; } // Export just one level DeserializedLevels levelsXmlToExport = new DeserializedLevels(); levelsXmlToExport.levels = new DeserializedLevels.Level[1]; levelsXmlToExport.levels[0] = levelXml; XmlIO.SaveXml <DeserializedLevels>(levelsXmlToExport, "./Assets/Resources/" + xmlItemsToExportGOName + ".xml"); }