当前位置:网站首页>Unity color palette | color palette | stepless color change function
Unity color palette | color palette | stepless color change function
2022-07-06 23:42:00 【Fuuyg】
effect
Unity adopt UGUI Realize the palette function of stepless color change , It's very simple and easy to use .
project git Address
https://github.com/zrzhang76/UnityColorBoard
Code ( This is written entirely because CSDN Don't think I have a few words , It is suggested to look directly at the project )
Color board part
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace CSharp.UI.ColorBoard
{
public class ColorBoard : MonoBehaviour, IPointerClickHandler, IDragHandler
{
// Display color Texture
Texture2D tex2d;
//RawImage Components
RawImage ri;
// Pixel width height 256( The default value is )
int TexPixelLength = 256;
int TexPixelHeight = 256;
// Common components
public Slider sliderCRGB;
public ColorHue colorHue;
// Color array
UnityEngine.Color[,] arrayColor;
// Self Transform
RectTransform rt;
// Circle of color focus
public RectTransform circleRect;
public delegate void ColorChangeDelegate(Color color);
public event ColorChangeDelegate OnColorChanged;
private void Awake()
{
ri = GetComponent<RawImage>();
rt = GetComponent<RectTransform>();
circleRect = transform.Find("img_cursor").GetComponent<RectTransform>();
TexPixelLength = (int)rt.sizeDelta.x;
TexPixelHeight = (int)rt.sizeDelta.y;
// Initialize the color array
arrayColor = new UnityEngine.Color[TexPixelLength, TexPixelHeight];
// Create a fixed length and width Texture
tex2d = new Texture2D(TexPixelLength, TexPixelHeight, TextureFormat.RGB24, true);
// Component assignment picture
ri.texture = tex2d;
ri.texture.wrapMode = TextureWrapMode.Clamp;
// Initialize and set the color of the board to red
SetColorPanel(UnityEngine.Color.red);
sliderCRGB.onValueChanged.AddListener(OnCRGBValueChanged);
}
// Monitoring of color changes
void OnCRGBValueChanged(float value)
{
UnityEngine.Color endColor=colorHue.GetColorBySliderValue(value);
SetColorPanel(endColor);
var color = GetColorByPosition(circleRect.anchoredPosition);
OnColorChanged?.Invoke(color);
}
// Set the color of the board
public void SetColorPanel(UnityEngine.Color endColor)
{
UnityEngine.Color[] CalcArray = CalcArrayColor(endColor);
// Fill the color board with color , And apply
tex2d.SetPixels(CalcArray);
tex2d.Apply();
}
// Through a final color value , Calculate the color of all pixels on the board , And return an array
UnityEngine.Color[] CalcArrayColor(UnityEngine.Color endColor)
{
// Calculate the average value of the difference between the final value and white in the horizontal direction , Used to calculate the color value of each horizontal pixel
UnityEngine.Color value = (endColor - UnityEngine.Color.white) / (TexPixelLength - 1);
for (int i = 0; i < TexPixelLength; i++)
{
arrayColor[i, TexPixelHeight - 1] = UnityEngine.Color.white + value * i;
}
// Empathy , vertical direction
for (int i = 0; i < TexPixelLength; i++)
{
value = (arrayColor[i, TexPixelHeight - 1] - UnityEngine.Color.black) / (TexPixelHeight - 1);
for (int j = 0; j < TexPixelHeight; j++)
{
arrayColor[i, j] = UnityEngine.Color.black + value * j;
}
}
// Returns an array , Save all color values
List<UnityEngine.Color> listColor = new List<UnityEngine.Color>();
for (int i = 0; i < TexPixelHeight; i++)
{
for (int j = 0; j < TexPixelLength; j++)
{
listColor.Add(arrayColor[j, i]);
}
}
return listColor.ToArray();
}
/// <summary>
/// Get the color by coordinate , Coordinates are Texture The two-dimensional coordinates above
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public UnityEngine.Color GetColorByPosition(Vector2 pos)
{
Texture2D tempTex2d = (Texture2D)ri.texture;
UnityEngine.Color getColor = tempTex2d.GetPixel((int)pos.x, (int)pos.y);
return getColor;
}
public Vector2 GetClampPosition(Vector2 touchPos)
{
Vector2 vector2 = new Vector2(touchPos.x, touchPos.y);
vector2.x = Mathf.Clamp(vector2.x, 0.001f, rt.sizeDelta.x);
vector2.y = Mathf.Clamp(vector2.y, 0.001f, rt.sizeDelta.y);
return vector2;
}
// Click event
public void OnPointerClick(PointerEventData eventData)
{
Vector3 wordPos;
// take UGUI The coordinates of are transformed into world coordinates
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, eventData.pressEventCamera, out wordPos))
circleRect.position = wordPos;
circleRect.anchoredPosition = GetClampPosition(circleRect.anchoredPosition);
var color = GetColorByPosition(circleRect.anchoredPosition);
OnColorChanged?.Invoke(color);
}
// Drag events
public void OnDrag(PointerEventData eventData)
{
Vector3 wordPos;
// take UGUI The coordinates of are transformed into world coordinates
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, eventData.pressEventCamera, out wordPos))
circleRect.position = wordPos;
circleRect.anchoredPosition = GetClampPosition(circleRect.anchoredPosition);
var color = GetColorByPosition(circleRect.anchoredPosition);
OnColorChanged?.Invoke(color);
}
}
}
Hue bar section
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ColorHue : MonoBehaviour
{
// Paint color Texture
Texture2D tex2d;
// Picture display components
RawImage ri;
// Length and width
int TexPixelWdith = 952;
int TexPixelHeight = 16;
// An array of colors
UnityEngine.Color[,] arrayColor;
private void Awake()
{
ri = gameObject.GetComponent<RawImage>();
// Initialize colors and Texture
arrayColor = new UnityEngine.Color[TexPixelWdith, TexPixelHeight];
tex2d = new Texture2D(TexPixelWdith, TexPixelHeight, TextureFormat.RGB24,true);
// Calculate color
UnityEngine.Color[] calcArray = CalcArrayColor();
// display
tex2d.SetPixels(calcArray);
tex2d.Apply();
ri.texture = tex2d;
ri.texture.wrapMode = TextureWrapMode.Clamp;
}
// Calculate the color array to be displayed on the hue bar
UnityEngine.Color[] CalcArrayColor()
{
// Calculate the equal increment of horizontal pixels
int addValue = (TexPixelWdith - 1) / 3;
//
for (int i = 0; i < TexPixelHeight; i++)
{
arrayColor[0, i] = UnityEngine.Color.red;
arrayColor[addValue, i] = UnityEngine.Color.green;
arrayColor[addValue+addValue, i] = UnityEngine.Color.blue;
arrayColor[TexPixelHeight - 1, i] = UnityEngine.Color.red;
}
UnityEngine.Color value = (UnityEngine.Color.green - UnityEngine.Color.red)/addValue;
for (int i = 0; i < TexPixelHeight; i++)
{
for (int j = 0; j < addValue; j++)
{
arrayColor[j, i] = UnityEngine.Color.red + value * j;
}
}
value = (UnityEngine.Color.blue - UnityEngine.Color.green)/ addValue;
for (int i = 0; i < TexPixelHeight; i++)
{
for (int j = addValue; j < addValue*2; j++)
{
arrayColor[j, i] = UnityEngine.Color.green + value * (j-addValue);
}
}
value = (UnityEngine.Color.red - UnityEngine.Color.blue) / ((TexPixelWdith - 1)-addValue-addValue);
for (int i = 0; i < TexPixelHeight; i++)
{
for (int j = addValue*2; j < TexPixelWdith - 1; j++)
{
arrayColor[j, i] = UnityEngine.Color.blue + value * (j- addValue * 2);
}
}
List<UnityEngine.Color> listColor = new List<UnityEngine.Color>();
for (int i = 0; i < TexPixelHeight; i++)
{
for (int j = 0; j < TexPixelWdith; j++)
{
listColor.Add(arrayColor[j, i]);
}
}
return listColor.ToArray();
}
/// <summary>
/// Get the color According to height
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public Color GetColorBySliderValue(float value)
{
float clampValue = Mathf.Clamp(value, 0.001f, 0.999f);
Color getColor=tex2d.GetPixel((int)((TexPixelWdith-1)*clampValue),0);
return getColor;
}
}
It's not easy to make , Give me a compliment ~~
边栏推荐
- koa2对Json数组增删改查
- [212] what are three methods for PHP to send post requests
- Up to 5million per person per year! Choose people instead of projects, focus on basic scientific research, and scientists dominate the "new cornerstone" funded by Tencent to start the application
- matplotlib画柱状图并添加数值到图中
- Ajout, suppression et modification d'un tableau json par JS
- Can async i/o be implemented by UDF operator and then called by SQL API? At present, it seems that only datastre can be seen
- ArrayExpress数据库里的细胞只有两个txt是不是只能根据Line到ENA下载测序跑矩阵?
- Gradle知识概括
- A novice asks a question. I am now deployed on a single machine. I submitted an SQL job and it runs normally. If I restart the service job, it will disappear and I will have to
- GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议
猜你喜欢

