用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. GBDT算法梳理

    1.GBDT(Gradient Boosting Decision Tree)思想 Boosting : 给定初始训练数据,由此训练出第一个基学习器: 根据基学习器的表现对样本进行调整,在之前学习器做 ...

  2. OTFS Research Notes

    △f = f·v·cosθ/c,f表示载波频率,5G/B5G朝着毫米波等高频段方向发展,因此多普勒拓展的影响将更显著.此外,Masssive MIMO的现有规模已达256维度,并将朝着上千的规模发展. ...

  3. ubuntu 停留开机界面解决方法

    1 问题 Ubuntu 启动时卡在开机界面上 2 修复 $ sudo apt-get -y reinstall ubuntu-desktop*

  4. 基础 | BIO、NIO与AIO

    本文链接:https://blog.csdn.net/bingbeichen/article/details/83617163 Java中的IO部分比较复杂,具体可参看书籍<Java NIO&g ...

  5. jQuery的ajax()方法提交数组问题

    http://blog.csdn.net/thc1987/article/details/7278269 解决办法是添加一个属性 traditional:true $.ajax({    type: ...

  6. 代码 | 自适应大邻域搜索系列之(7) - 局部搜索LocalSearch的代码解析

    前言 好了小伙伴们我们又见面了,咳咳没错还是我.不知道你萌接连被这么多篇代码文章刷屏是什么感受,不过,酸爽归酸爽.今天咱们依然讲代码哈~不过今天讲的依然很简单,关于局部搜索LocalSearch的代码 ...

  7. Luogu5071 [Ynoi2015]此时此刻的光辉 【莫队】

    题目链接:洛谷 这个跟上上个Ynoi题目是一样的套路,首先我们知道\(n=\prod p_i^{\alpha_i}\)时\(d(n)=\prod (\alpha_i+1)\). 首先对所有数分解质因数 ...

  8. Codeforces 704E Iron Man [树链剖分,计算几何]

    Codeforces 这题--真是搞死我了-- 好不容易下定了决心要不颓废,要写题,结果一调就调了十几个小时-- 思路 我们发现在树上做非常不舒服,于是树链剖分之后一次在重链上的移动就可以看做是在df ...

  9. Word 有哪些神奇的功能?

    作者:秦阳链接:https://www.zhihu.com/question/27035859/answer/621742048来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  10. 2019CSP-S初赛知识点汇总

    0x00 基本算法 0x01 位运算 0x02 前缀和与差分 0x03 二分 0x04 倍增 0x05 排序 0x06 离散化 0x07 高精度 0x10 数据结构 0x11 栈和队列 0x12 链表 ...