Runtime Paper2D Sprite Creation

I’ve been trying to create a new Paper2D sprite from scratch during runtime. I’ve come as far as importing the .png and turning them into a texture, which works beautifully. Creating a new sprite with NewObject and setting the various member variables (SourceTexture, BakedSourceTexture, Name, various versions of the Dimensions) and calling RebuildData() also works and I get a sprite I can use, but something does go wrong: Instead of actually displaying the texture, the sprite just shows one solid color picked from the texture. It’s actually a color contained in it and it’s always the same one, so it doesn’t seem to be random, but I can’t get the texture to show properly and I’m a bit at my wit’s end. When inspected, the Sprite appears exactly like a regular sprite created beforehand and all the values are correct. I’ve already looked through RebuildRenderData, but from what I can gather I only need to set BakedSourceTexture for it to work since the UV goes to the zerovector anyway (which should be correct, looking at the regular sprite) and the PPUU. The default RenderGeometry seems to be correct from what I can tell.

Hopefully somebody can give me a few pointers, because right now I don’t even know where I should start looking.

I did some additional digging and tried to use use the PaperSpriteFactory class as template, specifically its FactoryCreateNew() function, as well as the SpriteEditorViewportClient and CreateNewSprite(), but the effect was pretty much the same. I’ve started to look at PostEditChangeProperty, but it seems like I need to wrangle FPropertyChangedEvent for that. Maybe somebody has encountered this or something similar to it before. In any case, my current code looks like this:


UMonsterMasterSprite * URuntimeFactory::CreateSprite(UObject * Outer, UTexture2D * Texture, FName BaseName) {
    FName SpriteName = MakeUniqueObjectName(Outer, UMonsterMasterSprite::StaticClass(), BaseName);
    UMonsterMasterSprite* NewSprite = NewObject<UMonsterMasterSprite>(Outer, UMonsterMasterSprite::StaticClass(), SpriteName, RF_Transactional);

    FSpriteAssetInitParameters SpriteInitParams;
    SpriteInitParams.Texture = Texture;
    SpriteInitParams.Offset = FIntPoint(0, 0);
    SpriteInitParams.Dimension = FIntPoint(Texture->GetSizeX(), Texture->GetSizeY());

    const UPaperImporterSettings* ImporterSettings = GetDefault<UPaperImporterSettings>();
    ImporterSettings->ApplySettingsForSpriteInit(SpriteInitParams, ESpriteInitMaterialLightingMode::Automatic);
    NewSprite->InitializeSprite(SpriteInitParams);

    FVector2D Dimensions = FVector2D(Texture->GetSizeX(), Texture->GetSizeY());
    NewSprite->SetSourceTextureDimension(Dimensions);
    NewSprite->RebuildData();
    NewSprite->PostEditChange();

    return NewSprite;
}



As I said, when inspected it looks exactly like a sprite I created the usual way from a texture made from the same .png, the only difference is that it only shows a solid color. Maybe something is missing for the resize to work, or something is out of order. It feels like it should work and is only missing something small, but crucial.