Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Trying to make a Canvas Fader

Discussion in 'Scripting' started by jonathanrhcooper, Apr 18, 2021.

  1. jonathanrhcooper

    jonathanrhcooper

    Joined:
    Dec 18, 2020
    Posts:
    15
    I have been trying a couple of different ways to get a canvas fader to work and just seem to be having an issue with this concept. Right now I have made a functioning one that I can with a cube but I am having issue to get one that works with A canvas Panel. (Background) Currently we have a system that customers can come through and use a physical slider to fade through images of presidents to see before and after but we want to make that digitial with a UI slider as the physical one continuously breaks.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class scr : MonoBehaviour
    8. {
    9.     public GameObject currentGameObject;
    10.     public float alpha = 0.5f ;
    11.     private Material currentMat;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         currentGameObject = gameObject;
    16.         currentMat = currentGameObject.GetComponent<Renderer>().material;
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void ChangeAlpha(Material mat, float alphaVal)
    21.     {
    22.         Color oldColor = mat.color;
    23.         Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, alphaVal);
    24.         mat.SetColor("_Color", newColor);
    25.     }
    26.  
    27.     public void ChangeAlphaonSlider(Slider slider)
    28.     {
    29.         ChangeAlpha(currentMat, slider.value);
    30.     }
    31. }
    32.  
     
  2. styrbo likes this.