I’m trying to change the color of a button depending on an array. Is there any way I can dynamically change the color of a button? It should be under the image class, but it’s not.
If not, can I do a similar procedure, but with sprites? In the future, I plan to change the sprite based on the array, but until then I want to just use colors for debugging. Or I could just add dumb images.
Problem solved, was easy. I just didn’t think it through.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ColorActivity : MonoBehaviour {
//Reference to button to access its components
private Button theButton;
//this get the Transitions of the Button as its pressed
private ColorBlock theColor;
// Use this for initialization
void Awake () {
theButton = GetComponent<Button>();
theColor = GetComponent<Button>().colors;
}
/// <summary>
/// Example
/// just add this to your Button component On Click()
/// </summary>
public void ButtonTransitionColors()
{
theColor.highlightedColor = Color.blue;
theColor.normalColor = Color.cyan;
theColor.pressedColor = Color.green;
theButton.colors = theColor;
print("Cliked");
}
}
then just add it to the button on click
hope it helps
Does any one here knows how to change image color through time, so that when I change the image color it doesn’t change automatically, instead it changes slowly! Thank you
just animate button’s Image component color, by changing it over time… it will affect whole button color.
Or alternatively animate Button component colors, these are contained in ColorBlock, which you can get, update and then put back, so you can animate normal color only, for example.
You could use animation clip, curve + time, sin wave - whatever is right for your purpose.