• 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 02, 2013 at 10:40 AM · androidtexturestoggleandroid tab

Toggle Textures on and off

Hi there,

In my game I would like to be able to toggle Textures on and off.

Does anyone have experience doing this? or can offer some ideas how to do this?

I'm also doing this on the android platform.

Thanks in advance

Comment
Add comment · Show 7
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 bruce965 · Sep 02, 2013 at 11:45 AM 0
Share

What do you actually mean with "on" and "off"?

Brighter and darker?

Colored and colorless?

avatar image $$anonymous$$ · Sep 02, 2013 at 12:41 PM 0
Share

$$anonymous$$aybe he means to hide/show which is not just the texture then. You can use render.enabled = false, it will hide the whole mesh.

avatar image Exalia · Sep 02, 2013 at 01:31 PM 0
Share

I'm not sure how there is confusion :| when you render with a material you either have a texture or you don't, I wan't to be able to disable all textures at run time. and then enable them.

avatar image $$anonymous$$ · Sep 02, 2013 at 01:38 PM 0
Share

The confusion is derived from the fact that you can't turn a texture off without doing so to the mesh too, you can change material to something else, but you won't have an "off" texture option. So you want to hide the mesh too? To do so read my comment above.

avatar image bruce965 · Sep 02, 2013 at 01:41 PM 0
Share

Yeah, "Enable" and "Disable" makes more sense... Then I have another question for you before posting an answer: Do you want to Enable/Disable ALL textures in the scene at once, or just for a single mesh?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robhuhn · Sep 02, 2013 at 02:02 PM

I'm also a bit confused but I guess by saying 'on' and 'off' you mean something like adding a texture to a material and removing it from the material (same as assigning 'none' in the inspector). That would be accomplished by assigning the texture or null:

 void OnGUI()
 {
     if(GUI.Button(new Rect(10, 10, 150, 100), "toggle"))
     {
         isOn = !isOn;
        renderer.material.mainTexture = isOn ? texture : null;
     }
 }

@edit

or you would use sharedMaterial

 renderer.sharedMaterial.mainTexture = isOn ? texture : null;

for all objects using that material.

Comment
Add comment · Show 5 · 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 02, 2013 at 02:23 PM 0
Share

Thanks although it kind of sucks I can do this with every material in my project? :)

If so Thanks for the answer

avatar image robhuhn · Sep 02, 2013 at 02:36 PM 0
Share

If you want to get all materials you would have to find the renderers first like @bruce965 said.

 Renderer[] renderers = (Renderer[])FindObjectsOfType(typeof(Renderer));
 foreach (Renderer renderer in renderers)
 {
         ...
 }
avatar image Exalia · Sep 02, 2013 at 03:09 PM 0
Share

Yes this is perfect thank you very much :)

avatar image Exalia · Sep 09, 2013 at 11:27 AM 0
Share

Final Code I Used :

 using UnityEngine;
 using System.Collections;
 
 public class TexturesScript : $$anonymous$$onoBehaviour {
 
     public Texture texture;
     public bool isOn = true;
 
     void Start () 
     {
         isOn = !isOn;
 
         Renderer[] renderers = (Renderer[])FindObjectsOfType(typeof(Renderer));
 
         foreach (Renderer renderer in renderers)
         {
             renderer.shared$$anonymous$$aterial.mainTexture = isOn ? texture : null;
         }
     }
     
     void Update () 
     {
         if(Input.GetButtonUp("Jump"))
         {
             isOn = !isOn;
             Renderer[] renderers = (Renderer[])FindObjectsOfType(typeof(Renderer));
 
             foreach (Renderer renderer in renderers)
             {
                 renderer.shared$$anonymous$$aterial.mainTexture = isOn ? texture : null;
             }
         }
     }
 }
 

Thanks again to Robhuhn

avatar image Exalia · Sep 09, 2013 at 11:39 AM 0
Share

Actually I've just found a problem, This script completely reassigns the texture I've set, it works on all textures and 'turns them off' but If I want to turn them back on it doesn't work correctly. I'll have to store the old texture somewhere and restore it

If I get it working I'll post it back here as an answer :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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 avatar image avatar image

Related Questions

Android textures not showing properly 0 Answers

Sprite-based animation - mobile memory issue 0 Answers

Android World High Score 1 Answer

Texture become dirty on Android 1 Answer

Android tablet crashes depending on texture compression 0 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