Esempio n. 1
0
    public static void SaveLevel(int level)
    {
        var go = GameObject.FindObjectOfType <Grid>();

        if (go == null)
        {
            return;
        }
        var grid = go.GetComponent <Grid>();

        if (grid == null)
        {
            return;
        }
        var bytes = TileMapSerializer.SerializeGrid(grid, LevelManager.Tile2ID);

        if (bytes != null)
        {
            File.WriteAllBytes(LevelManager.GetMapPathFull(level), bytes);
        }
#if UNITY_EDITOR
        if (!Application.isPlaying)
        {
            AssetDatabase.Refresh();
        }

        Debug.LogFormat("SaveLevel {0} succ", level);
#endif
    }
Esempio n. 2
0
    // Start is called before the first frame update
    void Start()
    {
        //Creating of our serializer
        _serializer = new TileMapSerializer();

        if (save)
        {
            SaveRooms();
        }

        if (load_ONLY_DEBUG)
        {
            LoadRooms();
        }
    }
Esempio n. 3
0
        public static void SaveLevel(Grid grid, int level)
        {
            bool flag = grid == null;

            if (!flag)
            {
                CheckLoadTileIDMap();
                byte[] array = TileMapSerializer.WriteGrid(grid, new Func <TileBase, ushort>(UnityMap2DUtil.Tile2ID));
                bool   flag2 = array != null;
                if (flag2)
                {
                    File.WriteAllBytes(Map2DUtil.GetMapPathFull(level), array);
                }
            }
        }
Esempio n. 4
0
    // Start is called before the first frame update
    void Start()
    {
        //Creating of our serializer
        _serializer = new TileMapSerializer();

        //We assume the script is on the Tilemap Object so we get the Tilemap Component
        map = gameObject.GetComponent <Tilemap>();

        //We find the grid and camera objects, grab the components we need.
        grid = GameObject.FindWithTag("Grid").GetComponent <Grid>();
        cam  = GameObject.FindWithTag("MainCamera").GetComponent <Camera>();


        //TEST ONE
        TestingProgramaticallyCopyAndSerialize();
    }
Esempio n. 5
0
    public static byte[] WriteGrid(Grid grid, Func <TileBase, ushort> FuncGetTileIdx)
    {
        bool flag = grid == null;

        byte[] result;
        if (flag)
        {
            result = null;
        }
        else
        {
            Serializer  serializer           = new Serializer();
            LVector2Int val                  = new LVector2Int(2147483647, 2147483647);
            LVector2Int val2                 = new LVector2Int(-2147483648, -2147483648);
            Tilemap[]   componentsInChildren = grid.GetComponentsInChildren <Tilemap>();
            foreach (Tilemap tilemap in componentsInChildren)
            {
                Vector3Int min   = tilemap.cellBounds.min;
                Vector3Int max   = tilemap.cellBounds.max;
                bool       flag2 = min.x < val.x;
                if (flag2)
                {
                    val.x = min.x;
                }
                bool flag3 = min.y < val.y;
                if (flag3)
                {
                    val.y = min.y;
                }
                bool flag4 = max.x > val2.x;
                if (flag4)
                {
                    val2.x = max.x;
                }
                bool flag5 = max.y > val2.y;
                if (flag5)
                {
                    val2.y = max.y;
                }
            }
            serializer.Write(val);
            serializer.Write(val2);
            serializer.Write(grid.cellSize.ToLVector3());
            serializer.Write(grid.cellGap.ToLVector3());
            serializer.Write((int)grid.cellLayout);
            serializer.Write((int)grid.cellSwizzle);
            Tilemap[] componentsInChildren2 = grid.GetComponentsInChildren <Tilemap>();
            serializer.Write(componentsInChildren2.Length);
            foreach (Tilemap tilemap2 in componentsInChildren2)
            {
                serializer.Write(tilemap2.name);
            }
            foreach (Tilemap tilemap3 in componentsInChildren2)
            {
                TilemapRenderer component = tilemap3.GetComponent <TilemapRenderer>();
                Debug.Log(tilemap3.name + " " + component.sortingOrder);
                serializer.Write(component.sortingOrder);
            }
            foreach (Tilemap map in componentsInChildren2)
            {
                TileMapSerializer.WriteMap(serializer, map, FuncGetTileIdx);
            }
            result = serializer.CopyData();
        }
        return(result);
    }
