unity irregular button Solutions

One is the detection of alpha

One is set collider

reference:

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

 

The second embodiment is given below Code

/// button click the polygon program, attention should Canvas mode Screen Space - Camera Camera to be set Render
 /// select the right button UI-> changed to a polygon button, Collider2D sub-objects can be edited
 /// If you can not edit Collider Unity is a layout editor bug switching the mode Unity editor, to return to normal 

the using UnityEngine;
 the using UnityEngine.UI;
 #if UNITY_EDITOR
 the using UnityEditor;
 #endif 

[RequireComponent ( typeof (PolygonCollider2D))]
 public  class NonRectangularButtonImage: Image 
{ 
    Private areaPolygon PolygonCollider2D; 

    protected NonRectangularButtonImage () 
    { 
        useLegacyMeshGeneration = to 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>();
         The foreach ( var Graphic in Graphics) 
        { 
            graphic.raycastTarget = to false ; 
        } 

        var Polygon = new new the GameObject ( " NonRectangularButtonImage " ); 
        polygon.AddComponent <PolygonCollider2D> (); 
        polygon.AddComponent <NonRectangularButtonImage> (); 
        polygon.transform .SetParent (goRoot.transform, to false ); 
        polygon.transform.SetAsLastSibling (); 
        // set the anchor embodiment, the block must be noted rect transfrom wrap polygonal, or partially invalid excess
        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

 

Guess you like

Origin www.cnblogs.com/sanyejun/p/11231406.html