Get sprite color r,g,b,a, values c#
How do I access sprite's color values? (line 16)
using UnityEngine;
using System.Collections;
public class scr_spriteFade : MonoBehaviour {
private float orgR;
private float orgG;
private float orgB;
private float orgA;
public float alphaAmount = 63.75f;
public float fadeTime = 1.0f;
// Use this for initialization
void Start () {
Color32 orgColor = /*somehow get the original r,g,b,a channel values and store them in "orgColor" */ ;
orgR = orgColor.r;
orgG = orgColor.g;
orgB = orgColor.b;
orgA = orgColor.a;
}
}
Like when you want transform's values, you'd use something like:
Vector3 orgPos = transform.localPosition;
Is there a similar way to access the color values?
Answer by Dave-Carlile · Dec 14, 2015 at 01:23 PM
If you take a look at the Sprite documentation you'll see that it refers to the SpriteRenderer as the component that is responsible for displaying the sprite graphic. Looking at that component you'll see that it has a color property.
So given your sprite game object you need to use GetComponent to get a reference to the SpriteRenderer, and that will give you access to the color property.
$$anonymous$$ay I ask for an example code for how to get current color values?
It's just a property on the sprite renderer component...
Color color = renderer.color;
void Start () {
gameObject.GetComponent<SpriteRenderer> ();
Color color = renderer.color;
}
does not work.
Assets/scripts/environment/scr_spriteFade.cs(17,31): error CS0619: `UnityEngine.Component.renderer' is obsolete: `Property renderer has been deprecated. Use GetComponent<Renderer>() ins$$anonymous$$d. (UnityUpgradable)'
Assets/scripts/environment/scr_spriteFade.cs(17,40): error CS1061: Type `UnityEngine.Component' does not contain a definition for `color' and no extension method `color' of type `UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
Answer by wibble82 · Dec 14, 2015 at 02:11 PM
Try this: using UnityEngine; using System.Collections;
public class scr_spriteFade : MonoBehaviour {
private Color original_color;
public float alphaAmount = 63.75f;
public float fadeTime = 1.0f;
// Use this for initialization
void Start () {
original_color = GetComponent<SpriteRenderer>().color;
}
}
So I'm getting the sprite renderer component in 'start', and storing the color. Note that I'm not storing the initial value as separate floats - its a color, so I'm storing it as a color!
-Chris
That's the thing. I need to store the values separately.
Well you can, but I would question whether you need to. The values can be accessed on 'original_color' easily:
//print out the red bit of original color
Debug.Log(original_color.r)
//add 0.1 to the original color
original_color.r += 0.1f;
//create a new color which is the original tweaked a bit!
Color new_color = original_color;
new_color.r += 0.1f;
new_color.g = 0.0f;
But either way, the first line is key - it reads the color value out of the sprite renderer. Whether you choose to store the r/g/b/a values separately or not is up to you!
As a general piece of advice, good practice when program$$anonymous$$g is store data in a way that represents what it means! So if you have a set of numbers that represent a color, store them as a color. This makes your code easier to read, and means that all of unity's 'color' related functions will work with it.
Obviously you can store the red/green/blue/alpha values separately, but the is no gain. The 'color' structure stores 4 floats internally that you can access easily.
If you're confused, let me know what you're trying to do and I'll see if I can help explain why storing the numbers separately is unnecesary.
-Chris
Somehow line
sprRenderer.color = new Color32 (orgR, orgG, orgB, alphaAmount);
doesnt work. If I input values between 0-255 it works but orgR/G/B/A doesn't, even though they are floats.
Here is the current code: using UnityEngine; using System.Collections;
public class scr_spriteFade : $$anonymous$$onoBehaviour {
private Color32 originalColor;
private float orgR;
private float orgG;
private float orgB;
private float orgA;
public float alphaAmount = 63.75f;
public float fadeTime = 1.0f;
// Use this for initialization
void Start () {
originalColor = GetComponent<SpriteRenderer> ().color;
orgR = originalColor.r;
orgG = originalColor.g;
orgB = originalColor.b;
orgA = originalColor.a;
}
void OnTriggerEnter2D(Collider2D collider) {
SpriteRenderer sprRenderer = GetComponent<SpriteRenderer>();
sprRenderer.color = new Color32 (orgR, orgG, orgB, alphaAmount);
}
void OnTriggerExit2D(Collider2D collider) {
}
}