用unity 实现调色板功能。

直接上代码:

 using UnityEngine;
using System.Collections;
using UnityEngine.UI; public class ColorPick : MonoBehaviour
{ public Image Saturation;
public Image Hue;
public Image Paint; public RectTransform Point_Stauration;
public RectTransform Point_Hue; private Sprite Saturation_Sprite;
private Sprite Hue_Sprite; private Color32 currentHue = Color.red; private void Awake()
{ } private void Start()
{
UpdateStauration();
UpdateHue();
} float sWidth = , sHeight = ;
//更新饱和度
private void UpdateStauration()
{ Saturation_Sprite = Sprite.Create(new Texture2D((int)sWidth, (int)sHeight), new Rect(, , sWidth, sHeight), new Vector2(, )); for (int y = ; y <= sHeight; y++)
{
for (int x = ; x < sWidth; x++)
{
var pixColor = GetSaturation(currentHue, x / sWidth, y / sHeight);
Saturation_Sprite.texture.SetPixel(x, ((int)sHeight - y), pixColor);
}
}
Saturation_Sprite.texture.Apply(); Saturation.sprite = Saturation_Sprite;
} //更新色泽度
private void UpdateHue()
{ float w = , h = ; Hue_Sprite = Sprite.Create(new Texture2D((int)w, (int)h), new Rect(, , w, h), new Vector2(, )); for (int y = ; y <= h; y++)
{
for (int x = ; x < w; x++)
{
var pixColor = GetHue(y / h);
Hue_Sprite.texture.SetPixel(x, ((int)h - y), pixColor);
}
}
Hue_Sprite.texture.Apply(); Hue.sprite = Hue_Sprite;
} private Vector2 clickPoint = Vector2.zero;
public void OnStaurationClick(ColorPickClick sender)
{
var size2 = Saturation.rectTransform.sizeDelta / ;
var pos = Vector2.zero;
pos.x = Mathf.Clamp(sender.ClickPoint.x, -size2.x, size2.x);
pos.y = Mathf.Clamp(sender.ClickPoint.y, -size2.y, size2.y);
Point_Stauration.anchoredPosition = clickPoint = pos; UpdateColor();
} public void UpdateColor()
{
var size2 = Saturation.rectTransform.sizeDelta / ;
var pos = clickPoint;
pos += size2; var color = GetSaturation(currentHue, pos.x / Saturation.rectTransform.sizeDelta.x, - pos.y / Saturation.rectTransform.sizeDelta.y);
Paint.color = color;
} public void OnHueClick(ColorPickClick sender)
{
var h = Hue.rectTransform.sizeDelta.y / 2.0f;
var y = Mathf.Clamp(sender.ClickPoint.y, -h, h);
Point_Hue.anchoredPosition = new Vector2(, y); y += h;
currentHue = GetHue( - y / Hue.rectTransform.sizeDelta.y);
UpdateStauration();
UpdateColor();
} private static Color GetSaturation(Color color, float x, float y)
{
Color newColor = Color.white;
for (int i = ; i < ; i++)
{
if (color[i] != )
{
newColor[i] = ( - color[i]) * ( - x) + color[i];
}
} newColor *= ( - y);
newColor.a = ;
return newColor;
} //B,r,G,b,R,g //大写是升,小写是降
private readonly static int[] hues = new int[] { , , , , , }; private readonly static Color[] colors = new Color[] { Color.red, Color.blue, Color.blue, Color.green, Color.green, Color.red }; private readonly static float c = 1.0f / hues.Length; private static Color GetHue(float y)
{
y = Mathf.Clamp01(y); var index = (int)(y / c); var h = hues[index]; var newColor = colors[index]; float less = (y - index * c) / c; newColor[h] = index % == ? less : - less; return newColor;
} }

Main

点击操作:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System;
using UnityEngine.UI;
public class ColorPickClick : MonoBehaviour, IPointerDownHandler, IDragHandler
{
public Button.ButtonClickedEvent Click; public Vector3 ClickPoint { get; set; } public void OnDrag(PointerEventData eventData)
{
var rect = transform as RectTransform;
ClickPoint = rect.InverseTransformPoint(eventData.position);
Click.Invoke();
} public void OnPointerDown(PointerEventData eventData)
{
var rect = transform as RectTransform;
ClickPoint = rect.InverseTransformPoint(eventData.position);
Click.Invoke();
}
}

click

UI操作:

最终效果:

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

提取码:vp43

