Unity完美的上色并检测完成度工具

这篇具有很好参考价值的文章主要介绍了Unity完美的上色并检测完成度工具。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

直接将代码放在RawImage上(不能放在Image上面)

附加代码的物体RawImage上面的Texture需要设定为空,然后附加Mask组件,子物体同时需要RawImage组件并设定为需要上色展示的图像,可以忽略透明区域检测上色百分比

需要注意revocations变量不能等于1或者0,否则会进入死循环

用起来如果有卡顿,就改变RawImage的Scale值,Scale越大绘画越流畅

文中用到的GameManager在这篇文章:Unity学习笔记:在GameManager里记录手游操作框架_努力长头发的程序猿的博客-CSDN博客文章来源地址https://www.toymoban.com/news/detail-515671.html

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;
    }


}

到了这里,关于Unity完美的上色并检测完成度工具的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包