- Home /
how to change image texture using C#
hi,
i have an object with many textures and i want to change every texture with the single press of button, the problem is here i m very beginner of C# and don't know where to start i really don't know the basic of scripting, can anybody help or suggest me ??
any help would be appreciated
Thanks,
Answer by FatalError · Dec 19, 2013 at 03:34 PM
Hope this helps. I'm hardly the best, but this should work for your purpose. It'll grab the position, let your cycle through the whole array, and assigns it to your object's texture.
public Texture[] myTextures = new Texture[5];
int maxTextures;
int arrayPos = 0;
void Start ()
{
maxTextures = myTextures.Length-1;
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.U))
{
renderer.material.mainTexture = myTextures[arrayPos];
if(arrayPos == maxTextures)
{
arrayPos = 0;
}
else
{
arrayPos++;
}
}
}
Just attach the script to the object whose textures you want to change/cycle, and then drag & drop your textures to it in the editor. Doing it this way allows you to reuse this script on any object you want without having to recode anything.
Take note that if you're really wanting to change out their Material instead of the direct Texture, then you'll want to create your Material assets and then just change out Texture in this code with Material.
Example: public Material[] myMaterials = new Material[5];
Here is the code revised to change Materials instead of Textures.
public Material[] myMaterials = new Material[5];
int maxMaterials;
int arrayPos = 0;
void Start ()
{
maxMaterials = myMaterials.Length-1;
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.U))
{
renderer.material = myMaterials[arrayPos];
if(arrayPos == maxMaterials)
{
arrayPos = 0;
}
else
{
arrayPos++;
}
}
}
You can basically use this setup for whatever you're wanting to cycle with, just swap out the types. If you wanted to have a "previous" button, just make another section in the Update for that key and have it arrayPos--; instead.
If your default texture is also the first one in your array, you'll just want to put the renderer below the if check in update.
void Update ()
{
if (Input.GetKeyDown(KeyCode.U))
{
if(arrayPos == maxMaterials)
arrayPos = 0;
else
arrayPos++;
renderer.material = myMaterials[arrayPos];
}
if (Input.GetKeyDown(KeyCode.Y))
{
if(arrayPos == 0)
arrayPos = maxMaterials;
else
arrayPos--;
renderer.material = myMaterials[arrayPos];
}
}
You should go check out the Unity3D.com/learn site on their webpage. They've been updating it like crazy lately and it has a lot of excellent information there. There is a big section for the scripting also, and it goes over everything you'll need to know about C# to work with Unity.
C# Unity Scripting: http://unity3d.com/learn/tutorials/modules/beginner/scripting
Btw, it's bad practice to do what I just did in the Update (), you should make a method for it and call it in the Update instead. :D
public Material[] myMaterials = new Material[5];
int maxMaterials;
int arrayPos = 0;
void Start ()
{
maxMaterials = myMaterials.Length-1;
}
void updateMaterials()
{
//Cycle forward
if (Input.GetKeyDown (KeyCode.U)) {
if (arrayPos == maxMaterials)
arrayPos = 0;
else
arrayPos++;
renderer.material = myMaterials [arrayPos];
}
//Cycle backwards
if (Input.GetKeyDown (KeyCode.Y)) {
if (arrayPos == 0)
arrayPos = maxMaterials;
else
arrayPos--;
renderer.material = myMaterials [arrayPos];
}
}
void Update ()
{
updateMaterials ();
}
@FatalError :Thanks for the writing a long and various scripts for me, i used that script and it works
int Counter=0;
void Start ()
{
}
void Update ()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.U))
{
Counter++;
if(Counter>4)
Counter=1;
Texture txt=null;
switch(Counter)
{
case 1:txt=Resources.Load("caution-signal", typeof(Texture2D)) as Texture;break;
case 2:txt=Resources.Load("attention-signal", typeof(Texture2D)) as Texture;break;
case 3:txt=Resources.Load("proceed-signal", typeof(Texture2D)) as Texture;break;
case 4:txt=Resources.Load("stop-signal", typeof(Texture2D)) as Texture;break;
}
renderer.material.mainTexture = txt;
}
}
}
Thanks again guys,
Answer by Theinsanekiller · Dec 19, 2013 at 07:42 AM
hey , you can use this line of code to change your texture,
transform.renderer.material.SetTexture("_MainTex","texture");
as a texture you can use any texture you wish.
SetTexture() will work for any texture property of a shader, but there is a 'shortcut' for the main texture:
renderer.material.mainTexture = texture;
Yeah but, if you do it with maintexture it will change for all the instances of that particular material
Not true. If you change a material property of 'renderer.material' at runtime, Unity creates a new material instance so that the change will only apply to the one object...even if the material is used in multiple places. You can use 'renderer.shared$$anonymous$$aterial' if you want to change all object that use the material.
Yeah my mistake, and thanx a lot for correcting me....
@ roberthu : thanks for your comment,
using UnityEngine;
using System.Collections;
public class signal_change : $$anonymous$$onoBehaviour {
GameObject c1;
GameObject c2;
// Use this for initialization
void Start () {
c1= GameObject.Find ("attention-signal");
c2= GameObject.Find ("proceed-signal");
}
// Update is called once per frame
void Update ()
{
//$$anonymous$$oving Left
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.U))
renderer.material.mainTexture = c2;
}
}
I m just using this scrit I don't know where i m worng and still facing problem.
Your answer
Follow this Question
Related Questions
How to size a texture for a object New to unity. Please Help 0 Answers
How to change texture of another object by clicking on a object? 0 Answers
Blender texture problem! 3 Answers
Flip a Texture? 3 Answers
Textures don's show? 0 Answers