- Home /
Change Alpha on individual Texture2Ds
I want to change just the alpha of individual GUI textures.
I can do this in the OnGuI section like this
var alpha :float;
GUI.color = new Color(1,1,1,alpha);
GUI.Label (Rect (100,10,128,128), aTexture2D);
GUI.Label (Rect (200,10,128,128), bTexture2D);
GUI.Label (Rect (300,10,128,128), cTexture2D);
...
But is there not a more modular way such as putting them in an array, and then cycling through changing the alpha of each Texture2D element in the array, without having to keep writing the coordinates etc. each time?
Answer by Brian-Kehrer · Jan 23, 2010 at 06:05 PM
No, but it's easy to make one. Just set this up in Start or Awake, then Call item.DrawMe()
public class MyTexture2D{ Rect gRect; Texture2D tex; Color col;
public MyTexture2D(Rect theRect, Texture2D theTex, Color theColor){
gRect = theRect;
col = theColor;
tex = theTex;
}
public void DrawMe(){
Color tempCol = GUI.color;
GUI.color = col;
GUI.Label (gRect, tex);
GUI.color = tempCol;
}
public Color color{
get{
return col;
}
set{
col = value;
}
}
}
and example usage:
public class MyGUI : MonoBehaviour {
public Texture2D aTex;//actual texture
MyTexture2D thing;
public float alpha;
void Start(){
thing = new MyTexture2D(new Rect(100,10,128,128),
aTex,
new Color(1,1,1,alpha));
}
void OnGUI(){
thing.DrawMe();
}
}
Thanks $$anonymous$$ - that has answered 'several' questions.
When I test it on my demo, the following error appears:
ArgumentException: You can only call GUI functions from inside OnGUI.
Your answer
![](https://koobas.hobune.stream/wayback/20220612014442im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
change variable and Texture2D 1 Answer
Can I use a movie texture with alpha as a GUI object? 1 Answer
Alpha not working in GUITex 0 Answers