Tanoda
ColorSampler.cs
Go to the documentation of this file.
1
4
6
8{
17 public class ColorSampler : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
18 {
19 private Vector2 m_screenPos;
20
21 [SerializeField]
22 protected Button sampler;
23 private RectTransform sampleRectTransform;
24
25 [SerializeField]
26 protected Outline samplerOutline;
27
28 protected Texture2D screenCapture;
29
31
32 protected Color color;
33
34 protected virtual void OnEnable()
35 {
36 screenCapture = ScreenCapture.CaptureScreenshotAsTexture();
37 sampleRectTransform = sampler.GetComponent<RectTransform>();
38 sampler.gameObject.SetActive(true);
39 sampler.onClick.AddListener(SelectColor);
40 }
41
42 protected virtual void OnDisable()
43 {
44 Destroy(screenCapture);
45 sampler.gameObject.SetActive(false);
46 sampler.onClick.RemoveListener(SelectColor);
47 }
48
49 protected virtual void Update()
50 {
51 if (screenCapture == null)
52 return;
53
54 sampleRectTransform.position = m_screenPos;
55 color = screenCapture.GetPixel((int)m_screenPos.x, (int)m_screenPos.y);
56
58 }
59
60 protected virtual void HandleSamplerColoring()
61 {
62 sampler.image.color = color;
63
65 {
66 var c = Color.Lerp(Color.white, Color.black, color.grayscale > 0.5f ? 1 : 0);
67 c.a = samplerOutline.effectColor.a;
68 samplerOutline.effectColor = c;
69 }
70 }
71
72 protected virtual void SelectColor()
73 {
74 if (oncolorSelected != null)
75 oncolorSelected.Invoke(color);
76
77 enabled = false;
78 }
79
80 public void OnPointerDown(PointerEventData eventData)
81 {
82 m_screenPos = eventData.position;
83 }
84
85 public void OnPointerUp(PointerEventData eventData)
86 {
87 m_screenPos = Vector2.zero;
88 }
89
90 public void OnDrag(PointerEventData eventData)
91 {
92 m_screenPos = eventData.position;
93 }
94 }
95}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Color Color
Definition: TestScript.cs:32
Samples colors from a screen capture. Warning! In the editor if you're not in Free aspect mode then t...
Definition: ColorSampler.cs:18
void OnPointerUp(PointerEventData eventData)
Definition: ColorSampler.cs:85
void OnDrag(PointerEventData eventData)
Definition: ColorSampler.cs:90
void OnPointerDown(PointerEventData eventData)
Definition: ColorSampler.cs:80