unity字体重叠

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class JIETU : MonoBehaviour
{
    public RectTransform UIRect;
    public Camera camera;
    public RawImage rawImage;
    private void Start()
    {
        rawImage.texture = camera.targetTexture;
        TuYa1.gameObject.SetActive(false);
        TuYa2.gameObject.SetActive(false);
    }
    void Update()
    {
      
        if (Input.GetKeyDown(KeyCode.Space))
        {
            string fileName = Application.dataPath + "/StreamingAssets/" + "123.png";
            系统不识别标点符号,但支持中文
            IEnumerator coroutine = CaptureByUI(UIRect, fileName);
            StartCoroutine(coroutine);

        }

        if (Input.GetKeyDown(KeyCode.A))
        {
            string fileName = Application.dataPath + "/StreamingAssets/" + "123.png";
            系统不识别标点符号,但支持中文
            IEnumerator coroutine = CaptureByUITwo(UIRect, fileName);
            StartCoroutine(coroutine);

        }
    }



    public GameObject TuYa1;
    public GameObject TuYa2;
    public List<Vector2> FontList = new List<Vector2>();
    public Color  FontColor;
    public IEnumerator CaptureByUI(RectTransform UIRect, string mFileName)
    {
        //等待帧画面渲染结束
        yield return new WaitForEndOfFrame();

        int width = (int)(UIRect.rect.width);
        int height = (int)(UIRect.rect.height);

        Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

        //左下角为原点(0, 0)
        float leftBtmX = UIRect.transform.position.x + UIRect.rect.xMin;
        float leftBtmY = UIRect.transform.position.y + UIRect.rect.yMin;

        //从屏幕读取像素, leftBtmX/leftBtnY 是读取的初始位置,width、height是读取像素的宽度和高度
        tex.ReadPixels(new Rect(leftBtmX, leftBtmY, width, height), 0, 0);
        //执行读取操作
        tex.Apply();
        Color pixelColor;
        Color bgColor = new Color(1, 1, 1, 1);
        Color blackColor = new Color(0, 0, 0, 1);
        float num = 0;
        int h = tex.height;
        int w = tex.width;
        

        //需要做的事情 第一步 记录初始字体的坐标集合
        //从左到右 从上到下
        for (int y = h - 1; y >= 0; y--)
        {
            for (int x = 0; x < w; x++)
            {
                pixelColor = tex.GetPixel(x, y);
                if (pixelColor != bgColor && pixelColor != blackColor)
                {
                    FontColor = pixelColor;
                    num = num + 1;
                    print($"x = {x},y={y},color={pixelColor.ToString()}");
                    FontList.Add(new Vector2(x, y));
                }
            }
        }
        print($"面积为{num / (h*w)}");
        byte[] bytes = tex.EncodeToPNG();
        //保存
        System.IO.File.WriteAllBytes(mFileName, bytes);
    }


    public IEnumerator CaptureByUITwo(RectTransform UIRect, string mFileName)
    {
        //打开需要画的图层
       // TuYa1.gameObject.SetActive(true);
        //TuYa2.gameObject.SetActive(true);
        //等待帧画面渲染结束
        yield return new WaitForEndOfFrame();

        int width = (int)(UIRect.rect.width);
        int height = (int)(UIRect.rect.height);

        Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

        //左下角为原点(0, 0)
        float leftBtmX = UIRect.transform.position.x + UIRect.rect.xMin;
        float leftBtmY = UIRect.transform.position.y + UIRect.rect.yMin;

        //从屏幕读取像素, leftBtmX/leftBtnY 是读取的初始位置,width、height是读取像素的宽度和高度
        tex.ReadPixels(new Rect(leftBtmX, leftBtmY, width, height), 0, 0);
        //执行读取操作
        tex.Apply();
        Color pixelColor;
        float num = 0;

        //需要做的事情 第一步 记录初始字体的坐标集合
        //第二部计算  在字体坐标位置的颜色 不为原来颜色的集合
        for (int i = 0; i < FontList.Count; i++)
        {
            int x = (int)FontList[i].x;
            int y = (int)FontList[i].y;
            pixelColor = tex.GetPixel(x, y);
            if (FontColor != pixelColor)
            {
                num = num + 1;
                print($"x = {x},y={y},color={pixelColor.ToString()}");
            }
          
        }

        print($"面积为{num / (FontList.Count)}");
        byte[] bytes = tex.EncodeToPNG();
        //保存
        System.IO.File.WriteAllBytes(mFileName, bytes);
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值