# How to apply a UTexture2D to a UStaticMeshComponent?

**URL:** <https://forums.unrealengine.com/t/how-to-apply-a-utexture2d-to-a-ustaticmeshcomponent/144369>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [May 25, 2020, 1:48am UTC](https://forums.unrealengine.com/t/how-to-apply-a-utexture2d-to-a-ustaticmeshcomponent/144369 "2020-05-25T01:48:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_520439b1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_520439b1](https://forums.unrealengine.com/u/anonymous_user_520439b1)\
**Post date:** [May 25, 2020, 1:48am UTC](https://forums.unrealengine.com/t/how-to-apply-a-utexture2d-to-a-ustaticmeshcomponent/144369/1 "2020-05-25T01:48:04Z")

</div>

So in my header I have:

```auto

UPROPERTY(EditAnywhere)
UStaticMeshComponent* SampleMesh;

```

And set the mesh with the dragged in instance:

 ![SharedScreenshot.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/3X/4/d/4d159128f014cab9df887235ad59331bef012c5f.jpeg)

The in my cpp implementation I load a PNG into a UTexture2D successfully.

```auto

TSharedPtr<IImageWrapper> PNGImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);
/*
File load code skipped for this post
*/
if(PNGImageWrapper->GetRaw(ERGBFormat::BGRA, 8, RawData))
{
UE_LOG(LogTemp, Log, TEXT("GetRaw success keep going"));

UTexture2D* myTexture = UTexture2D::CreateTransient(500, 500, PF_B8G8R8A8);
FTexture2DMipMap& Mip = myTexture->PlatformData->Mips[0];
void* Data = Mip.BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(Data, RawData.GetData(), (500 * 500 * 4));
Mip.BulkData.Unlock();
myTexture->UpdateResource();

//SampleMesh
}

```

Now I’m not quite sure how to take that and apply it to this plane.

I figure there’s a few lines of setting up this UTexture2D with a material?

Any pointers would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_82dfa2f11](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_82dfa2f11](https://forums.unrealengine.com/u/anonymous_user_82dfa2f11)\
**Post date:** [May 25, 2020, 2:43am UTC](https://forums.unrealengine.com/t/how-to-apply-a-utexture2d-to-a-ustaticmeshcomponent/144369/2 "2020-05-25T02:43:26Z")

</div>

Ok, so first off, which are you trying to do: Are you trying to apply your texture to a StaticMeshComponent that has been placed on an actor in your world? Or do you want to apply your texture to the static mesh actor that you’ve dragged into your world (StaticMeshComponent still applies, but it would be good to know what your end goal is). I ask because you said you have that StaticMeshComponent property in your header – header of what?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_82dfa2f11](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_82dfa2f11](https://forums.unrealengine.com/u/anonymous_user_82dfa2f11)\
**Post date:** [May 25, 2020, 3:46am UTC](https://forums.unrealengine.com/t/how-to-apply-a-utexture2d-to-a-ustaticmeshcomponent/144369/3 "2020-05-25T03:46:14Z")

</div>

Actually, nevermind. I think I see what you’re doing now. So, here’s what I would do – it may not be the best, and someone may have better ideas, but this should get you started. Make a new material. In your Content folder, right-click and choose Material & Textures \> Material. Name your material (I’ll call it Foo to keep it simple). Then open up your material, right-click in the material graph and search for TextureSampleParameter2D – give the parameter a name, like TextureInput. And connect the RGB pin to the Base Color pin of Foo. You can keep the TextureOverride value to whatever the default is – we’ll be changing it to your Texture2D in code:

 ![material.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/3X/3/d/3d9d0b58c10be3593af1eeb9756892d4db049533.jpeg)

Now, back in your code, give your class another property – UMaterialInstanceDynamic\* MyMaterial – this will be the material you’ll be applying to your mesh. After you’re done creating your Texture2D, you’ll want to load the material you saved in your Content folder, and then change the texture its using to your texture. Something like this:

```auto

UMaterialInterface* LoadedMaterial = LoadObject<UMaterialInterface>(nullptr, TEXT("/Game/Foo.Foo"), nullptr, LOAD_None, nullptr);
if (LoadedMaterial)
{
   MyMaterial = UMaterialInstanceDynamic::Create(LoadedMaterial, NULL);

  if (MyMaterial)
  {
      //Here we're applying your texture to the material
      MyMaterial->SetTextureParameterValue(TEXT("TextureInput"), myTexture);

      //Finally, apply the material to your mesh component
      SampleMesh->SetMaterial(0, MyMaterial);
  }
}

```

See if that works. Again, there may be better ways, but this is how I’d try to go about it.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_520439b1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_520439b1](https://forums.unrealengine.com/u/anonymous_user_520439b1)\
**Post date:** [May 25, 2020, 7:17am UTC](https://forums.unrealengine.com/t/how-to-apply-a-utexture2d-to-a-ustaticmeshcomponent/144369/4 "2020-05-25T07:17:51Z")

</div>

Thanks for the tips!! I got it to compile and update the texture but it just shows up as white. It is indeed updating the material/texture though! Another step closer… Maybe I’ll try another png and see if I at least see a color difference…

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_520439b1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_520439b1](https://forums.unrealengine.com/u/anonymous_user_520439b1)\
**Post date:** [May 25, 2020, 7:28am UTC](https://forums.unrealengine.com/t/how-to-apply-a-utexture2d-to-a-ustaticmeshcomponent/144369/5 "2020-05-25T07:28:01Z")

</div>

BOOM! I at least got it to show up! Now I can fiddle with the alignment! You also pointed out a lot of subjects I can now study about how/why this works so thank you much!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_82dfa2f11](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_82dfa2f11](https://forums.unrealengine.com/u/anonymous_user_82dfa2f11)\
**Post date:** [May 25, 2020, 2:47pm UTC](https://forums.unrealengine.com/t/how-to-apply-a-utexture2d-to-a-ustaticmeshcomponent/144369/6 "2020-05-25T14:47:48Z")

</div>

Not a problem. Glad I could help.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_520439b1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_520439b1](https://forums.unrealengine.com/u/anonymous_user_520439b1)\
**Post date:** [May 25, 2020, 7:00pm UTC](https://forums.unrealengine.com/t/how-to-apply-a-utexture2d-to-a-ustaticmeshcomponent/144369/7 "2020-05-25T19:00:21Z")

</div>

Only thing now is I tried packaging it to run as a delivered app, it doesn’t grab the cat image. just displays as a white texutre… I saw a window pop up saying allow my program to have access type message, so I accepted that, close/reopen, still a white texture… Hmmm

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_520439b1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_520439b1](https://forums.unrealengine.com/u/anonymous_user_520439b1)\
**Post date:** [May 25, 2020, 7:46pm UTC](https://forums.unrealengine.com/t/how-to-apply-a-utexture2d-to-a-ustaticmeshcomponent/144369/8 "2020-05-25T19:46:50Z")

</div>

Actually tried again . Originally built as development package which gave me white square. Deleted that and packaged a shipping output and it worked! BUT for some reason the cat in shipping outout it perfectly aligned with plane while in the testing environment it had an image offset… i don’t love that it looks different on final export… maybe I have to do some extra lines of code to specify the texture alignment with that plane…
