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 Exalia · Sep 09, 2013 at 11:43 AM · androidtexturerenderertoggle

Toggle Textures Script

I've been asking for help on creating a script that toggles all textures in the scene on and off. I'm currently using this script which works to an extent. It toggles textures off and on however upon toggling back on the textures are default textures. I require the old Texture to be able to come back for this script to be considered fully functional. I'll be working on this for a while and post back an answer If I find a solution. Untill then I'd appreciate any ideas on how to achieve this. Thanks :)

 using UnityEngine;
 using System.Collections;
 
 public class TexturesScript : MonoBehaviour {
 
     public Texture texture;
     public bool isOn = true;
 
     void Start () 
     {
         isOn = !isOn;
 
         Renderer[] renderers = (Renderer[])FindObjectsOfType(typeof(Renderer));
 
         foreach (Renderer renderer in renderers)
         {
             renderer.sharedMaterial.mainTexture = isOn ? texture : null;
         }
     }
     
     void Update () 
     {
         if(Input.GetButtonUp("Jump"))
         {
             isOn = !isOn;
             Renderer[] renderers = (Renderer[])FindObjectsOfType(typeof(Renderer));
 
             foreach (Renderer renderer in renderers)
             {
                 renderer.sharedMaterial.mainTexture = isOn ? texture : null;
             }
         }
     }
 }
 
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
Best Answer

Answer by robhuhn · Sep 09, 2013 at 11:51 AM

Store the textures on start and map them to the renderer instead of passing it through the editor (code not testet):

 using System.Collection.Generic;
 ...

 private Dictionary<Renderer, Texture> textureForRenderer = new Dictionary<Renderer, Texture>();

 void Start () 
 {
     isOn = !isOn;
 
     Renderer[] renderers = (Renderer[])FindObjectsOfType(typeof(Renderer));
 
     foreach (Renderer renderer in renderers)
     {
         textureForRenderer[renderer] = renderer.sharedMaterial.mainTexture
         renderer.sharedMaterial.mainTexture = isOn ? texture : null;
     }
 }

Get the desired texture using the renderer object:

 foreach (Renderer renderer in renderers)
 {
     renderer.sharedMaterial.mainTexture = isOn ? textureForRenderer[renderer] : null;
 }
Comment
Add comment · Show 11 · 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 Exalia · Sep 09, 2013 at 11:55 AM 0
Share

Wow thanks again. Nice speedy answer too :) <3

avatar image Exalia · Sep 09, 2013 at 12:17 PM 0
Share

I've tested this script out and I'm getting the error "The given key was not present in the dictionary"

any idea what could be going wrong? :(

avatar image Exalia · Sep 09, 2013 at 12:22 PM 0
Share

I think I've found an issue, If there are 2 objects in the scene using the same renderer

The texture will be saved the first time and the current texture will be set to null.

The next time the foreach loop finds the same object the current texture will be null and it will overwrite the dictionary

avatar image Exalia · Sep 09, 2013 at 12:39 PM 0
Share

Done some more testing and another problem seems to be reading textures from the dictionary. I'm not sure if the syntax is incorrect of if there is something wrong with the code but

renderer.shared$$anonymous$$aterial.mainTexture = isOn ? textureForRenderer[renderer] : null;

Produces errors

avatar image robhuhn · Sep 09, 2013 at 01:24 PM 0
Share

The syntax should be ok. First of all I would declare Renderer[] renderers as a private field variable, there is no need to fetch it every time I guess.

Defining the Renderer/Texure dictionary is fine too but a $$anonymous$$or improvement would be to change this to $$anonymous$$aterial/Texture:

 private Dictionary<$$anonymous$$aterial, Texture> textureFor$$anonymous$$aterial = new Dictionary<$$anonymous$$aterial, Texture>();
 textureFor$$anonymous$$aterial[renderer.shared$$anonymous$$aterial] = renderer.shared$$anonymous$$aterial.mainTexture;

If there are 2 objects in the scene using the same renderer

Don't know how it would be possible that two objects use the same renderer instance.

Show more comments

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

www.texture results in black texture in Android 2 Answers

how to make high quality textures with low size of downlaod 1 Answer

Toggle button GUI texture not changing on click? 0 Answers

null texture passed to GUI.DrawTexture 0 Answers

A node in a childnode? 1 Answer


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