用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. javascript动态合并表格相同的单元格

    <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...

  2. 1101: [POI2007]Zap(莫比乌斯反演)

    1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MB Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定 ...

  3. java 封装返回结果实体类 返回结果以及错误信息

    public class ResponseMessage { private final static String STATUS_OK = "0"; private final ...

  4. 启动Django项目报错

    今天一时手欠将电脑名字改成了中文,在启动Django或Flask项目的时候一直报下面的错误 问题描述: 环境配置成功,Django成功pip,运行项目报错,浏览器输入127.0.0.1:8000报错 ...

  5. 模板 - Codeforces模板

    #include<bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 2e5; const ...

  6. jQuery Cookie (内附 上百行的中文使用手册,与 所有的注释中文翻译)

    jQuery Cookie (内附 上百行的中文使用手册,与 所有的注释中文翻译) 博主亲自翻译. 大家多多捧场. 更多资源请点击"查看TA 的资源" .全场通通 2积分. htt ...

  7. kvm 学习(三)存储池

    创建kvm存储池 1.查看系统已经存储的存储池 [root@runstone ~ ::]#virsh pool-list Name State Autostart ------------------ ...

  8. Android Handler消息处理顺序分析

    看到Handler中的消息处理函数: public void dispatchMessage(Message msg){...} 这个函数是在Looper的执行消息循环loop()的时候取出Messa ...

  9. mysql中查询某个字段重复的数据

    SELECT corp_name,count(*) as sums FROM corp_tax WHERE corp_year = 2018 AND corp_month = 8 group by c ...

  10. 全局设置页面颜色 返回按钮样式 iOS

    思路 1.建个UIViewController的分类 2.hook方法viewDidLoad(Aspects是三方库 可以不用) 3.看下面蓝色部分代码 #import "UIViewCon ...