用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. BZOJ 3162 / Luogu P4895: 独钓寒江雪 树hash+DP

    题意 给出一棵无根树,求本质不同的独立集数模100000000710000000071000000007的值. n≤500000n\le 500000n≤500000 题解 如果是有根树就好做多了.然 ...

  2. 使用jQuery快速高效制作网页交互特效---jQuery选择器

    一.什么是jQuery选择器 Query选择器继承了CSS与Path语言的部分语法,允许通过标签名.属性名或内容对DOM元素进行快速.准确的选择, 而不必担心浏览器的兼容性,通过jQuery选择器对页 ...

  3. Mysql查看所有表的数据量

    ##查看所有表信息 SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'pcms-zgh20190327' ##查看各个表数据量 ...

  4. Greenplum 查看连接与锁信息数据字典

    查看系统中会话/连接/SQL与锁情况 1.查看当前活动的客户端连接数  SELECT count(*) FROM pg_stat_activity WHERE NOT pid=pg_backend_p ...

  5. 对深层嵌套对象进行取值&赋值

    需求如下: let obj = { foo: { bar: { name: 'biz' } } }; // 输出 'biz' this.getObj(obj, 'foo.bar.name'); obj ...

  6. Codeforces 1182D Complete Mirror [树哈希]

    Codeforces 中考考完之后第一个AC,纪念一下qwq 思路 简单理解一下题之后就可以发现其实就是要求一个点,使得把它提为根之后整棵树显得非常对称. 很容易想到树哈希来判结构是否相同,而且由于只 ...

  7. maven 安装 jar 包

    注意 jar 包路径,和版本号 mvn install:install-file -Dfile=E:\ChromeBox\box\kaptcha-2.3.2\kaptcha-2.3.2.jar -Dg ...

  8. 5.linux 软件安装的三种方法

      一.linux 操作系统中 软件的分类 以及软件的安装     vmtools  调用了perl语言写的安装脚本去进行内核的升级安装  ./ xxxxx        源码包安装软件:GNU  使 ...

  9. bind--dns-docker---[nslookup/dig]

    [dig]  https://www.cnblogs.com/apexchu/p/6790241.html [dns resolution and revserse ]https://www.cnbl ...

  10. Hibernate 关系配置

    表之间关系 1. 一对多 一个部门有多个员工,一个员工只能属于某一个部门 一个班级有多个学生,一个学生只能属于一个班级 2. 多对多 一个老师教多个学生,一个学生可以被多个老师教 一个学生可以先择多门 ...