Globally transparency texture - script

Hello guys,

I create script to change texture transparency:

using UnityEngine;
using System.Collections;

public class ChangeAlpha : MonoBehaviour {

    public KeyCode minalpha;
    public KeyCode maxalpha;
    public float alphalevel = 1.0f;

    // Use this for initialization
    void Start () {  
    }
  
    // Update is called once per frame
    void Update () {

        if (Input.GetKeyDown(maxalpha))
            alphalevel += .5f;

        if (Input.GetKeyDown(minalpha))
            alphalevel -= .5f;

      GetComponent<MeshRenderer>().material.color = new Color(1, 1, 1, alphalevel);

    }
}

I would like one button to enable transparency of several textures. Is it possible?
how can I choose textures which I would like to edit? :slight_smile:

Please help me.

Well you have two options. If you only have a few textures you need to change and you never anticipate adding a lot more later, then a quick way to do it, is just add hooks to those GameObjects in this script:

using UnityEngine;
using System.Collections;
public class ChangeAlpha : MonoBehaviour {
    public KeyCode minalpha;
    public KeyCode maxalpha;
    public float alphalevel = 1.0f;
    // Drag these gameObjects here in the inspector
    public GameObject[]  stuffToChange;
    // Use this for initialization
    void Start () {
    }
    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown(maxalpha))
         {
            alphalevel += .5f;
            ChangeAlpha(); 
        }
        if (Input.GetKeyDown(minalpha))
        {
            alphalevel -= .5f;
            ChangeAlpha();
        }
     
    }

     void ChangeAlpha()
     {
        
      foreach(GameObject oneObject in stuffTochange)
      {
            MeshRenderer rend = oneObject.GetComponent<MeshRenderer>();
            Color oldColor = rend.material.color;
            oldColor.a = alphaLevel;
            rend.material.color = oldColor;
      }
}
}

However, if you want a reusable script that can handle a large number of changeable objects you should probably implement and event. Fire off a ChangedAlpha Event with the new alpha level. And any object that needs to be changed would subscribe to this event, and change its own alpha when it receives it.

1 Like

I thought I should post the event solution as well:

First you need an EventArgument Script. This script shouldn’t be attached to any GameObject:

using UnityEngine;
using System;
using System.Collections;

public class AlphaChangedEventArgs : EventArgs
{
    public float alpha;
}

Then you you need your ChangeAlpha script with the EVent code added in
This code should be attached to a GameObject in your scene
ChangeAlpha with Event

using UnityEngine;
using System;
using System.Collections.Generic;

public class ChangeAlpha : MonoBehaviour
{
    public KeyCode minalpha;
    public KeyCode maxalpha;
    public float alphalevel = 1.0f;

    public event EventHandler<AlphaChangedEventArgs> AlphaChanged;

    protected virtual void OnAlphaChanged(AlphaChangedEventArgs e)
    {
        EventHandler<AlphaChangedEventArgs> handler = AlphaChanged;
        if (handler != null)
            handler(this, e);
    }

    void Update()
    {
        if (Input.GetKeyDown(maxalpha))
        {
            alphalevel += .5f;
            FireEvent();
        }
        if (Input.GetKeyDown(minalpha))
        {
            alphalevel -= .5f;
            FireEvent();
        }
    }

    void FireEvent()
    {
        AlphaChangedEventArgs e = new AlphaChangedEventArgs();
        e.alpha = alphalevel;
        OnAlphaChanged(e);
    }

}

Then here is how you would implement it on another object:
This Code should be attached to a different GameObject in your scene. And actually to every GO that you need the transparency changed on:
GameObject code to Catch The Event

using UnityEngine;
using System;
using System.Collections.Generic;
using UnityEngine.UI;

public class GenericTest : MonoBehaviour
{
    void Start()
    {
        GameObject changeAlphaGO = GameObject.FindGameObjectWithTag("ChangeAlpha");
        ChangeAlpha changeAlpha = changeAlphaGO.GetComponent<ChangeAlpha>();
        changeAlpha.AlphaChanged += this.AlphaChanged;
   
    }
    void AlphaChanged(object sender, AlphaChangedEventArgs e)
    {
        Debug.Log("Changing Alpha to " + e.alpha);
    }
}

If you run this code and press the Keys you assign to increase and decrease you will see the 2nd GameObject catches the event and prints out a Debug. You would just change to the actual code to change this object’s transpraency

EDIT: Just to note; I added the tag “ChangeAlpha” to the Event Handler script, so all my objects could find it. IF you don’t want to do that you could also add this code to every GameObject that needed to find it :

public GameObject changedAlphaGO;

and drag the Event Handling GameObject onto that variable in the inspector. But this will get tedious if you have to do it to a lot of objects… Easier to use a tag.

1 Like

Your answer is very extensive and almost all I explained. ^^
I want to write a simple script so would use the first option.

but you write // Drag these gameObjects here in the inspector

how it should look like?

in my project I want to turn on and off a few textures transparency, will be a constant value.

If you put this line in your script:

public GameObject[]  stuffToChange;

You will notice stuffToChange will show up in the inspector with a down array
If you click that the size will say 0. Change that number to how many textures you need to change.
Then each GameObject that has a texture to change, drag it onto one of the spots in stuffToChange