Change object texture on tap on UI Object
Somebody help me please! I have code:
var matArray : Material[];
private var index : int;
function Update () {
if (Input.GetButtonDown("Fire1")) {
index++;
index = index % matArray.Length;
GetComponent.<Renderer>().material = matArray[index];
}
}
But I want change texture only then tap on UI object. How I can do it?
Thanks.
Answer by rabby · Feb 11, 2016 at 06:06 AM
Hello @Zelentsov , If I understand correctly then you want to change the texture of materials when you touch/click on any UI object. For this do the following.
1.Create an empty Gameobject and attach a newly created c# script and paste the following code snippets on that script.
public Material objectMaterial; // material which texture is going to change
public Texture texture2change; // texture that will be reflected on material after change
public void buttonClicked(){
objectMaterial.mainTexture = texture2change;
}
Attach the material you want to change and the texture that will be reflected on material after change to the public field of the script.
Create a new UI button/object , select that object on the hierarchy panel , on the inspector tab click ->Add Component -> UI -> Button(this step is necessary other that Button UI object).
On the Onclick segment of button component, hit '+' sign , Add the newly created Gameobject(not the UI object , the other one) to the field indicating "None(Object)" , click "no function" section , select the script attached with the gameobject and then select the public method buttonClicked().
Save and Run.
You have to attach the material to some gameobject in order to view the reflected change. Let me know if that helps you.
Thanks.
Hi Rabby,
I'm looking for a similar thing to what Zelentsov has asked.
However this needs to work with the press of a specific UI button.
For example I will have a game object. There will be multiple UI buttons. The button will have a picture of the texture, When that button is pressed it will apply the shown texture to the object.
As a result I need atleast a dozen different buttons each which apply a different texture to the game object.
Do you have any suggestions?
public $$anonymous$$aterial object$$anonymous$$aterial; // material which texture is going to change public void buttonClicked(Texture texture2change){
object$$anonymous$$aterial.mainTexture = texture2change; }
Attach the above code snippet to the gameobject and add the buttonClicked method as onclickListener of the button. Pass the texture as the parameter. In this way no matter how many button you create , you can pass different texture with each of them . Hope this solves your problem
Brilliant thanks for the example :)
Sorry for being unaware but I'm new to Unity and C#. I'm getting on well with it but have yet to do any real work with UI systems.
I will use the Snippet you have suggested and enter that into a new C# script. This can then be attached to my game object as a new component.
I understand that fine,
What I don't know how to do is associate this to the button.
"add the buttonClicked method as onclickListener of the button. Pass the texture as the parameter."
Not to sure how to actually do this part. Apologies if it is really simple and i'm just being naive.
I'm aware this was not originally my question and i'm going a little off topic so if you'd rather I messaged you privately you then that's fine.
I appreciate the assistance none the less :)
Your answer
Follow this Question
Related Questions
How to move UI element inside of canvas programmatically 0 Answers
Player Health Glitch? 0 Answers
Instantiate panel background not showing 1 Answer
CharacterInfo.glyphWidth always 0 2 Answers
Scaling an image sent to UI Image 0 Answers