I’m using a Paper2D’s tile set component. And I want to change the tile set’s texture at runtime using C++.
However, after my testing, I found that only texture assets imported to UE can be displayed correctly. (This following code can run perfectly: )
//load the texture2d
UTexture2D* BackgroundTexture = LoadObject<UTexture2D>(nullptr,TEXT("/Script/Engine.Texture2D'/Game/Maps/test.test'"));
//create tile set and set texture
UPaperTileSet* BackgroundTileSet = NewObject<UPaperTileSet>();
BackgroundTileSet->SetTileSheetTexture(BackgroundTexture);
BackgroundTileSet->SetTileSize(FIntPoint(Width, Height));
//create tile map
int32 TileNum = Length / Width;
int32 Remainder = Length % Width;
if (Remainder != 0)
{
TileNum += 1;
}
TileMapComponent->CreateNewTileMap(TileNum,1, Width, Height);
if (TileMapComponent->OwnsTileMap())
{
//create tileinfo
FPaperTileInfo TileInfo;
TileInfo.TileSet = BackgroundTileSet;
TileInfo.PackedTileIndex = 0;
TileInfo.SetFlagValue(EPaperTileFlags::FlipHorizontal, false);
TileInfo.SetFlagValue(EPaperTileFlags::FlipVertical, false);
TileInfo.SetFlagValue(EPaperTileFlags::FlipDiagonal, false);
//set tile
for (size_t i = 0; i < TileNum; i++)
{
TileMapComponent->SetTile(i, 0, 0, TileInfo);
}
//set collision
TileMapComponent->SetLayerCollision(0,false,false,0.0f);
TileMapComponent->MarkRenderStateDirty();
}
if the texture is a purely dynamically generated texture (such as loading a PNG file from disk as a texture), it can be applied to the tile set, but It cannot be displayed correctly. The tile map has no tiles, so there is nothing in the scene, just like a void.(see the following codes )
//load texture2d from disk
UTexture2D* BackgroundTexture = LoadTargetTexture2D(PngFliePath);//this function's return value is a UTexture2D pointer. And it's OK for Sprite or Flipbook component
int32 Width = BackgroundTexture->GetSizeX();
int32 Height = BackgroundTexture->GetSizeY();
///the following codes are same as before
//create tile set and set texture
UPaperTileSet* BackgroundTileSet = NewObject<UPaperTileSet>();
BackgroundTileSet->SetTileSheetTexture(BackgroundTexture);
BackgroundTileSet->SetTileSize(FIntPoint(Width, Height));
//create tile map
int32 TileNum = Length / Width;
int32 Remainder = Length % Width;
if (Remainder != 0)
{
TileNum += 1;
}
TileMapComponent->CreateNewTileMap(TileNum,1, Width, Height);
if (TileMapComponent->OwnsTileMap())
{
//create tileinfo
FPaperTileInfo TileInfo;
TileInfo.TileSet = BackgroundTileSet;
TileInfo.PackedTileIndex = 0;
TileInfo.SetFlagValue(EPaperTileFlags::FlipHorizontal, false);
TileInfo.SetFlagValue(EPaperTileFlags::FlipVertical, false);
TileInfo.SetFlagValue(EPaperTileFlags::FlipDiagonal, false);
//set tile
for (size_t i = 0; i < TileNum; i++)
{
TileMapComponent->SetTile(i, 0, 0, TileInfo);
}
//set collision
TileMapComponent->SetLayerCollision(0,false,false,0.0f);
TileMapComponent->MarkRenderStateDirty();
}
I don’t know why it doesn’t work, and how can I solve this problem?
hope somebody can provide some answers and help, appreciated !![]()