zoukankan      html  css  js  c++  java
  • unity不规则按钮解决方案

    一种是alpha检测

    一种是设置collider

    参考:

    https://zhuanlan.zhihu.com/p/34204396

    下面给出第二种方案代码

    ///按钮多边形点击方案,注意Canvas模式应该是Screen Space - Camera  需设置 Render Camera
    ///选中按钮右键UI->变更为多边形按钮,编辑子物体的Collider2D即可
    ///如果无法编辑Collider 则是Unity编辑器bug 切换下Unity编辑器的布局模式,即可恢复正常
    
    using UnityEngine;
    using UnityEngine.UI;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    [RequireComponent(typeof(PolygonCollider2D))]
    public class NonRectangularButtonImage : Image
    {
        private PolygonCollider2D areaPolygon;
    
        protected NonRectangularButtonImage()
        {
            useLegacyMeshGeneration = true;
        }
    
        private PolygonCollider2D Polygon
        {
            get
            {
                if (areaPolygon != null)
                    return areaPolygon;
    
                areaPolygon = GetComponent<PolygonCollider2D>();
                return areaPolygon;
            }
        }
    
        protected override void OnPopulateMesh(VertexHelper vh)
        {
            vh.Clear();
        }
    
        public override bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
        {
            return Polygon.OverlapPoint(eventCamera.ScreenToWorldPoint(screenPoint));
        }
    
    #if UNITY_EDITOR
        protected override void Reset()
        {
            base.Reset();
            transform.localPosition = Vector3.zero;
            var w = rectTransform.sizeDelta.x * 0.5f + 0.1f;
            var h = rectTransform.sizeDelta.y * 0.5f + 0.1f;
            Polygon.points = new[]
            {
                new Vector2(-w, -h),
                new Vector2(w, -h),
                new Vector2(w, h),
                new Vector2(-w, h)
            };
        }
    #endif
    }
    #if UNITY_EDITOR
    [CustomEditor(typeof(NonRectangularButtonImage), true)]
    public class CustomRaycastFilterInspector : Editor
    {
        public override void OnInspectorGUI()
        {
        }
    }
    
    public class NonRectAngularButtonImageHelper
    {
        [MenuItem("GameObject/UI/变更为多边形按钮")]
        public static void CreateNonRectAngularButtonImage()
        {
            var goRoot = Selection.activeGameObject;
            if (goRoot == null)
                return;
    
            var button = goRoot.GetComponent<Button>();
    
            if (button == null)
            {
                Debug.Log("Selecting Object is not a button!");
                return;
            }
    
            // 关闭原来button的射线检测
            var graphics = goRoot.GetComponentsInChildren<Graphic>();
            foreach (var graphic in graphics)
            {
                graphic.raycastTarget = false;
            }
    
            var polygon = new GameObject("NonRectangularButtonImage");
            polygon.AddComponent<PolygonCollider2D>();
            polygon.AddComponent<NonRectangularButtonImage>();
            polygon.transform.SetParent(goRoot.transform, false);
            polygon.transform.SetAsLastSibling();
            //设置锚点方案,注意rect transfrom的框必须包住多边形,否则超出的部分无效
            polygon.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0);
            polygon.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0);
            polygon.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, 0);
            polygon.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 0, 0);
            polygon.GetComponent<RectTransform>().anchorMin = new Vector2(0, 0);
            polygon.GetComponent<RectTransform>().anchorMax = new Vector2(1, 1);
        }
    }
    
    #endif
  • 相关阅读:
    不要同时使用ReentrantLock类与synchronized关键字锁定会修改同一个资源的不同方法
    java中volatile关键字的含义
    浅谈操作系统对内存的管理
    Java线程池使用说明
    写在清明节之后
    PY一天一学
    24小时只睡了1个小时
    关于团队关于吐槽
    出尔反尔
    时间都去哪儿了?
  • 原文地址:https://www.cnblogs.com/sanyejun/p/11231406.html
Copyright © 2011-2022 走看看