- Home /
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
What do you actually mean with "on" and "off"?
Brighter and darker?
Colored and colorless?
$$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.
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.
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.
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?
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.
Thanks although it kind of sucks I can do this with every material in my project? :)
If so Thanks for the answer
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)
{
...
}
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
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
![](https://koobas.hobune.stream/wayback/20220613113426im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to stick 2D textures? 0 Answers
Unity5 Android mip-mapping problem 1 Answer
When does texture max-size override take effect? 1 Answer
Blurry Android Textures 3 Answers
Android (Galaxy Tab 2): Sprites are black when mipmaps are disabled 1 Answer