直接将代码放在RawImage上(不能放在Image上面)
附加代码的物体RawImage上面的Texture需要设定为空,然后附加Mask组件,子物体同时需要RawImage组件并设定为需要上色展示的图像,可以忽略透明区域检测上色百分比
需要注意revocations变量不能等于1或者0,否则会进入死循环
用起来如果有卡顿,就改变RawImage的Scale值,Scale越大绘画越流畅
文中用到的GameManager在这篇文章:Unity学习笔记:在GameManager里记录手游操作框架_努力长头发的程序猿的博客-CSDN博客
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;
}
}