ugui中实现圆形按钮

实现圆形按钮,原本是使用 alphHitTestMinimumThreshold

改成重载IsRaycastLocationValid来实现,直接贴代码

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class UICircle : Image
{
    RectTransform m_RectTransform;
    protected UICircle()
    {
        
    }
    protected override void OnPopulateMesh(VertexHelper vh)
    {
        vh.Clear();
    }
    public override bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
    {
        if(m_RectTransform == null)
            m_RectTransform = GetComponent();
        Vector2 position;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(m_RectTransform, screenPoint, eventCamera, out position);
        var w = m_RectTransform.rect.width;
        var h = m_RectTransform.rect.height;
        var x = position.x;
        var y = position.y;
        var radius = Mathf.Min(w, h) / 2;       
        var b = x*x +y*y <=radius*radius;
        Debug.Log(b);
        return b;
    }

#if UNITY_EDITOR
    protected override void Reset()
    {
        base.Reset();
      
    }
#endif
}
#if UNITY_EDITOR
[CustomEditor(typeof(UICircle), true)]
public class UIPolygonInspector : Editor
{
    public override void OnInspectorGUI()
    {
    }
}
#endif

 

参考

https://www.jianshu.com/p/e82b22a9c5eb

附上地址:

https://github.com/yingsz/UGUI/tree/master/UGUI

转载于:https://www.cnblogs.com/marcher/p/10517368.html

你可能感兴趣的:(游戏)