Home > Enterprise >  Why when setting the alpha to 0 or 255 or any value it's not affecting at all the result of the
Why when setting the alpha to 0 or 255 or any value it's not affecting at all the result of the

Time:12-20

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class ReadPixelsFromImage : MonoBehaviour
{
    public Texture2D tx2d;
    public RawImage ri;

    // Start is called before the first frame update
    void Start()
    {
        ReadPixelsFromT2D(tx2d);
        ri.texture = tx2d;
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void ReadPixelsFromT2D(Texture2D Texture)
    {
        Color[] colors = Texture.GetPixels();
        
        for (int i = 0; i < colors.Length; i  )
        {
            if (i > 20000 && i < 100000)
            {
                colors[i] = new Color(255,0,0,0);
            }
        }

        Texture.SetPixels(colors);
        Texture.Apply();
    }
}

In the new Color i'm setting the most right value the alpha to 0 tried 150 tried 255 but it's not changing anything the color of the pixels is red but not transparent i mean i want to color the pixels in red but keep the image not just fill it in red.

i want to make the same like when you make it gray scale for example.

this is the result :

red

CodePudding user response:

I guess you need to set the value between 0f-1f.

https://docs.unity3d.com/ScriptReference/Color-ctor.html

CodePudding user response:

Color (uses float 0-1) != Color32 (uses byte 0-255)


Note that still your shader/material needs to support transparency (might e.g. need to change Opaque to Fade/Transparent) depending on the used shader.


Easier would be you exposed this in the Inspector and there adjust the target color without needing to recompile everytime

[SerializeField] private Color _color;

...

colors[i] = _color;
  • Related