Unity Record 4.2 - Storage - Get Tile path from json file

The article was first published on the blog: https://mwhls.top/4813.html .
No picture/format error/for subsequent updates, please see the first page.
For more updates, please visit mwhls.top
. You are welcome to leave a message with questions or criticisms. Private messages will not be answered.

Summary: Unity Records

Summary: Get the Tile material path from the json file.

Determine the preservation plan-2023/08/15
  • Asked GPT and it said Tilemaps are rendered based on camera position, so oversized maps will not affect loading performance.
  • GPT recommends many methods. I am going to use json+compression first.
  • Below is the test case.
    {
    
    
        "version": "0.0.1",
        "date": "20230815",
        "tiles":{
    
    
            "BlockSoilGrassySoil":{
    
    
                "ID": 0,
                "name": "Grassy Soil",
                "description": "",
                "path": "Tilemap/Block/Soil/BlockSoilGrassySoil"
            }
        }
    }
Newtonsoft.Json installation-2023/08/16
Loading materials from json files-2023/08/16
  • Load material information as shown below.
    • First, two structures are consistent with the json format, then load them using files and convert them to json through "Newtonsoft.Json".
    using Newtonsoft.Json;
    
    public struct TileInfo{
    
    
        public int ID;
        public string name;
        public string description;
        public string path;
    };
    
    public struct TilesInfo{
    
    
        public string version;
        public string date;
        public Dictionary<string, TileInfo> tiles;
    }
    
    public class TilemapBase : MonoBehaviour{
    
    
        public static TilesInfo tiles_info;
    
        void Start(){
    
    
            load_tiles_info();
        }
    
        void load_tiles_info(){
    
    
            string tiles_info_path = "Assets/Resources/Saved/TilesInfo.json";
            string jsonText = File.ReadAllText(tiles_info_path);
            tiles_info = JsonConvert.DeserializeObject<TilesInfo>(jsonText);
        }
    }
    
  • Replace the previously hard-coded material path
    • Note: The path in the previous article is different from the path in this article because I changed the naming style.
    string tile_path = "Tilemap/Block/Soil/BlockSoilGrassySoil";
    ->
    string tile_path = TilemapBase.tiles_info.tiles["BlockSoilGrassySoil"].path;

Guess you like

Origin blog.csdn.net/asd123pwj/article/details/132558672