ShapesGameDev
ShapesGameDev

Reputation: 3

How to change quad transparency in Unity 2D?

I'm making a kind of space-shooter game which has a simplistic geometric style. I got a scrolling background and put it in as a quad. The scrolling background works, but I thought it was a little to bright so I wanted to change the opacity when I realized. "Oh wow, I'm a stupid brain new developer, and I don't know anything about quads or 3D repeating textures and how they behave in a 2D game, since all I've been doing is following a tutorial on how to implement a scrolling background." So I just need some simple code that will change the transparency of my quad.

Upvotes: 0

Views: 2485

Answers (1)

Hassan Omar
Hassan Omar

Reputation: 94

First step you should change the material rendering mode "the material which assigned to quad or any 3d model " change it to Fade or transparent
enter image description here


Now You Can change the opacity of the color on material this will be applied
enter image description here


Second Step change color from code just you need a reference for MeshRenderer Component

 public MeshRenderer quad; // quad to change its opacity 

 public void setOpacity(float opacity)
 {
    Color oldColor =  quad.sharedMaterial.color; // old color 
    // now will set old color but with new opacity 
    quad.sharedMaterial.color = new Color(oldColor.r,oldColor.g,oldColor.g,opacity);
 }

what is occurs here? Color Component use rgba coloring system
r for red value , g for green , b for blue , a for alpha value
all from 0 to 1 so when you set alpha value = 1; this means color is non transparent
when you set alpha value = 1; this means color is completely transparent 0

Upvotes: 1

Related Questions