我们使用Unity或者其他软件都有一个颜色选择功能,但是这个组件只能在编辑模式中用。因此我就自己开发了一个颜色选择器的插件。

在网上也看到其他人的实现方法,都大同小异,通过新建一张texture,再用代码在图片上渲染颜色。正好我刚刚学习了Shader Forge,所以就想着用shader来渲染颜色,也可以复习新学到的知识。
开发环境:Unity2017.1.0f3 ShaderForge1.38


实现初效果

界面使用UGUI开发,选择器分为三个模块,Saturation,Hue,Color。

Hue是 七色渐变图,实现多种颜色的选择。
Saturation是Hue选择的颜色的饱和度。
Color是最后选择的颜色。

下面是每个功能的逐个实现。

首先用UGUI创建界面,可以全部使用Image

Saturation大小是80,轴点放到左上角。创建子节点SatDot,大小为5。
Hue高80,宽10,轴点放到最上方居中。创建子节点HueDot,高为3,宽为10。

ShaderForge创建2D shader,命名为HueShader,节点图如下

HueShader自带有Hue节点,直接使用即可。因为Hue是从上到下,所以UV只需要V点的区域。

ShaderForge创建2D shader,命名为SaturationShader,节点图如下

因为需要在Hue上选择的颜色,只要将HueDot的y坐标相对Hue的高度的比例传给Value。

ShaderForge创建2D shader,命名为ColorShader,节点图如下

Value是HueDot位置相对Hue的高度的比例(同上),u是SatDot的x坐标相对于Saturation的宽度的比例,v是SatDot的y坐标相对于Saturation的高度的比例。

shader创建完成,在创建3个材质球,将3个shader分别拖到材质球,把材质球对应给不同的Image。
下面就开始一些简单的C#代码实现颜色的选择。

首先SatDot要在Saturation范围内通过鼠标拖动可以上下左右移动,HueDot要在Hue范围内只能上下移动,不能左右移动。
创建C#脚本,将脚本挂到SatDot和HueDot。如下代码即可使鼠标拖动SatDot和HueDot。

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;public class DragDot : MonoBehaviour,IDragHandler,IBeginDragHandler,IEndDragHandler
{public void OnDrag(PointerEventData eventData){if(this.transform.parent.name=="Saturation"){Saturation (eventData.position);}else{Hue(eventData.position);}}public  void Saturation(Vector3 mousePos){this.transform.position = mousePos;if(this.transform.localPosition.x<0f){this.transform.localPosition = new Vector2 (0f, this.transform.localPosition.y);}if(this.transform.localPosition.x>80f){this.transform.localPosition = new Vector2 (80f, this.transform.localPosition.y);}if(this.transform.localPosition.y>0){this.transform.localPosition = new Vector2 (this.transform.localPosition.x,0);}if(this.transform.localPosition.y<-80f){this.transform.localPosition = new Vector2 (this.transform.localPosition.x,-80f);}}public void Hue(Vector3 mousePos){this.transform.position = new Vector2 (this.transform.position.x, mousePos.y);if(this.transform.localPosition.y>0){this.transform.localPosition = new Vector2 (this.transform.localPosition.x,0);}if(this.transform.localPosition.y<-80f){this.transform.localPosition = new Vector2 (this.transform.localPosition.x,-80f);}}}

然后需要实现点击Saturation和Hue两个Image,两个Dot分别到点击的位置。
创建C#脚本,将脚本挂到Saturation和Hue。如下代码即可使SatDot和HueDot到鼠标点击位置。

