经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
用 Unity 实现调色板功能
来源:cnblogs  作者:晚安、枕头  时间:2019/9/27 9:36:16  对本文有异议

用unity 实现调色板功能。

直接上代码:

  1. 1 using UnityEngine;
  2. 2 using System.Collections;
  3. 3 using UnityEngine.UI;
  4. 4
  5. 5 public class ColorPick : MonoBehaviour
  6. 6 {
  7. 7
  8. 8 public Image Saturation;
  9. 9 public Image Hue;
  10. 10 public Image Paint;
  11. 11
  12. 12 public RectTransform Point_Stauration;
  13. 13 public RectTransform Point_Hue;
  14. 14
  15. 15 private Sprite Saturation_Sprite;
  16. 16 private Sprite Hue_Sprite;
  17. 17
  18. 18 private Color32 currentHue = Color.red;
  19. 19
  20. 20
  21. 21 private void Awake()
  22. 22 {
  23. 23
  24. 24 }
  25. 25
  26. 26 private void Start()
  27. 27 {
  28. 28 UpdateStauration();
  29. 29 UpdateHue();
  30. 30 }
  31. 31
  32. 32 float sWidth = 200, sHeight = 200;
  33. 33 //更新饱和度
  34. 34 private void UpdateStauration()
  35. 35 {
  36. 36
  37. 37 Saturation_Sprite = Sprite.Create(new Texture2D((int)sWidth, (int)sHeight), new Rect(0, 0, sWidth, sHeight), new Vector2(0, 0));
  38. 38
  39. 39
  40. 40 for (int y = 0; y <= sHeight; y++)
  41. 41 {
  42. 42 for (int x = 0; x < sWidth; x++)
  43. 43 {
  44. 44 var pixColor = GetSaturation(currentHue, x / sWidth, y / sHeight);
  45. 45 Saturation_Sprite.texture.SetPixel(x, ((int)sHeight - y), pixColor);
  46. 46 }
  47. 47 }
  48. 48 Saturation_Sprite.texture.Apply();
  49. 49
  50. 50 Saturation.sprite = Saturation_Sprite;
  51. 51 }
  52. 52
  53. 53 //更新色泽度
  54. 54 private void UpdateHue()
  55. 55 {
  56. 56
  57. 57 float w = 50, h = 50;
  58. 58
  59. 59 Hue_Sprite = Sprite.Create(new Texture2D((int)w, (int)h), new Rect(0, 0, w, h), new Vector2(0, 0));
  60. 60
  61. 61 for (int y = 0; y <= h; y++)
  62. 62 {
  63. 63 for (int x = 0; x < w; x++)
  64. 64 {
  65. 65 var pixColor = GetHue(y / h);
  66. 66 Hue_Sprite.texture.SetPixel(x, ((int)h - y), pixColor);
  67. 67 }
  68. 68 }
  69. 69 Hue_Sprite.texture.Apply();
  70. 70
  71. 71 Hue.sprite = Hue_Sprite;
  72. 72 }
  73. 73
  74. 74 private Vector2 clickPoint = Vector2.zero;
  75. 75 public void OnStaurationClick(ColorPickClick sender)
  76. 76 {
  77. 77 var size2 = Saturation.rectTransform.sizeDelta / 2;
  78. 78 var pos = Vector2.zero;
  79. 79 pos.x = Mathf.Clamp(sender.ClickPoint.x, -size2.x, size2.x);
  80. 80 pos.y = Mathf.Clamp(sender.ClickPoint.y, -size2.y, size2.y);
  81. 81 Point_Stauration.anchoredPosition = clickPoint = pos;
  82. 82
  83. 83 UpdateColor();
  84. 84 }
  85. 85
  86. 86 public void UpdateColor()
  87. 87 {
  88. 88 var size2 = Saturation.rectTransform.sizeDelta / 2;
  89. 89 var pos = clickPoint;
  90. 90 pos += size2;
  91. 91
  92. 92 var color = GetSaturation(currentHue, pos.x / Saturation.rectTransform.sizeDelta.x, 1 - pos.y / Saturation.rectTransform.sizeDelta.y);
  93. 93 Paint.color = color;
  94. 94 }
  95. 95
  96. 96 public void OnHueClick(ColorPickClick sender)
  97. 97 {
  98. 98 var h = Hue.rectTransform.sizeDelta.y / 2.0f;
  99. 99 var y = Mathf.Clamp(sender.ClickPoint.y, -h, h);
  100. 100 Point_Hue.anchoredPosition = new Vector2(0, y);
  101. 101
  102. 102 y += h;
  103. 103 currentHue = GetHue(1 - y / Hue.rectTransform.sizeDelta.y);
  104. 104 UpdateStauration();
  105. 105 UpdateColor();
  106. 106 }
  107. 107
  108. 108
  109. 109 private static Color GetSaturation(Color color, float x, float y)
  110. 110 {
  111. 111 Color newColor = Color.white;
  112. 112 for (int i = 0; i < 3; i++)
  113. 113 {
  114. 114 if (color[i] != 1)
  115. 115 {
  116. 116 newColor[i] = (1 - color[i]) * (1 - x) + color[i];
  117. 117 }
  118. 118 }
  119. 119
  120. 120 newColor *= (1 - y);
  121. 121 newColor.a = 1;
  122. 122 return newColor;
  123. 123 }
  124. 124
  125. 125
  126. 126 //B,r,G,b,R,g //大写是升,小写是降
  127. 127 private readonly static int[] hues = new int[] { 2, 0, 1, 2, 0, 1 };
  128. 128
  129. 129 private readonly static Color[] colors = new Color[] { Color.red, Color.blue, Color.blue, Color.green, Color.green, Color.red };
  130. 130
  131. 131 private readonly static float c = 1.0f / hues.Length;
  132. 132
  133. 133 private static Color GetHue(float y)
  134. 134 {
  135. 135 y = Mathf.Clamp01(y);
  136. 136
  137. 137 var index = (int)(y / c);
  138. 138
  139. 139 var h = hues[index];
  140. 140
  141. 141 var newColor = colors[index];
  142. 142
  143. 143 float less = (y - index * c) / c;
  144. 144
  145. 145 newColor[h] = index % 2 == 0 ? less : 1 - less;
  146. 146
  147. 147 return newColor;
  148. 148 }
  149. 149
  150. 150 }
Main

 

点击操作:

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.EventSystems;
  4. using System;
  5. using UnityEngine.UI;
  6. public class ColorPickClick : MonoBehaviour, IPointerDownHandler, IDragHandler
  7. {
  8. public Button.ButtonClickedEvent Click;
  9. public Vector3 ClickPoint { get; set; }
  10. public void OnDrag(PointerEventData eventData)
  11. {
  12. var rect = transform as RectTransform;
  13. ClickPoint = rect.InverseTransformPoint(eventData.position);
  14. Click.Invoke();
  15. }
  16. public void OnPointerDown(PointerEventData eventData)
  17. {
  18. var rect = transform as RectTransform;
  19. ClickPoint = rect.InverseTransformPoint(eventData.position);
  20. Click.Invoke();
  21. }
  22. }
click

 

 

 

 

UI操作:

 

 

 

 

 

 

 

  最终效果:

 

 

附上demo地址:https://pan.baidu.com/s/1xpsg4cvjR_m7kVt59opsDw

提取码:vp43

 

原文链接:http://www.cnblogs.com/lzzhentou/p/11593703.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号