/*****************************
* Title:
* Date: 2022.11.01
* Author: 玄策
* UnityVersion: 2022.1.21
* Func:
*
****************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour
{
RenderTexture rt; //RawImage上的RenderTexture
Texture2D texture;
Color color;
float ins;
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);
//注:当屏幕不是1920*1080时,获取前端页面(1920)与当前屏幕分辨率的比例
//ins = (float)texture.width / (float)Screen.width;
//color = texture.GetPixel((int)(Input.mousePosition.x * ins), (int)(Input.mousePosition.y * ins));
Debug.Log (
color.r * 255 + ", "
+ color.g * 255 + ", "
+ color.b * 255 + ", "
+ color.a * 255);
}
}
}
Unity检测UI上的像素 判断颜色
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 像素深度(bits per pixel,简称:bpp):一个像素由多少位二进制表示。用来表示一个像素的二进制位数越...
- 直接上代码吧,不想bb了 下面为测试结果: 测试结果1: 测试结果2: 注意:上图的画布使用Overlay模式,所...