using UnityEngine;
using UnityEngine.EventSystems;
public class ClickImage : MonoBehaviour ,IPointerClickHandler
{public DragPoint dragPoint;void IPointerClickHandler.OnPointerClick (PointerEventData eventData){if(this.name=="Saturation"){dragPoint.Saturation (eventData.position);}else{dragPoint.Hue(eventData.position);}}

再新建一个脚本,可以使shader之间相互传值。

using UnityEngine;public class ColorPicker : MonoBehaviour
{public Material satMat;public Material colorMat;public void GetHueColor(float hueValue){satMat.SetFloat ("_Value",hueValue);}public void GetSaturationColor(float x,float y,float hueValue){colorMat.SetFloat ("_Value",hueValue);colorMat.SetFloat ("_U",x);colorMat.SetFloat ("_V",y);}
}

最后,在DragDot 脚本中计算出拖动Dot时,Dot相对于父节点的比例,在调用GetHueColor方法和GetSaturationColor方法。

附上 完整的DragDot 的代码

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;public class DragPoint : MonoBehaviour,IDragHandler,IBeginDragHandler,IEndDragHandler
{public void OnDrag(PointerEventData eventData){if(this.transform.parent.name=="Saturation"){Saturation (eventData.position);}else{Hue(eventData.position);}}public  void Saturation(Vector3 mousePos){if (this.name == "HueDot")return;this.transform.position = mousePos;if(this.transform.localPosition.x<0f){this.transform.localPosition = new Vector2 (0f, this.transform.localPosition.y);}if(this.transform.localPosition.x>80f){this.transform.localPosition = new Vector2 (80f, this.transform.localPosition.y);}if(this.transform.localPosition.y>0){this.transform.localPosition = new Vector2 (this.transform.localPosition.x,0);}if(this.transform.localPosition.y<-80f){this.transform.localPosition = new Vector2 (this.transform.localPosition.x,-80f);}float x = Mathf.Abs (this.transform.localPosition.x) / 80f;float y = Mathf.Abs (this.transform.localPosition.y) / 80f;//ColorPicker中创建单例//hueValue是在ColorPicker中初始换ColorPicker._instance.GetSaturationColor (x, y,ColorPicker._instance.hueValue);}public void Hue(Vector3 mousePos){this.transform.position = new Vector2 (this.transform.position.x, mousePos.y);if(this.transform.localPosition.y>0){this.transform.localPosition = new Vector2 (this.transform.localPosition.x,0);}if(this.transform.localPosition.y<-80f){this.transform.localPosition = new Vector2 (this.transform.localPosition.x,-80f);}float rate = (Mathf.Abs(this.transform.localPosition.y)  - 2) / 80f;ColorPicker._instance.hueValue = rate;ColorPicker._instance.GetHueColor (rate);GameObject dot = GameObject.Find ("SatDot");dot.GetComponent<DragPoint> ().Saturation (dot.transform.position);}}

PC、安卓完美实现。还有一些其他功能,后续开发中…

Unity3D 颜色选择器相关推荐

  1. vue颜色选择器_如何制作? Vue的颜色选择器!

    vue颜色选择器 by ZAYDEK 由ZAYDEK 如何制作? Vue的颜色选择器! (How to make a ? color picker with Vue!) 注意:颜色看起来可能比实际颜色 ...

  2. js颜色选择器 制作分析

    给html元素设置事件监听, 触发事件 弹出颜色选择器 颜色选择器绘制 获取上次选择的颜色(当前颜色) 绘制渐变色板(canvas) (方法: 横轴渐变ff0000, ffff00, 00ff00, ...

  3. Ubuntu中的颜色选择器实用程序(彩色移液器)[关闭]

    本文翻译自:Color picker utility (color pipette) in Ubuntu [closed] I'am looking for a color picker utilit ...

  4. java 选择 颜色的控件_JavaFX颜色选择器(ColorPicker)

    颜色选择器控件允许用户从可用的颜色范围中选择颜色,或通过指定RGB或HSB组合设置其他颜色.JavaFX ColorPicker控件具有颜色选择器,调色板和自定义颜色对话框窗口. 创建ColorPic ...

  5. 019_ColorPicker颜色选择器

    1. ColorPicker颜色选择器 1.1. 用于颜色选择, 支持多种格式. 1.2. 颜色选择器属性 参数 说明 类型 可选值 默认值 value / v-model 绑定值 string 无 ...

  6. 颜色代码对照表、网页颜色选择器

    颜色代码对照表.网页颜色选择器 2006-12-04 20:47:28|  分类: 博客教程 |  标签:博客  教程  |举报|字号 订阅 颜色代码大全 网页颜色选择器 ffff00 ffff33 ...

  7. 【PC工具】大神开源项目-配色调色工具Colorpicker颜色选择器

    微信关注 "DLGG创客DIY" 设为"星标",重磅干货,第一时间送达. 今天看到一个不错的开源的大神的作品,Colorpicker颜色选择器,和大家分享一下. ...

  8. android类中定义颜色,自定义实现简单的Android颜色选择器(附带源码)

    在写Android App过程中需要一个简单的颜色选择器,Android自带的ColorPicker和网上的一些ColorPicker都太高端了,都实现了颜色渐变功能,我要的不需要那么复杂,只想提供几 ...

  9. php和android选择器,Android_android 字体颜色选择器(ColorPicker)介绍,primary_text_yellow.xml 复制代码 代 - phpStudy...

    android 字体颜色选择器(ColorPicker)介绍 primary_text_yellow.xml themes.xml @color/primary_text_yellow相关阅读: 在L ...

  10. 基于vue的颜色选择器vue-color-picker

    项目中有用到颜色选择器的童鞋们可以看过来了 关于color-picker的jquery的插件是有蛮多,不过vue组件没有吧,反正我没有找到, 虽然element-ui里面有这个,但是你愿意为了一个小功 ...

最新文章

  1. c#技巧教程(连载)
  2. java SocketChannel and ServerSocketChannel
  3. flask貌似html文件里只能用flask指定的路径格式,css文件里则可用相对路径
  4. Java类的继承总结
  5. 树莓派 + Windows IoT Core 搭建环境监控系统
  6. C++描述杭电OJ 2008.数值统计 ||
  7. 没有Wi-Fi?三种方法通过iPhone让Macbook连上网络
  8. java 子类的同名方法_java 父类子类有同名方法时如何调用的实现
  9. 委托与事件-闲聊系列(二)
  10. 表单内如何直接贴图而不用上传图片_重磅更新|偷偷告诉你,表单大师官网改版啦啦啦啦...
  11. sql server锁异常_SQL Server中异常处理的背景
  12. 设置透明色有残留怎么办_冬天车玻璃结冰,车被冻住了怎么办?据说只有10%的人做对了...
  13. 干货分享:Neutron的PPT,帮助你理解Neutron的各种细节
  14. 微软 .NET Core 3.0 版本发布
  15. android之去掉actionbar
  16. webpack5学习与实战-(八)-配置打包后的文件名
  17. Viso各版本网盘免费下载
  18. OpenCV3历程(4)——寻找直线的十字交叉点
  19. 创业不是 闹着玩的,水很深,
  20. 18-09-20 关于Xlrd和Xlwt的初步学习

热门文章

  1. linux系统nohob安装,Linux启动详解1
  2. STM8S 红外解码+低功耗处理
  3. 网页上透明FLASH代码详解
  4. 剪切板是计算机系统,剪贴板
  5. https工作原理及CA证书及验证证书
  6. SIM900A—发送、接收中英文短信
  7. GSM sim900a mini模块详解
  8. scrapy框架简介和基础应用
  9. 泰坦尼克号数据下载链接
  10. 待业在家的6个月,我靠淘宝月入百万:你看不起的行业,往往很赚钱