Esempio n. 6
0
    public static GridInfo LoadLevel(int level)
    {
        CheckLoadTileIDMap();
        var path = LevelManager.GetMapPathFull(level);

        if (!File.Exists(path))
        {
            Debug.LogError("Have no map file" + level);
            return(null);
        }

        var bytes  = File.ReadAllBytes(path);
        var reader = new BinaryReader(new MemoryStream(bytes));
        var info   = TileMapSerializer.ReadGrid(reader);
        var go     = GameObject.FindObjectOfType <Grid>();

        if (go != null)
        {
            var maps = go.GetComponentsInChildren <Tilemap>();
            for (int i = 0; i < maps.Length; i++)
            {
                var tileMap     = maps[i];
                var tileMapInfo = info.GetMapInfo(tileMap.name);
                if (tileMapInfo == null)
                {
                    continue;
                }
                tileMapInfo.tilemap = tileMap;
                tileMap.ClearAllTiles();
                tileMap.SetTiles(tileMapInfo.GetAllPositions(), tileMapInfo.GetAllTiles());
                if (Application.isPlaying)
                {
                    if (tileMap.name == TILE_MAP_NAME_BORN_POS)
                    {
                        tileMap.GetComponent <TilemapRenderer>().enabled = false;
                    }
                }

                if (tileMap.name == TILE_MAP_NAME_BORN_POS ||
                    tileMap.name == TILE_MAP_NAME_GRASS)
                {
                    tileMapInfo.hasCollider = false;
                }

                if (tileMap.name == TILE_MAP_NAME_BORN_POS)
                {
                    tileMapInfo.isTagMap = true;
                }
            }
        }

        var min = new Vector2Int(int.MaxValue, int.MaxValue);
        var max = new Vector2Int(int.MinValue, int.MinValue);

        foreach (var tempInfo in info.tileMaps)
        {
            var tileMap = tempInfo.tilemap;
            var mapMin  = tileMap.cellBounds.min;
            if (mapMin.x < min.x)
            {
                min.x = mapMin.x;
            }
            if (mapMin.y < min.y)
            {
                min.y = mapMin.y;
            }
            var mapMax = tileMap.cellBounds.max;
            if (mapMax.x > max.x)
            {
                max.x = mapMax.x;
            }
            if (mapMax.y > max.y)
            {
                max.y = mapMax.y;
            }
        }

        GameManager.Instance.min = min;
        GameManager.Instance.max = max;
        return(info);
    }
Esempio n. 7
0
    public static GridInfo LoadLevel(int level)
    {
        CheckLoadTileIDMap();
        var path = LevelManager.GetMapPathFull(level);

        if (!File.Exists(path))
        {
            Debug.LogError("Have no map file" + level);
            return(null);
        }

        var bytes  = File.ReadAllBytes(path);
        var reader = new BinaryReader(new MemoryStream(bytes));
        var info   = TileMapSerializer.ReadGrid(reader);
        var go     = GameObject.FindObjectOfType <Grid>();

        Debug.Assert(go != null, "Can not find Grid in scene");
        //init tilemap info
        var maps = go.GetComponentsInChildren <Tilemap>();

        for (int i = 0; i < maps.Length; i++)
        {
            var tileMap     = maps[i];
            var tileMapInfo = info.GetMapInfo(tileMap.name);
            if (tileMapInfo == null)
            {
                continue;
            }
            tileMapInfo.tilemap = tileMap;
            tileMap.ClearAllTiles();
            tileMap.SetTiles(tileMapInfo.GetAllPositions(), tileMapInfo.GetAllTiles());
            if (Application.isPlaying)
            {
                if (tileMap.name == TILE_MAP_NAME_BORN_POS)
                {
                    tileMap.GetComponent <TilemapRenderer>().enabled = false;
                }
            }

            if (tileMap.name == TILE_MAP_NAME_BORN_POS ||
                tileMap.name == TILE_MAP_NAME_GRASS)
            {
                tileMapInfo.hasCollider = false;
            }

            if (tileMap.name == TILE_MAP_NAME_BORN_POS)
            {
                tileMapInfo.isTagMap = true;
            }
        }

        // resort tilemaps by layer
        int count    = maps.Length;
        var tempMaps = go.GetComponentsInChildren <TileMapHelper>().ToList();

        tempMaps.Sort((a, b) => { return(b.layer - a.layer); }); //重排优先级
        Debug.Assert(tempMaps.Count == count,
                     "Some tilemap do not have component!!" + typeof(TileMapHelper).ToString());
        for (int i = 0; i < count; i++)
        {
            maps[i] = tempMaps[i].GetComponent <Tilemap>();
        }

        var tempTileMaps = new TileInfos[count];
        var tempNames    = new string[count];

        for (int i = 0; i < count; i++)
        {
            var name = tempMaps[i].name;
            tempNames[i]    = name;
            tempTileMaps[i] = info.GetMapInfo(name);
        }

        info.tileMaps = tempTileMaps;
        info.names    = tempNames;

        var min = new Vector2Int(int.MaxValue, int.MaxValue);
        var max = new Vector2Int(int.MinValue, int.MinValue);

        foreach (var tempInfo in info.tileMaps)
        {
            var tileMap = tempInfo.tilemap;
            var mapMin  = tileMap.cellBounds.min;
            if (mapMin.x < min.x)
            {
                min.x = mapMin.x;
            }
            if (mapMin.y < min.y)
            {
                min.y = mapMin.y;
            }
            var mapMax = tileMap.cellBounds.max;
            if (mapMax.x > max.x)
            {
                max.x = mapMax.x;
            }
            if (mapMax.y > max.y)
            {
                max.y = mapMax.y;
            }
        }

        GameManager.Instance.min = min;
        GameManager.Instance.max = max;
        return(info);
    }