美文网首页
Unity用UGUI制作颜色拾取

Unity用UGUI制作颜色拾取

作者: runinnn | 来源:发表于2020-08-14 13:41 被阅读0次

    使用ugui做一个可以吸取屏幕上图片的颜色的功能。

    然后主要用到的知识点就是

    这个方法了:Texture2D.GetPixel:

    1)函数形式

    public Color GetPixel(int x, int y);

    2)功能描述

    Returns pixel color at coordinates (x, y).

    If the pixel coordinates are out of bounds (larger than width/height or small than 0), they will be clamped or repeated based on the texture's wrap modeTexture coordinates start at lower left cornerIf you are reading a large block of pixels from the texture, it may be faster to use GetPixels32 or GetPixels which returns a whole block of pixel colorsThe texture must have the Read/Write Enabled flag set in the import settings, otherwise this function will fail. GetPixel is not available on Textures using Crunch texture compression.

    有两种比较简单粗暴的方法:

    1、就是直接以整个屏幕做为对象拾取对应点的颜色;

    2、就是用显示的图片自身的Textured2D图片获取对应位置的像素;

    首先就是先找一张热度颜色图,

    用第一种方法的话,就不用对这个图片做任何处理,只需要放在场景中以供选择颜色使用;

    用第二种方法呢,就必须要注重一点:就是图片属性的“Read/Write Enabled“必须选中,不然就读取不到图片的像素。

    如下图”:

    然后就是代码部分了:

    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;

    using UnityEngine.UI;

    using UnityEngine.EventSystems;

    using UnityEngine.Events;

    public class CatchColor : MonoBehaviour,IPointerUpHandler

    {

        /// <summary>

        /// 捕捉的颜色

        /// </summary>

        public Color ReturnColor;

        public Image GetColorImage;

        private RectTransform GetColorImageRect;

        private 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);

            }

        }

        public void OnPointerUp(PointerEventData eventData)

        {

            StopCoroutine(CalColor(eventData));

            StartCoroutine(CalColor(eventData));

        }

        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)));

            }

        }

    }

    相关文章

      网友评论

          本文标题:Unity用UGUI制作颜色拾取

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