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 FontList = new List();
    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);
    }
}

你可能感兴趣的:(unity,游戏引擎)