How do make textures for an object after its already been placed into Unity?
This has been a problem that has puzzled me for a long time and I hope I explain it correctly. I want to make a customization system for vehicles in a unity game, basically I want one object to be able to have multiple textures that can be switched between as the player wants. I'm mostly confused about the process of creating these textures and assigning them to the specific object. I use blender and like I know I have to uv map the object to the image I want to texture it with, then save export and boom I've got the texture, but how do I create more textures for this object at a later point without reimporting the object? I'm new to texturing but I can't find anything that helps me with this so I would appreciate the assistance.
Answer by ray2yar · Feb 18, 2019 at 11:20 AM
Well, you can apply whichever texture you want. So there are a few things you could do here....
This code is a sample of how you could create a texture entirely in script and assign it... tedious, but you can make infinite textures like in a paint program from your code for the user to apply...
Texture2D myTexture;
int sizeX;
int sizeY;
int locX;
int locY;
Color32 myColor;
void Start()
{
//creature a new texture
myTexture = new Texture2D(sizeX, sizeY, TextureFormat.ARGB32, false);
//use to change the color of a certain pixel
myTexture.SetPixel(locX, locY, myColor);
//use to actually apply the changes to your texture
myTexture.Apply();
//use to actually assign the texture to the material of the object
GetComponent<Renderer>().material.mainTexture = myTexture;
}
Of course you probably just want a bunch of textures that are premade that the user can select from. Here is a code snippet to get you going on that. The textures would need to be assigned from the inspector.
public Texture2D[] sampleTextures;
public Renderer[] myRenders;
int currentTexture=0;
//called from UI when user presses a button
public void OnButtonCycle()
{
currentTexture++;
if (currentTexture >= sampleTextures.Length) currentTexture = 0;
foreach(Renderer REND in myRenders)
{
REND.material.mainTexture = sampleTextures[currentTexture];
}
}
Your answer
Follow this Question
Related Questions
Custom Event not showing up on the validator 0 Answers
Problem with force movement when calling from another scripts 0 Answers
Rotated object raycasting in wrong directions!!? 3 Answers
can't check if my NPC has a specific script attached 0 Answers
after pressing button many times the player starts moving more. 1 Answer