用 Unity 实现调色板功能的更多相关文章

  1. Ioc 之 Unity的AOP功能

    前面我们介绍了Unity的依赖注入功能,现在来介绍下Unity的AOP功能.AOP是面向切面编程,它能够使我们在不改变现有代码结构的情况下额外的为其添加一些功能. 我们还是使用配置文件来对类型进行注入 ...

  2. Unity多玩家网络游戏开发教程1章Unity带有网络功能

    Unity网络多玩家游戏开发教程第1章Unity自带网络功能 Unity拥有大量的第三方插件.专门提供了对网络功能的支持. 可是.大部分开发人员第一次接触到的还是Unity自带的网络功能.也就是大家常 ...

  3. Unity寻路的功能总结

    源地址:http://blog.csdn.net/sgnyyy/article/details/21878163 1. 利用Unity本身自带的NavMesh 这篇文章已经比较详细,可能对于很多需要a ...

  4. Unity带有网络功能——创建服务,并连接到一个特定的服务

    游戏本身需要在网络上创建服务,然后其他的游戏能够连接到这个服务,此外真实场景现在玩同样的游戏效果一起. 该方法是创建一个服务呼叫Network.InitializeServer( ): 是Networ ...

  5. 使用Unity的2D功能开发弹球游戏

    https://mp.weixin.qq.com/s/7xjysNDVHe7avF1v2NZWcg

  6. Unity Button延迟功能

    有时候Button点下去不是要求立即反应的,而是先有个特别短的动画,再反应. 实现: 继承Button,然后重写一下OnPointerClick,利用协程来延迟. using System.Colle ...

  7. c#/asp.net实现炫酷仿调色板/颜色选择器功能

    asp.net 之颜色选择器,仿调色板功能 1. 插件非常容易使用,只需引用相应的js文件和css样式文件即可,见代码示例,插件精小,炫酷 2. 只需要初始化即可使用,并且选择的颜色会在文本框中以16 ...

  8. Unity内存理解(转)

    Unity3D 里有两种动态加载机制:一个是Resources.Load,另外一个通过AssetBundle,其实两者区别不大. Resources.Load就是从一个缺省打进程序包里的AssetBu ...

  9. unity自带寻路Navmesh入门教程(一)

    说明:从今天开始,我阿赵打算写一些简单的教程,方便自己日后回顾,或者方便刚入门的朋友学习.水平有限请勿见怪.不过请尊重码字截图录屏的劳动,如需转载请先告诉我.谢谢! unity自从3.5版本之后,增加 ...

随机推荐

  1. TDOA 之TDOA算法python实现

    这里指的TDOA算法,实际是解两个双曲线方程,由于两个二次方程设计东西较多,如果强解,计算量很大,从网上参考了如下链接: 算法推到:https://blog.csdn.net/lpsl1882/art ...

  2. learning scala dependency injection

    println("Step 1: Create a trait which knows how to do create, read, update and delete operation ...

  3. 块状链表 codevs 2333弹飞绵羊

    块状链表,分块处理,先预处理每一个点跳到下一个块 跳到哪,步数.然后修改的时候,修该那一个块即可 #include<cstdio>#include<cmath>int a[20 ...

  4. 【LGR-059】洛谷7月月赛题解

    传送门 比赛的时候正在大巴上,笔记本没网又没电(不过就算有我估计也不会打就是了) \(A\) 咕咕 const int N=(1<<10)+5; int a[N][N],n; void s ...

  5. 2019暑期金华集训 Day5 生成函数

    自闭集训 Day5 生成函数 一般生成函数 无脑地把序列变成多项式: \[ \{a_i\}\rightarrow A(x)=\sum_{n} a_nx^n \] 形式幂级数 生成函数是一种形式幂级数. ...

  6. http状态码-备查

    http状态码分类 分类 分类描述 1** 信息,服务器收到请求,需要请求者继续执行操作 2** 成功,操作被成功接收并处理 3** 重定向,需要进一步的操作以完成请求 4** 客户端错误,请求包含语 ...

  7. Selenium操作Chrome模拟手机浏览器

    目录 使用指定设备 使用自定义设备 在使用Chrome浏览网页时,我们可以使用Chrome开发者工具模拟手机浏览器,在使用Selenium操作Chrome时同样也可以模拟手机浏览器.主要有以下两种用途 ...

  8. geometry_msgs.msg.PoseStamped 代码示例

    https://programtalk.com/python-examples/geometry_msgs.msg.PoseStamped/

  9. 解决Ubuntu重启后,core_pattern失效问题——手动关闭apport

    云主机重启后,core_pattern,即/proc/sys/kernel/core_pattern和/etc/sysctl*配置失效,被系统自动修改. 配置后,重启后core_pattern被重写 ...

  10. redis之为什么redis是单线程?

    官方FAQ表示,因为Redis是基于内存的操作,CPU不是Redis的瓶颈,Redis的瓶颈最有可能是机器内存的大小或者网络带宽.既然单线程容易实现,而且CPU不会成为瓶颈,那就顺理成章地采用单线程的 ...