Unity Record 4.1 - Storage - Loading Tiles based on keywords

The article was first published on the blog: https://mwhls.top/4810.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: After implementing the Tilemap map generation, load Tiles based on keywords.

  • To save a map, you must first be able to load the map based on keywords instead of dragging and dragging in the inspector, so the first step is to load local files based on the path.
Addressable Assets System (unsuccessful)-2023/08/13-2023/08/14
        async void place_tiles(Tilemap tilemap, ArrayList pos_array){
    
    
            AsyncOperationHandle<GameObject> handle = Addressables.LoadAssetAsync<GameObject>("Assets/ResourcesAddressable/Prefab/Block/SoilBlock.prefab");
            await handle.Task;
            GameObject tilePrefab = handle.Result;
            GameObject instantiatedTile = Instantiate(tilePrefab);
    
            TileBase tile = instantiatedTile.GetComponent<TileBase>();
            foreach (Vector3Int pos in pos_array){
    
    
                tilemap.SetTile(pos, tile);
            }
        }
Resources loaded-2023/08/14
  • This is actually my plan five months ago.
    • But now that I have GPT I can ask, and it recommended Addressable to me, which looks really good, so I wrote the above solution first.
  • As shown in the code below, I loaded the asset instead of the prefab, and it was implemented in just two lines.
    • The actual path is "Assets\Resources\Tilemap\Block\Soil/1_GrassSoilBlock.asset", omit Resources and their prefix, and omit the file extension.
    • Use addressable to load, whether it is asset or prefab, it returns GameObjectthe type, and I can't find TileBasea way to convert it.
    string tile_path = "Tilemap/Block/Soil/1_GrassSoilBlock";
    TileBase tile = Resources.Load<TileBase>(tile_path);

Guess you like

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