用 Unity 实现调色板功能
用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 实现调色板功能的更多相关文章
- Ioc 之 Unity的AOP功能
前面我们介绍了Unity的依赖注入功能,现在来介绍下Unity的AOP功能.AOP是面向切面编程,它能够使我们在不改变现有代码结构的情况下额外的为其添加一些功能. 我们还是使用配置文件来对类型进行注入 ...
- Unity多玩家网络游戏开发教程1章Unity带有网络功能
Unity网络多玩家游戏开发教程第1章Unity自带网络功能 Unity拥有大量的第三方插件.专门提供了对网络功能的支持. 可是.大部分开发人员第一次接触到的还是Unity自带的网络功能.也就是大家常 ...
- Unity寻路的功能总结
源地址:http://blog.csdn.net/sgnyyy/article/details/21878163 1. 利用Unity本身自带的NavMesh 这篇文章已经比较详细,可能对于很多需要a ...
- Unity带有网络功能——创建服务,并连接到一个特定的服务
游戏本身需要在网络上创建服务,然后其他的游戏能够连接到这个服务,此外真实场景现在玩同样的游戏效果一起. 该方法是创建一个服务呼叫Network.InitializeServer( ): 是Networ ...
- 使用Unity的2D功能开发弹球游戏
https://mp.weixin.qq.com/s/7xjysNDVHe7avF1v2NZWcg
- Unity Button延迟功能
有时候Button点下去不是要求立即反应的,而是先有个特别短的动画,再反应. 实现: 继承Button,然后重写一下OnPointerClick,利用协程来延迟. using System.Colle ...
- c#/asp.net实现炫酷仿调色板/颜色选择器功能
asp.net 之颜色选择器,仿调色板功能 1. 插件非常容易使用,只需引用相应的js文件和css样式文件即可,见代码示例,插件精小,炫酷 2. 只需要初始化即可使用,并且选择的颜色会在文本框中以16 ...
- Unity内存理解(转)
Unity3D 里有两种动态加载机制:一个是Resources.Load,另外一个通过AssetBundle,其实两者区别不大. Resources.Load就是从一个缺省打进程序包里的AssetBu ...
- unity自带寻路Navmesh入门教程(一)
说明:从今天开始,我阿赵打算写一些简单的教程,方便自己日后回顾,或者方便刚入门的朋友学习.水平有限请勿见怪.不过请尊重码字截图录屏的劳动,如需转载请先告诉我.谢谢! unity自从3.5版本之后,增加 ...
随机推荐
- HDU-1573-X问题(线性同余方程组)
链接: https://vjudge.net/problem/HDU-1573 题意: 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1] ...
- [React + GraphQL] Use useLazyQuery to manually execute a query with Apollo React Hooks
When using useQuery from Apollo React Hooks, the request will be sent automatically after the compon ...
- learning scala Option[T]
Option(选项)类型用来表示一个值是可选的(有值或无值). Option[T] 是一个类型为 T 的可选值的容器: 如果值存在, Option[T] 就是一个 Some[T] ,如果不存在, Op ...
- Linux操作系统常用命令合集——第四篇-文件系统权限操作(5个命令)
1.umask [命令作用] 文件或目录创建时的遮罩码 [命令语法] umask [选项] [参数] [常用选项] -p --输出的权限掩码可直接作为指令来执行 -s --以符号方式 ...
- [TJOI2013]奖学金 乱搞
[TJOI2013]奖学金 乱搞 从\(c\)个二元组\((v,w)\)中选出\(n\)个,使其\(v\)的中位数最大的同时使\(w\)和小于等于\(f\),求这个中位数 有点意思.有点像二分答案的思 ...
- Input类中常用的验证方式
Deolin一般将Input类对象作为POST请求方法的参数, Input类的域与前端的数据结构一一对应,由于后端不应该相信前端传过来的任何数据, 所以前端的数据对象先绑定到Input对象中,通过JS ...
- 为Python配置Vim编辑器(GUI/非GUI皆可)
原文地址:https://blog.csdn.net/alanzjl/article/details/49383943 Vim as a python IDE ** 最近一直在写Python,但一直没 ...
- Python 学习随笔 - 2 - list 、tuple 、dict、set 特殊数据类型 及 实际应用
1.list list是一种有序的集合,可以随时添加和删除其中的元素; 和C语言不同的地方是list里的元素甚至可以是不同类型的,甚至是另个list 例如:['A', 'B', 'C'] ['A ...
- SpringBoot面试题 转(已迁移到java相关知识点)
## 什么是springboot 用来简化spring应用的初始搭建以及开发过程 使用特定的方式来进行配置(properties或yml文件) 创建独立的spring引用程序 main方法运行 嵌入的 ...
- Flask上下文源码分析(一)
flask中的上下文分两种,application context和request context,即应用上下文和请求上下文. 从名字上看,可能会有误解,认为应用上下文是一个应用的全局变量,所有请 ...