Unity完美的上色并检测完成度工具
直接将代码放在RawImage上(不能放在Image上面)
附加代码的物体RawImage上面的Texture需要设定为空,然后附加Mask组件,子物体同时需要RawImage组件并设定为需要上色展示的图像,可以忽略透明区域检测上色百分比
需要注意revocations变量不能等于1或者0,否则会进入死循环
用起来如果有卡顿,就改变RawImage的Scale值,Scale越大绘画越流畅
-
using System;
-
using System.Collections.Generic;
-
using UnityEngine;
-
using UnityEngine.EventSystems;
-
using UnityEngine.UI;
-
-
public class Painting : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
-
{
-
public Color color;
-
//radius的值不能等于1和0,不然会进入死循环
-
public int radius = 5;
-
private Canvas canvas;
-
private RawImage mainImage;
-
private RectTransform mainRect;
-
private Texture2D texture;
-
private Texture2D minTexture;
-
private bool handIn;
-
private Vector2 lastPos;
-
private Color backgroundColor = new Color(1,1,1,0);
-
List<Dictionary<Vector2,Color>> revocations = new List<Dictionary<Vector2,Color>>();
-
public Action<float> setColorFill = fill => { };
-
private float setNow = 0, setMax = 0;
-
public bool isClose;
-
public float getFill
-
{
-
get { return setNow / setMax; }
-
}
-
-
void Start()
-
{
-
canvas = transform.root.GetComponent<Canvas>();
-
mainImage = GetComponent<RawImage>();
-
mainRect = mainImage.rectTransform;
-
minTexture = GetMinTexture((Texture2D) transform.GetChild(0).GetComponent<RawImage>().texture);
-
for (int i = 0; i < minTexture.width; i )
-
{
-
for (int j = 0; j < minTexture.height; j )
-
{
-
if (minTexture.GetPixel(i, j).a > 0.1f)
-
{
-
setMax ;
-
}
-
}
-
}
-
texture = new Texture2D((int)mainRect.sizeDelta.x,(int)mainRect.sizeDelta.y);
-
for (int i = 0; i < texture.width; i )
-
{
-
for (int j = 0; j < texture.height; j )
-
{
-
texture.SetPixel(i,j,backgroundColor);
-
}
-
}
-
-
setMax *= 0.99f;
-
texture.Apply();
-
mainImage.texture = texture;
-
mainImage.SetNativeSize();
-
}
-
-
private void Update()
-
{
-
if(!handIn || isClose){return;}
-
-
if (FanGameManager.Main.GetClick())
-
{
-
revocations.Insert(0,new Dictionary<Vector2, Color>());
-
}
-
-
if (FanGameManager.Main.GetClickAlways())
-
{
-
Vector2 pos = GetPosForTexture();
-
if (lastPos.x == 0 && lastPos.y == 0 || Vector2.Distance(pos,lastPos) <= radius/2)
-
{
-
DrawPoint((int)pos.x,(int)pos.y);
-
}
-
else
-
{
-
DrawLine((int)lastPos.x,(int)lastPos.y,(int)pos.x,(int)pos.y);
-
}
-
lastPos = pos;
-
}
-
-
if (FanGameManager.Main.GetClickUp())
-
{
-
lastPos = Vector2.zero;
-
}
-
-
if (Input.GetKeyDown(KeyCode.Z))
-
{
-
Revocation();
-
}
-
if (Input.GetKeyDown(KeyCode.C))
-
{
-
ClearAll();
-
}
-
}
-
-
Vector2 GetPosForTexture()
-
{
-
Vector2 pos = Vector2.zero;
-
RectTransformUtility.ScreenPointToLocalPointInRectangle
-
(mainImage.rectTransform,FanGameManager.Main.GetNumPos(), canvas.worldCamera, out pos);
-
pos.x = mainRect.sizeDelta.x * 0.5f;
-
pos.y = mainRect.sizeDelta.y * 0.5f;
-
return pos;
-
}
-
-
void DrawLine(int startX, int startY, int endX, int endY)
-
{
-
float k = ((float)endY - startY) / ((float)endX - startX);
-
int b = startY - (int)(startX*k);
-
int minX = startX < endX ? startX : endX;
-
int maxX = startX > endX ? startX : endX;
-
int minY = startY < endY ? startY : endY;
-
int maxY = startY > endY ? startY : endY;
-
if (maxY - minY > maxX - minX)
-
{
-
for (int i = 0; i <= maxY - minY; i =radius/2)
-
{
-
if (endX == startX)
-
{
-
DrawPoint(minX,minY i);
-
}
-
else
-
{
-
DrawPoint((int)(((minY i) - b) / k),minY i);
-
}
-
}
-
}
-
else
-
{
-
for (int i = 0; i <= maxX - minX; i =radius/2)
-
{
-
DrawPoint(minX i,b (int)((minX i) * k));
-
}
-
}
-
}
-
-
void DrawPoint(int x,int y)
-
{
-
int setY = 0;
-
for (int i = -radius x; i <= radius x; i )
-
{
-
setY = (radius * radius) - ((i - x) * (i - x));
-
setY = (int) (Math.Sqrt(setY)) y;
-
for (int j = y - (setY - y); j <= setY; j )
-
{
-
if(i<0||i>texture.width||j<0||j>texture.height){break;}
-
if (texture.GetPixel(i, j) != color)
-
{
-
if (minTexture.GetPixel(i,j).a > 0.1f)
-
{
-
setNow ;
-
setColorFill(setNow / setMax);
-
}
-
try
-
{
-
if (!revocations[0].ContainsKey(new Vector2(i, j)))
-
{
-
revocations[0].Add(new Vector2(i,j),texture.GetPixel(i,j));
-
}
-
}catch (Exception e){}
-
}
-
texture.SetPixel(i,j,color);
-
}
-
}
-
SetTexture();
-
}
-
-
void SetTexture()
-
{
-
texture.Apply();
-
mainImage.texture = texture;
-
}
-
-
void Revocation()
-
{
-
if(revocations.Count == 0){return;}
-
foreach (var pos in revocations[0])
-
{
-
texture.SetPixel((int)pos.Key.x,(int)pos.Key.y,pos.Value);
-
}
-
revocations.RemoveAt(0);
-
SetTexture();
-
}
-
-
void ClearAll()
-
{
-
revocations.Insert(0,new Dictionary<Vector2, Color>());
-
for (int i = 0; i <= texture.width; i )
-
{
-
for (int j = 0; j <= texture.height; j )
-
{
-
if (texture.GetPixel(i, j) != backgroundColor)
-
{
-
revocations[0].Add(new Vector2(i,j), texture.GetPixel(i,j));
-
}
-
texture.SetPixel(i, j, backgroundColor);
-
}
-
}
-
-
if (revocations[0].Count == 0)
-
{
-
revocations.RemoveAt(0);
-
}
-
-
SetTexture();
-
}
-
-
public void OnPointerEnter(PointerEventData eventData)
-
{
-
handIn = true;
-
}
-
-
public void OnPointerExit(PointerEventData eventData)
-
{
-
handIn = false;
-
}
-
-
public Texture2D GetTexture()
-
{
-
return texture;
-
}
-
-
Texture2D GetMinTexture(Texture2D texture)
-
{
-
int scaleX = (int) transform.localScale.x;
-
int scaleY = (int) transform.localScale.y;
-
int outX = texture.width % scaleX / 2;
-
int outY = texture.height % scaleY / 2;
-
Texture2D t = new Texture2D(texture.width/scaleX,texture.height/scaleY);
-
List<float> alpha;
-
int q = 0;
-
int e = 0;
-
for (int i = outX; i < texture.width - outX; i = scaleX)
-
{
-
for (int j = outY; j < texture.height - outY; j = scaleY)
-
{
-
alpha = new List<float>();
-
for (int k = 0; k < scaleX; k )
-
{
-
for (int l = 0; l < scaleY; l )
-
{
-
alpha.Add(texture.GetPixel(i k,j l).a);
-
}
-
}
-
t.SetPixel(q,e,new Color(0,0,0,GetMeanValue(alpha)));
-
e ;
-
}
-
q ;
-
}
-
t.Apply();
-
return t;
-
}
-
-
float GetMeanValue(List<float> value)
-
{
-
float meanValue = 0;
-
foreach (float i in value)
-
{
-
meanValue = i;
-
}
-
meanValue /= value.Count;
-
return meanValue;
-
}
-
-
-
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfkikec
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13