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
1
Question by ina · May 03, 2011 at 09:39 PM · scenematerialsruntime

Loop through all materials in scene and change them

Is there a way to access the materials in a scene at runtime, and reassign them?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jean-Fabre · May 04, 2011 at 06:29 AM

Hi,

There is much better built in alternative.

Camera.RenderWithShader

Bounce from this page for other types of similar features.

If you are looking exchanging different materials etc etc, then you'll have to do it manually, and I would recommend doing a reusable component for it using GetComponentsInChildren(); to retrieve all renderer of that game Object and its childrens, then you can store material references, exchange them at will etc etc. I do that for example, when I need to hide parts of an assembly, I have a transparent shader that I swap at runtime.

If you need a more exhaustive description of such component, say so, and I'll wrap up something.

[EDIT] here is a component to do just that:

using UnityEngine; using System.Collections;

public class MaterialSwitcher : MonoBehaviour {

 public Material switchMaterial;

 private Hashtable _matList = new Hashtable();

 private Renderer[] _renderers;

 private bool switched;

 /// <summary>
 /// Record all material of the gameobject and its children
 /// </summary>
 void Start () { 
     _renderers= GetComponentsInChildren<Renderer>();
     foreach( Renderer _renderer in _renderers){
         _matList.Add(_renderer,_renderer.material);
     }
 }

 /// <summary>
 /// just for testing
 /// </summary>
 public void Update(){

     if (Input.anyKeyDown){
         ToggleMaterial();
     }
 }

 /// <summary>
 /// Toggle between the switch material and the default.
 /// </summary>
 public void ToggleMaterial(){
     switched = !switched;
     if (switched){
         SwitchMaterial();
     }else{
         ResetMaterial();
     }
 }
 /// <summary>
 /// Revert to the default material
 /// </summary>
 public void ResetMaterial(){
     switched = false;
     foreach( Renderer _renderer in _renderers){
     _renderer.material = _matList[_renderer] as Material;
     }
 }

 /// <summary>
 /// Switch to the predefined switchMaterial
 /// </summary>
 public void SwitchMaterial(){
     switched = true;
     SetMaterial(switchMaterial);
 }   

 /// <summary>
 /// If you want to assign an arbitrary material
 /// </summary>
 /// <param name="mat">
 /// A <see cref="Material"/>
 /// </param>
 public void SetMaterial(Material mat){
     switched = true;
     if (mat==null){
         Debug.LogWarning("no Material defined to set on GameObject: "+name);
         return;
     }
     foreach( Renderer _renderer in _renderers){
         _renderer.material = mat;
     }
 }

}

Bye,

Jean

Comment
Add comment · Show 2 · 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 ina · May 04, 2011 at 10:08 PM 0
Share

Yes, more details please!

avatar image Jean-Fabre · May 05, 2011 at 04:52 AM 0
Share

Edited answer and put a working script.

avatar image
0

Answer by Bryan 4 · May 03, 2011 at 09:43 PM

you would need the game objects you want to change, then you can set the gameObject.renderer.material or gameObject.renderer.materials[] for multiple materials if necessary.

Comment
Add comment · Show 3 · 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 ina · May 03, 2011 at 10:56 PM 0
Share

is there a way to fetch all gameobjects at runtime?

avatar image Quazistax · May 04, 2011 at 09:48 AM 0
Share

Slow way (not wise to use it every frame), but it works: GameObject[] objects = (GameObject[]).FindObjectsOfType(typeof(GameObject)); Works just for active objects, of course :) Or you could get all LineRenderers: FindObjectsOfType(typeof(LineRenderer))

avatar image Quazistax · May 04, 2011 at 11:54 AM 0
Share

This one is not documented... but should be better to use in this case: FindSceneObjectsOfType(typeof(GameObject)) http://www.google.com/search?hl=en&q=FindSceneObjectsOfType

avatar image
0

Answer by David St-Hilaire · May 04, 2011 at 12:51 PM

I agree with Jean, I think that for each object that you want to switch materials, you should add a component on that object that will perform the switch. This component can look maybe like:

public Material NewMaterial;

public void Awake() { renderer.material = NewMaterial; }

If you need to have a more complex logic to choose between multiple materials, you can add it to this class and change the material to the desired one. This more distributed approach seems more in the Unity way of doing things, which I like alot ;p

-- David

Comment
Add comment · 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

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

Load data from file, if scene changed, fails to load location 0 Answers

Changing material during runtime 1 Answer

GameObject references runtime script in scene file. Unsure whats wrong 2 Answers

How can I load 3d models(.obj, .fbx) dynamically in to unity scene after building it to WebGL ? 1 Answer

Create scenes at runtime 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