x


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.

more ▼

asked Nov 08 '10 at 06:43 PM

maxmad gravatar image

maxmad
32 1 1 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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?

more ▼

answered Nov 08 '10 at 08:45 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

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 MTLs are shared by all objects of the kind (project tab shows one MTL of each type). -As the inspector shows a "MTL 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.

Nov 09 '10 at 03:00 PM maxmad
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2209
x1366
x819
x380
x237

asked: Nov 08 '10 at 06:43 PM

Seen: 2134 times

Last Updated: Nov 08 '10 at 06:43 PM