ChangeColor.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using UnityEngine.UI;
  4. public class ChangeColor : MonoBehaviour, IPointerClickHandler
  5. {
  6. void OnEnable ()
  7. {
  8. }
  9. public void SetRed(float value)
  10. {
  11. OnValueChanged(value, 0);
  12. }
  13. public void SetGreen(float value)
  14. {
  15. OnValueChanged(value, 1);
  16. }
  17. public void SetBlue(float value)
  18. {
  19. OnValueChanged(value, 2);
  20. }
  21. public void OnValueChanged(float value, int channel)
  22. {
  23. Color c = Color.white;
  24. if (GetComponent<Renderer>() != null)
  25. c = GetComponent<Renderer>().material.color;
  26. else if (GetComponent<Light>() != null)
  27. c = GetComponent<Light>().color;
  28. c[channel] = value;
  29. if (GetComponent<Renderer>() != null)
  30. GetComponent<Renderer>().material.color = c;
  31. else if (GetComponent<Light>() != null)
  32. GetComponent<Light>().color = c;
  33. }
  34. public void OnPointerClick(PointerEventData data)
  35. {
  36. if (GetComponent<Renderer>() != null)
  37. GetComponent<Renderer>().material.color = new Color(Random.value, Random.value, Random.value, 1.0f);
  38. else if (GetComponent<Light>() != null)
  39. GetComponent<Light>().color = new Color(Random.value, Random.value, Random.value, 1.0f);
  40. }
  41. }