SwitchButtonCollor.cs 1.25 KB
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class SwitchButtonCollor : MonoBehaviour {

	public Button thisButton;
	public GameObject reference;
	private Graphic thisButtonGraphic;

	public bool isEnabled;
	private static Color enabledColor = new Color(0.356F, 0.78F, 0.815F, 1F);
	private static Color disabledColor = new Color(1F, 1F, 1F, 1F);


	void Start ()
	{
		if (thisButton == null)
			thisButton = (Button) gameObject.GetComponent<Button>();

		thisButtonGraphic = thisButton.GetComponent<Graphic>();

		if (reference == null)
			updateColor();
		else
			switchColorByReference();
	}

	void OnEnable ()
	{
		if (thisButtonGraphic != null && reference != null)
			switchColorByReference();
	}


	public void updateColor()
	{
		thisButtonGraphic.color = isEnabled ? enabledColor : disabledColor;
	}

	public void switchColor(bool isEnabled)
	{
		this.isEnabled = isEnabled;
		updateColor();
	}
	public void switchCollor() {
		switchColor( ! isEnabled);
	}

	public void switchColorByReference()
	{
		switchColorByReference(reference);
	}
	public void switchColorByReference(GameObject reference)
	{
		ColorChangeState state = reference.GetComponent<ColorChangeState>();
		if (state != null) switchColor(state.getColorChangeState());
	}

}