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
- Use Addressable Assets System for dynamic map loading.
- related resources:
- But I tried many ways, but couldn't load the tilebase and then draw on the tilemap.
- The following code can load the prefab correctly. After execution, there will be a bunch of prefab on the inspector, but an error will be reported when drawing the tilemap.
- When using tilemap to draw tiles, the content of the inspector will not be changed. The main problem should be that it
GameObject tilePrefab = handle.Result;
can only be usedGameObject
but not usedTileBase
.
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
GameObject
the type, and I can't findTileBase
a way to convert it.
string tile_path = "Tilemap/Block/Soil/1_GrassSoilBlock";
TileBase tile = Resources.Load<TileBase>(tile_path);