美文网首页unityUnity技术分享Unity教程合集
ugui 基于图片透明度的点击过滤器

ugui 基于图片透明度的点击过滤器

作者: Babybus_Unity | 来源:发表于2015-12-17 13:55 被阅读616次
    using UnityEngine;
    
    using System.Collections;
    
    using UnityEngine.UI;
    
    using System.IO;
    
     
    
    [DisallowMultipleComponent]
    
    [RequireComponent(typeof(Image))]
    
    [ExecuteInEditMode]
    
    public class ImageRaycastFilter : MonoBehaviour, ICanvasRaycastFilter
    
    {
    
        public bool reversed;
    
     
    
        private Image image;
    
     
    
        void OnEnable()
    
        {
    
            image = GetComponent<Image>();
    
            image.type = Image.Type.Simple;
    
        }
    
     
    
        public bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
    
        {
    
            if (!enabled)
    
                return true;
    
     
    
            Sprite sprite = image.overrideSprite;
    
            if (sprite == null)
    
                return true;
    
     
    
            Vector2 local;
    
            RectTransformUtility.ScreenPointToLocalPointInRectangle(image.rectTransform, screenPoint, eventCamera, out local);
    
     
    
            Rect rect = image.rectTransform.rect;
    
     
    
            // Convert to have lower left corner as reference point.
    
            local.x += image.rectTransform.pivot.x * rect.width;
    
            local.y += image.rectTransform.pivot.y * rect.height;
    
     
    
            float u = local.x / rect.width;
    
            float v = local.y / rect.height;
    
     
    
            try
    
            {
    
                if(!reversed)
    
                    return sprite.texture.GetPixelBilinear(u, v).a != 0;
    
                else
    
                    return sprite.texture.GetPixelBilinear(u, v).a == 0;
    
            }
    
            catch (UnityException e)
    
            {
    
                Debug.LogException(e);
    
                return true;
    
            }
    
        }
    
    }
    
    Paste_Image.png

    对图片进行任意变形都支持。只有非透明区域是可以点击的。也可以反过来用,对透明区域可以点击,比如用来做点击遮罩。

    using UnityEngine;
    
    using System.Collections;
    
    using UnityEngine.UI;
    
    using System.IO;
    
     
    
    [DisallowMultipleComponent]
    
    [RequireComponent(typeof(RawImage))]
    
    public class RawImageRaycastFilter : MonoBehaviour, ICanvasRaycastFilter
    
    {
    
        public bool reversed;
    
     
    
        private RawImage rawImage;
    
     
    
        void OnEnable()
    
        {
    
            rawImage = GetComponent<RawImage>();
    
        }
    
     
    
        public bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
    
        {
    
            if (!enabled)
    
                return true;
    
     
    
            Texture2D texture = rawImage.texture as Texture2D;
    
            if (texture == null)
    
                return true;
    
     
    
            Vector2 local;
    
            RectTransformUtility.ScreenPointToLocalPointInRectangle(rawImage.rectTransform, screenPoint, eventCamera, out local);
    
     
    
            Rect rect = rawImage.rectTransform.rect;
    
     
    
            // Convert to have lower left corner as reference point.
    
            local.x += rawImage.rectTransform.pivot.x * rect.width;
    
            local.y += rawImage.rectTransform.pivot.y * rect.height;
    
     
    
            Rect uvRect = rawImage.uvRect;
    
            float u = local.x / rect.width * uvRect.width + uvRect.x;
    
            float v = local.y / rect.height * uvRect.height + uvRect.y;
    
     
    
            try
    
            {
    
                if (!reversed)
    
                    return texture.GetPixelBilinear(u, v).a != 0;
    
                else
    
                    return texture.GetPixelBilinear(u, v).a == 0;
    
            }
    
            catch (UnityException e)
    
            {
    
                Debug.LogException(e);
    
                return true;
    
            }
    
        }
    
    } 
    
    ![3.png](https://img.haomeiwen.com/i1239281/0de722714aa6d388.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

    相关文章

      网友评论

        本文标题:ugui 基于图片透明度的点击过滤器

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