赞
踩
直接将代码放在RawImage上(不能放在Image上面)
包含了撤销和清除功能,两个功能也可以联动
需要注意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 PaintingGraffiti : 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 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;
- 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++;
- }
- }
-
- setMax = setMax * 0.9f;
- texture.Apply();
- mainImage.texture = texture;
- mainImage.SetNativeSize();
- }
-
- private void Update()
- {
- if(!handIn || isClose){return;}
-
- if (GameManager.Main.GetClick())
- {
- revocations.Insert(0,new Dictionary<Vector2, Color>());
- }
-
- if (GameManager.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 (GameManager.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,GameManager.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)
- {
- setNow++;
- setColorFill(setNow / setMax);
- if (!revocations[0].ContainsKey(new Vector2(i, j)))
- {
- revocations[0].Add(new Vector2(i,j),texture.GetPixel(i,j));
- }
- }
- 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;
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。