Station B Big utilise mon monde pour faire un réseau neuronal convolutif, Le Cun Forward! Le foie a explosé pendant 6 mois, et un million de fois.

每年 2000 亿投资进入芯片领域,「中国芯」创投正蓬勃

MySQL connected vscode successfully, but this error is reported

The programmer said, "I'm 36 years old, and I don't want to be rolled, let alone cut."

AI金榜题名时,MLPerf榜单的份量究竟有多重?

服务器SMP、NUMA、MPP体系学习笔记。

氢创未来 产业加速 | 2022氢能专精特新创业大赛报名通道开启!

资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
docker启动mysql及-eMYSQL_ROOT_PASSWORD=my-secret-pw问题解决

Server SMP, NUMA, MPP system learning notes.
随机推荐
实现多彩线条摆出心形
英国都在试行4天工作制了,为什么BAT还对996上瘾?
Experiment 6: installing eve-ng
The tutorial of computer reinstallation win10 system is simple and easy to understand. It can be reinstalled directly without U disk
B站大佬用我的世界搞出卷积神经网络,LeCun转发!爆肝6个月,播放破百万
每年 2000 亿投资进入芯片领域,「中国芯」创投正蓬勃
What should I do if the USB flash disk data is formatted and how can I recover the formatted USB flash disk data?
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
Please help xampp to do sqlilab is a black
matplotlib画柱状图并添加数值到图中
《数字经济全景白皮书》保险数字化篇 重磅发布
问下各位,有没有flink sql生成作业的文档啊或是案列啊知道flink cli可以建表和指定目
同一个作业有两个source,同一链接不同数据库账号,为何第二个链接查出来的数据库列表是第一个账号的
传统企业要为 Web3 和去中心化做的 11 个准备
The intranet penetrates the zerotier extranet (mobile phone, computer, etc.) to access intranet devices (raspberry pie, NAS, computer, etc.)
ArrayExpress数据库里的细胞只有两个txt是不是只能根据Line到ENA下载测序跑矩阵?
Microsoft win11 is still "unsatisfactory". Multi user feedback will cause frequent MSI crashes
机器人材料整理中的套-假-大-空话
Daily question brushing record (XV)
氢创未来 产业加速 | 2022氢能专精特新创业大赛报名通道开启!