Search code examples
unity-game-engineuser-interfaceslidertransparencyrawimage

How To Change RawImage Transparency using Slider


How can I change transparency of Raw Image, without Material rendering? I am trying to create the app for image tracing. I use Nature Gallery script to upload image from Gallery to RawImage. I do it using Texture. When I try to add Material, it cover the texture and nothing work..)

Thank you!

I think, I have to call RawImage.color, but not sure how?


Solution

  • To change the transparency of a RawImage using a slider you can do the following:

    // Image who's transparency you want to change via slider
    public RawImage image;
    
    // Callback for slider value change
    public void OnSliderValueChanged(float sliderValue)
    {
        // Get current color including transparency
        Color currentColor = image.color;
    
        // Apply slider value as transparency (slider range: 0 - 1 fits perfectly)
        image.color = new Color(currentColor.r, currentColor.g, currentColor.b, sliderValue);
    }
    

    Don't forget to reference your RawImage in the inspector on your script. Also, on your slider component, add a OnValueChanged() event and assign the OnSliderValueChanged(float sliderValue) method under Dynamic float (not Static Parameters) as the corresponding callback. See the following images as a help: