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<RectTransform>();
        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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值