unity不规则按钮解决方案

Posted sanyejun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于unity不规则按钮解决方案的主要内容,如果未能解决你的问题,请参考以下文章

Unity3D日常开发Unity3D中实现不规则Button按钮的精准响应

Unity UGUI不规则区域按钮点击实现

unity按钮松不开了,卡住了

由小见大!不规则造型按钮解决方案

Unity UI按钮不调用Onclick方法

Raycast 未检测到自定义 Unity UI 按钮脚本