参考获取屏幕颜色
原参考里代码格式不方便看,拷过来
这里的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;
}
}