Search Unity

How can I make 3 modes for changing the alpha color ?

Discussion in 'Scripting' started by haimmoshe, Feb 16, 2018.

  1. haimmoshe

    haimmoshe

    Joined:
    Jun 3, 2017
    Posts:
    237
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. [ExecuteInEditMode]
    7. public class InsideVisible : MonoBehaviour
    8. {
    9.    public float fadeDuration = 4f;
    10.    public bool fade = false;
    11.    [Range(0, 1)]
    12.    public float fader = 0;
    13.  
    14.    private Material _materialCopy;
    15.    private Color color;
    16.  
    17.    private void Start()
    18.    {
    19.        if (_materialCopy == null)
    20.            _materialCopy = GetComponent<Renderer>().sharedMaterial;
    21.    }
    22.  
    23.    private void Update()
    24.    {
    25.        if (fade == false)
    26.        {
    27.            color.a = fader;
    28.            _materialCopy.SetColor("Color (RGBA)", color);
    29.            _materialCopy.color = color;
    30.        }
    31.        else
    32.        {
    33.            if (Input.GetKeyDown(KeyCode.S))
    34.            {
    35.                if (color.a == 1)
    36.                {
    37.                    color.a = 0;
    38.                    _materialCopy.SetColor("Color (RGBA)", color);
    39.                    _materialCopy.color = color;
    40.                    StartCoroutine(FadeTo(1f, 7f));
    41.                }
    42.                else
    43.                {
    44.                    color.a = 1;
    45.                    _materialCopy.SetColor("Color (RGBA)", color);
    46.                    _materialCopy.color = color;
    47.                    StartCoroutine(FadeTo(0f, 7f));
    48.                }
    49.            }
    50.        }
    51.    }
    52.  
    53.    IEnumerator FadeTo(float endAlpha, float durationSeconds)
    54.    {
    55.        // Cache the material so we don't need to re-get it all the time,
    56.        // and can clean it up when we're done with it.
    57.        //if (_materialCopy == null)
    58.        //  _materialCopy = GetComponent<Renderer>().material;
    59.  
    60.        // Record the starting colour so we can preserve it.
    61.        Color color = _materialCopy.color;
    62.        // Record the starting alpha so we know where we're fading from.
    63.        float startAlpha = color.a;
    64.  
    65.        for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / durationSeconds)
    66.        {
    67.            // Lerp between start and end, not between current and end.
    68.            color.a = Mathf.Lerp(startAlpha, endAlpha, t);
    69.            _materialCopy.color = color;
    70.            yield return null;
    71.        }
    72.  
    73.        // Ensure we end exactly where we want, not off by any fraction.
    74.        color.a = endAlpha;
    75.        _materialCopy.color = color;
    76.    }
    77.  
    78.    // Add a method to clean up our local material copy when we're done with it,
    79.    // so it doesn't clutter up memory until the next scene change.
    80.    void OnDestroy()
    81.    {
    82.        if (_materialCopy != null)
    83.        {
    84.          //DestroyImmediate(_materialCopy);
    85.        }
    86.    }
    87. }
    88.  
    I want to have 3 modes or options:

    1. If fade is false use the range slider. (This is working)

    2. If fade is true use the S key to change between 0 and 1 each press on the S key should stop the current fading if in the middle and the start to the other direction.

    3. Another key if pressed once it will keep fading automatic between 0 and 1 non stop until I will press once on the same key again for example key N.
    Number 1 should work in editor and also when the game is running. The rest 2 and 3 only when the game is running.

    Now what it does when in editor I can change the slider but when running the game and pressing on S it will fade once or two and then will make the cube object one deep black color and will not return to the original color/texture.

    A mess. I can't make 2 and 3 at all.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Is your situation you described at the end happening if you wait for the coroutine to finish, or only if you interrupt it with the 's' key while it's going?
     
    haimmoshe likes this.
  3. haimmoshe

    haimmoshe

    Joined:
    Jun 3, 2017
    Posts:
    237
    At the end it happen when I press on S and coroutine finished but I did it all a big mess.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If you're not changing anything but the alpha, I think you might want to try getting the colour in Start() and you can also remove it from the coroutine (getting it).

    As for stopping the change, store the coroutine in a variable when you create it. Set it to null at the end of the coroutine & check if it's not null before your stop it.

    Hope that helps a bit. I was getting the black, too, when I tested this. But it's not doing that for me now. I didn't actually try the other part I described, but I'm pretty sure it will work.