美文网首页
unity 截图

unity 截图

作者: runinnn | 来源:发表于2023-02-16 22:12 被阅读0次

using UnityEngine;

using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour

{

    RenderTexture rt;  //RawImage上的RenderTexture

    Texture2D texture;

    Color color;

    void Start()

    {

        StartCoroutine(CaptureScreenshot());

    }

    IEnumerator CaptureScreenshot()

    {

        while (true)

        {

            //只在每一帧渲染完成后才读取屏幕信息

            yield return new WaitForEndOfFrame();

            //RawImage上的RenderTexture

            rt = GetComponent<RawImage>().texture as RenderTexture;

            texture = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false);

            RenderTexture.active = rt;

            texture.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);

            //RawImage上的trxture

            //texture = GetComponent<RawImage>().texture as Texture2D;

            texture.Apply();

            color = texture.GetPixel((int)Input.mousePosition.x, (int)Input.mousePosition.y);

            Debug.Log (

                  color.r * 255 + ",      "

                + color.g * 255 + ",      "

                + color.b * 255 + ",      "

                + color.a * 255);

        }

    }

}

相关文章

网友评论

      本文标题:unity 截图

      本文链接:https://www.haomeiwen.com/subject/wpfafdtx.html