Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by maxmad · Nov 08, 2010 at 06:43 PM · texturemeshmaterialchangeswitch

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maxmad · Nov 09, 2010 at 03:00 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges