public void CreateMap()
    {
        // HARD CODE
        uvMap_X_Dictionary = new Dictionary <RoadType, Vector2>();
        uvMap_Y_Dictionary = new Dictionary <RoadType, Vector2>();
        uvMap_X_Dictionary.Add(RoadType.CrissCross, new Vector2(0.2f, 0.4f));
        uvMap_X_Dictionary.Add(RoadType.Horizontal, new Vector2(0.4f, 0.6f));
        uvMap_X_Dictionary.Add(RoadType.Vertical, new Vector2(0, 0.2f));
        uvMap_X_Dictionary.Add(RoadType.Turn_LD, new Vector2(0.6f, 0.8f));
        uvMap_X_Dictionary.Add(RoadType.Turn_LU, new Vector2(0.6f, 0.8f));
        uvMap_X_Dictionary.Add(RoadType.Turn_RD, new Vector2(0.8f, 0.6f));
        uvMap_X_Dictionary.Add(RoadType.Turn_RU, new Vector2(0.8f, 0.6f));

        uvMap_Y_Dictionary.Add(RoadType.Turn_RU, new Vector2(1f, 0));
        uvMap_Y_Dictionary.Add(RoadType.Turn_LU, new Vector2(1f, 0));
        if (GenerateNewMap)
        {
            GenerateMap();
        }
        else
        {
            string path = "Assets/Saved Maps/" + selectedFile;
            _tileMap  = MyFileIO.ByteArrayToObject(File.ReadAllBytes(path)) as TileMap;
            tileSizeZ = _tileMap.GetLength(0);
            tileSizeX = _tileMap.GetLength(1);
        }
        BuildMesh();
        AssignUVs();
        BuildMiniMapTexture(); // Map for minimap viewer
        ApplyMesh();
        // Temp code for testing A*
        //PathFinding pathFinder;

        //pathFinder = GetComponent<PathFinding>();
        //pathFinder.TestRun();
    }
Example #2
0
    void OnGUI()
    {
        if (files_arr == null)
        {
            files_arr = RefreshFileList();
        }
        GUILayout.Label("Export Map to Assets:", EditorStyles.boldLabel);
        GUILayout.Space(5);
        AssetName = EditorGUILayout.TextField("Save Map As: ", AssetName);

        if (GUILayout.Button("Save Map"))
        {
            // Ask for name here
            MapGenerator mapGenerator = MapGenerator.Instance;
            if (mapGenerator != null)
            {
                string   path     = "Assets/Saved Maps/" + AssetName + ".map";
                FileInfo fi       = new FileInfo(path);
                bool     saveFile = false;
                if (fi.Exists)
                {
                    if (EditorUtility.DisplayDialog("Confirm Overwrite", "File with name " + AssetName + ".map already exists. Overwrite?", "Overwrite", "No"))
                    {
                        saveFile = true;
                    }
                }
                else
                {
                    saveFile = true;
                }
                if (saveFile)
                {
                    //AssetDatabase.CreateAsset(mapGenerator.tileMap, "Assets/Saved Maps/" + AssetName + ".asset");
                    File.WriteAllBytes(path, MyFileIO.ObjectToBytesArray(mapGenerator.tileMap));
                    message   = "Map successfully saved as " + path + " at " + System.DateTime.Now;
                    files_arr = RefreshFileList();
                    AssetDatabase.Refresh();
                    // Serialize and save the tilemap
                }
            }
            else
            {
                message = "Create a map first. It is null right now.";
            }
        }


        GUILayout.Label("Import Map from Assets:", EditorStyles.boldLabel);
        GUILayout.Space(5);

        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("Choose map: ");

        selectIndex = EditorGUILayout.Popup(selectIndex, files_arr);
        EditorGUILayout.EndHorizontal();

        if (GUILayout.Button("Load Map"))
        {
            string path = "Assets/Saved Maps/" + files_arr[selectIndex];
            // TileMap tileMap = AssetDatabase.LoadAssetAtPath<TileMap>("Assets/Saved Maps/" + AssetName + ".asset");
            TileMap tileMap = MyFileIO.ByteArrayToObject(File.ReadAllBytes(path)) as TileMap;
            if (tileMap != null)
            {
                message = "Tilemap loaded " + tileMap.tiles;
                MapGenerator mapGenerator = MapGenerator.Instance;
                mapGenerator.CreateMapFrom(tileMap);
            }
        }
        if (GUILayout.Button("Refresh List"))
        {
            files_arr = RefreshFileList();
            message   = files_arr.Length + "";
        }

        GUILayout.Label("Message:", EditorStyles.boldLabel);
        GUILayout.Label(message, EditorStyles.wordWrappedLabel);
    }