Пример #1
0
    void ReadTiled()
    {
        var sr = new StreamReader(Application.dataPath + TiledTMXFile);                                 // some String Games

        string FileLoad = "Assets" + TiledTMXFile;                                                      // Assets/etc/File.tmx
        string FilePath = FileLoad.Remove(FileLoad.LastIndexOf("/") + 1);                               // Assets/etc/
        string FileName = FileLoad.Remove(0, FileLoad.LastIndexOf("/") + 1);                            // quit folder path structure

        FileName = FileName.Remove(FileName.LastIndexOf("."));                                          // quit .xml extension

        FolderPath = FilePath + FileName + " Prefabs/";                                                 // Assets/Resources/FileName_Prefabs/
        string FileContent = sr.ReadToEnd();

        sr.Close();
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        XmlDocument Doc = new XmlDocument();

        Doc.LoadXml(FileContent);


        // CHECK IT S A TMX FILE FROM TILED
        if (Doc.DocumentElement.Name == "map")                                                                                                          // Access root Map
        {
            // REBUILD TMX OBJECT IN UNITY SCENE
            DestroyImmediate(GameObject.Find(FileName));                                                                                        // ReBuild the Layers Container & stuff.
            GameObject map = new GameObject(FileName);                                                                                          // inside the editor hierarchy.
            MapTransform = map.transform;                                                                                                       // take map transform cached

            //(map.AddComponent<LevelAttributes>() as LevelAttributes).bounds = new Rect(0, 0,			// Set Level bounds for camera
            //    int.Parse(Doc.DocumentElement.Attributes["width"].Value) * TileOutputSize.x,
            //    int.Parse(Doc.DocumentElement.Attributes["height"].Value) * TileOutputSize.y);
            //	map.AddComponent("CombineMeshes");

            if (PrefabRebuild && AssetDatabase.DeleteAsset(FolderPath.Remove(FolderPath.LastIndexOf("/"))))
            {
                Debug.Log(" Folder deleted & Rebuilded");
            }

            Directory.CreateDirectory(FolderPath);                                                                              // build a Folder to hold everything
            Directory.CreateDirectory(FolderPath + "Meshes/");                                                                  // build a Folder to hold Meshes too

            // SEEK BITMAP SOURCE FILE
            foreach (XmlNode TileSetInfo in Doc.GetElementsByTagName("tileset"))                                // array of the level nodes.
            {
                var TileSetRef = new cTileSet(TileSetInfo, FilePath);
                TileSets.Add(TileSetRef);
            }
            // CREATE LAYERS . . .
            for (int i = Doc.GetElementsByTagName("layer").Count - 1; i >= 0; i--)
            {
                BuildLayer(Doc.GetElementsByTagName("layer").Item(i));
            }   // end of each Layer Info

            //	INSTANTIATE PREFABS OBJECTS
            foreach (XmlNode ObjectsGroup in Doc.GetElementsByTagName("objectgroup"))
            {
                BuildPrefabs(ObjectsGroup);
            }

            TileOutputSize.z = 0;
        }
        else
        {
            Debug.LogError(FileName + " it's not a Tiled File!, wrong load at: " + FilePath);
        }

        Debug.Log("Tiled Level Build Finished");

        this.Close();
    }   // End of Method loader
Пример #2
0
    //----------------------------------------------------------------------------------------//

    public bool Load(string filePath)
    {
        CamTransform = Managers.Display.MainCamera.transform;
        //Debug.Log(Application.dataPath + filePath);
        if (MapTransform != null)
        {
            Debug.LogWarning("To create a new Map Unload previous one first");
            return(false);
        }


//        if ( !File.Exists(Application.dataPath + filePath) )
//        {
//            Debug.LogWarning("Couldn't Load the TileMap, File Don't Exists!");
//            return false;
//        }
//
//        string fileName = filePath.Remove(0, filePath.LastIndexOf("/") + 1);			    // quit folder path structure
//        fileName = fileName.Remove(fileName.LastIndexOf("."));							    // quit .tmx or .xml extension
//
//        StreamReader sr = File.OpenText(Application.dataPath + filePath);				    // Do Stream Read
//        XmlDocument Doc = new XmlDocument();
//		  FileData.text.ToString
//        Doc.LoadXml(sr.ReadToEnd());                                                        // and Read XML
//        sr.Close();

        XmlDocument Doc = new XmlDocument();

        Debug.Log(filePath);
        TextAsset textAsset = (TextAsset)Resources.Load("Levels/" + filePath, typeof(TextAsset));

        Doc.LoadXml(textAsset.text);


        // CHECK IT'S A TMX FILE FROM TILED
        if (Doc.DocumentElement.Name == "map")                                                                                          // Access root Map
        {
//			Managers.Register.currentLevelFile = "Levels/" +filePath ;						// This made my head hurt someday
            Managers.Register.currentLevelFile = filePath;

            GameObject map = new GameObject(filePath);                  // inside the editor hierarchy.
            MapTransform = map.transform;                               // take map transform cached

            Managers.Display.cameraScroll.ResetBounds(new Rect(0, 0,    // Set Level bounds for camera
                                                               int.Parse(Doc.DocumentElement.Attributes["width"].Value) * TileOutputSize.x,
                                                               int.Parse(Doc.DocumentElement.Attributes["height"].Value) * TileOutputSize.y));

            // SEEK BITMAP SOURCE FILE
            foreach (XmlNode TileSetInfo in Doc.GetElementsByTagName("tileset"))                        // array of the level nodes.
            {
                var TileSetRef = new cTileSet(TileSetInfo);
                TileSets.Add(TileSetRef);
            }

            for (XmlNode Layer = Doc.DocumentElement.LastChild; Layer.Name != "tileset"; Layer = Layer.PreviousSibling)
            {
                switch (Layer.Name)
                {
                case "layer":                                                               // TagName: TileSet Layers
                    StartCoroutine(BuildLayer(Layer));
                    break;

                case "imagelayer":                                                          // TagName: Image Layers (for scrolling)
                    StartCoroutine(BuildScrollLayers(Layer));
                    break;

                case "objectgroup":                                                         // TagName: Object Group Layer
                    StartCoroutine(BuildPrefabs(Layer));
                    break;
                }
            }

            SetupScroll();
        }
        else
        {
            Debug.LogError(filePath + " it's not a Tiled File!, wrong load at: " + filePath);
            return(false);
        }


        if (Doc.DocumentElement.FirstChild.Name == "properties")
        {
            foreach (XmlNode MapProperty in Doc.DocumentElement.FirstChild)
            {
                if (MapProperty.Attributes["name"].Value.ToLower() == "music")
                {
                    Managers.Audio.PlayMusic((AudioClip)Resources.Load("Sound/" + MapProperty.Attributes["value"].Value, typeof(AudioClip)), .3f, 1);
                }

                //if (MapProperty.Attributes["name"].Value.ToLower() == "camerafixedheight")
                //    Managers.Game.PlayerPrefab.GetComponent<CameraTargetAttributes>().FixedHeight = true;

                if (MapProperty.Attributes["name"].Value.ToLower() == "zoom")
                {
                    Managers.Game.PlayerPrefab.GetComponent <CameraTargetAttributes>().distanceModifier = 3.5f;
                }
            }
        }

        Debug.Log("Tiled Level Build Finished: " + filePath);
        return(true);
    }