Esempio n. 1
0
    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);
    }
Esempio n. 4
0
        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 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");
    }