unity测屏幕某个像素点的亮度

参考获取屏幕颜色
原参考里代码格式不方便看,拷过来
这里的screen指的是game视图。

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

using UnityEngine.EventSystems;

using UnityEngine.Events;

[ExecuteInEditMode]
public class CatchColorInTwoWays : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        if (GetColorImage)

        {

            GetColorImageRect = GetColorImage.gameObject.GetComponent<RectTransform>();

            // 添加鼠标事件

            EventTrigger testET = GetColorImageRect.gameObject.AddComponent<EventTrigger>();

            testET.triggers = new List<EventTrigger.Entry>();

            EventTrigger.Entry entryUp = new EventTrigger.Entry();

            entryUp.eventID = EventTriggerType.PointerUp;

            entryUp.callback = new EventTrigger.TriggerEvent();

            // 抬起回调

            UnityAction<BaseEventData> upCB = new UnityAction<BaseEventData>((BaseEventData bed) => {

                OnPointerUp();

            });

            entryUp.callback.AddListener(upCB);

            testET.triggers.Add(entryUp);

        }
   
    }

    // Update is called once per frame
    void Update()
    {

    }
    /// <summary>

    /// 捕捉的颜色

    /// </summary>

    public Color ReturnColor;

    public Image GetColorImage;

    private RectTransform GetColorImageRect;

    public void OnPointerUp()
    {
        StopCoroutine(CalColor(null));

        StartCoroutine(CalColor(null));
    }

    private IEnumerator CalColor(PointerEventData eventData)
    {
        //在每一帧渲染完成后读取信息

        yield return new WaitForEndOfFrame();

       // GetColorByScreen();
       GetColorByTexture(eventData);
    }

    /// <summary>

    /// 基于屏幕读取点的颜色

    /// </summary>

    private void GetColorByScreen()
    {
        Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24,false);

        //读取Rect范围内的像素写入纹理中

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

        texture.Apply();//实际应用

        ReturnColor = texture.GetPixel(Mathf.FloorToInt(Input.mousePosition.x), Mathf.FloorToInt(Input.mousePosition.y));
    }
    /// <summary>

    /// 直接通过自身的Texture获取

    /// </summary>

    private void GetColorByTexture(PointerEventData eventData) {

        Vector2 clickPosition = Vector2.zero;

        if (GetColorImage && eventData != null)

        {

            //获取鼠标点击的对应GetColorImageRect的位置坐标

            RectTransformUtility.ScreenPointToLocalPointInRectangle(GetColorImageRect,eventData.position,eventData.enterEventCamera,out clickPosition);

            ReturnColor = GetColorImage.sprite.texture.GetPixel(Mathf.FloorToInt(clickPosition.x + (GetColorImageRect.rect.width / 2)), Mathf.FloorToInt(clickPosition.y + (GetColorImageRect.rect.height / 2)));

        }

    }

    
}

但是为什么Image里不能拖入普通图片我也不晓得

然后我这边只需要读屏幕一种方法

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

using UnityEngine.EventSystems;

using UnityEngine.Events;

[ExecuteAlways]
public class CatchColor : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
   
    }

    // Update is called once per frame
    void Update()
    {
        OnPointerUp();
    }
    /// <summary>

    /// 捕捉的颜色

    /// </summary>

    public Color c;

    public float brightness;

    public void OnPointerUp()
    {
        StopCoroutine(CalColor(null));

        StartCoroutine(CalColor(null));
    }

    private IEnumerator CalColor(PointerEventData eventData)
    {
        //在每一帧渲染完成后读取信息

        yield return new WaitForEndOfFrame();

        GetColorByScreen();
    }

    /// <summary>

    /// 基于屏幕读取点的颜色

    /// </summary>

    private void GetColorByScreen()
    {
        Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24,false);

        //读取Rect范围内的像素写入纹理中

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

        texture.Apply();//实际应用

        c = texture.GetPixel(Mathf.FloorToInt(Input.mousePosition.x), Mathf.FloorToInt(Input.mousePosition.y));
        brightness = 0.3f * c.r + 0.6f * c.g + 0.1f * c.b;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值