Unity完美的UI绘画工具

60 篇文章 2 订阅
6 篇文章 0 订阅

直接将代码放在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;
    }
}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
Unity科技UI是一种非常强大和灵活的用户界面工具,它可以用于开发各种类型的应用程序和游戏。它提供了许多组件和功能,可以帮助开发者快速创建漂亮和交互式的用户界面。 Unity科技UI具有很多特色功能。首先,它提供了各种预制件和布局工具,使开发者可以轻松地创建和管理UI元素。开发者可以使用预制件快速添加按钮、文本、图像等UI元素,并使用布局工具自动调整它们的位置和大小。 其次,Unity科技UI具有可编程性,开发者可以使用脚本语言编写逻辑代码,从而实现更复杂的UI功能。例如,开发者可以编写代码来处理按钮点击事件、显示/隐藏UI元素、根据玩家输入更改UI状态等等。 此外,Unity科技UI还提供了丰富的过渡和动画效果。开发者可以通过简单的配置设置实现按钮按下、UI元素进入和退出等动画效果,从而提升用户界面的体验和吸引力。 最后,Unity科技UI还支持多平台开发。开发者可以使用Unity引擎制作适用于各种平台的应用程序和游戏,包括PC、移动设备、虚拟现实设备等。不同平台的UI适配由Unity自动处理,大大简化了开发流程。 总之,Unity科技UI是一个功能强大和易用的工具,为开发者提供了创建精美和交互式用户界面的各种功能和工具。无论是开发游戏还是应用程序,Unity科技UI都是一个令人满意的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值