- Home /
How to switch texture of several 3D objects on GUI buttons click?
Hi @all,
The scene has some 3d objects with same texture. On mouse click on GUIbuttons, the 3dobjects will change to another texture. I made the materials and are already in the "materials list" of those objects. So, with the activation of one button, the objects should switch the "material 0" (current) for "material 1", clicking another button should change current for material 2 and so on. Will it help if all those 3dobjects are in the same gameobject or not?
As a fresh padawan in scripting, dont have code to post, even dont know if the script to achive that behaviour must be attached to objects or to the buttons.
Thanx in advance.
Answer by skovacs1 · Nov 08, 2010 at 08:45 PM
Your question fails to describe some key details. Also, I'm not sure if you are confused about the terminology and actually want to switch materials or if you simply wanted to swap a texture on some materials.
3dobjects in the same GameObject doesn't really make sense. Do you mean as children of the same GameObject or as part of the same mesh or something else entirely?
Textures:
Textures would be simply put as 2D images that are mapped to some coordinate space. The texture of a material can be treated like any other material property such as color and swapped the same.
Materials:
materials and sharedMaterial are the same, except changes to material will create a new material for just this object, whereas changes to sharedMaterial will change the material for all objects using the material. Anywhere in the following where it is written .sharedMaterial
, you can substitute .material
if you need. If you want to change the material for all objects using it, then you don't even need to know what the object is, but only need change the material that they share like so:
var target : Material;
var swapValue : Color = Color.white;
function OnGUI() {
if(GUI.Button(...) && target) {
var temp : Color = target.color;
target.color = swapValue;
swapValue = temp;
}
}
If you wanted to change the materials for all instances of a prefab, you don't need to change the instances, but can make the changes to the prefab in much the same way as shared materials above.
A single object can have multiple materials if this is specified, but in general most objects will only use the first material, except in certain specific cases. Adding more materials to the materials arrays will not really serve much purpose if you aren't using them and swapping them like this isn't really the intention of the arrays, but to swap the values you could do something like:
//swapping index 0 and 1 in the materials or sharedMaterials array
var objects : GameObject[];
function OnGUI() {
if(GUI.Button(...)) {
for(go : GameObject in objects) {
var current : Renderer = go.renderer;
if(!current || current.sharedMaterials.Length < 2)
continue;
var temp : Material = current.sharedMaterials[0];
current.sharedMaterials[0] = current.sharedMaterials[1];
current.sharedMaterials[1] = temp;
}
}
}
In a more complete approach, you would do something like adding something to indicate the material to swap to in a script attached to the object or prefab and then call the function or apply the changes from the script.
swapMat.js (attached to the thing with a mat to swap)
var otherMaterial : Material;
function SwapMaterial() { if(otherMaterial && renderer && renderer.sharedMaterial) { var temp : Material = renderer.sharedMaterial; renderer.sharedMaterial = otherMaterial; otherMaterial = temp; } }
swapButton.js
var objects : GameObject[];
function OnGUI() {
if(GUI.Button(...)) {
for(go : GameObject in objects) {
var script : swapMat = go.GetComponent(SwapMat);
if(!script) continue;
script.SwapMaterial();
}
}
}
Do you really mean to swap materials or are you simply changing the material values which is a lot simpler?
Thank you very much, skovacs1. Let me clarify: I was/am confused! -The shader mode in the materials are the same, they only vary diff and spec textures. The same $$anonymous$$TLs are shared by all objects of the kind (project tab shows one $$anonymous$$TL of each type). -As the inspector shows a "$$anonymous$$TL list", I thought the way to go was adding materials with the different textures. From your answer, what I need is a sharedmaterial and change only the textures applied. By "3dobjects in the same GameObject" I mean to use a gameobject as group parent the 3d gameObjects of same kind. Will test your code, Thanks.
Your answer
Follow this Question
Related Questions
How to switch the texture being used by the material of the mesh renderer 1 Answer
can't put texture materials on imported mesh. 1 Answer
Change material of an object for all the scenes 1 Answer
Is it possible to save runtime generated mesh as a texture ? 1 Answer
How to create a GUI button to change a character's texture in real-time? 2 Answers