在ui上使用boxcollider的问题

  • 问题就是boxCollider不会随着UI缩放而缩放
  • 这个问题也很简单
  • 下面脚本可以实现随着recttransform来同步设置boxcollider
csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoxColliderAdjust : MonoBehaviour
{
    public bool AdjustBoxCollider = false;
    private BoxCollider2D boxCollider2D;
    private RectTransform gameObject;
    // Use this for initialization
    void Start()
    {
        gameObject = this.GetComponent<RectTransform>();
        boxCollider2D = this.GetComponent<BoxCollider2D>();
    }
    // Update is called once per frame
    void Update()
    {
        if (boxCollider2D == null)
        {
            Debug.Log("can't find collider");
            return;
        }
        else
        {
            if (AdjustBoxCollider == true)
            {
                boxCollider2D.offset = gameObject.rect.center;      //把box collider设置到物体的中心
                boxCollider2D.size = new Vector2(gameObject.rect.width, gameObject.rect.height);    //改变collider大小
            }
        }
    }
}
相关推荐
久爱物联网6 小时前
【WinForm UI控件系列】BarPlot柱状图控件
ui·ui控件·winformui·csharpui控件·桌面ui控件
for_ever_love__7 小时前
UI学习:多界面传值的正向传值(属性传值)和反向传值(代理传值)
学习·ui·ios·objective-c
Python私教9 小时前
FuturesDesk:配置驱动 UI 的 Electron 金融桌面应用模板
ui·金融·electron
椰羊~王小美9 小时前
LVGL 界面UI库
ui
久爱物联网12 小时前
【WinForm UI控件系列】Breadcrumb 面包屑控件,支持三种样式
ui·breadcrumb·面包屑控件·winformui·csharpui控件·桌面ui控件
久爱物联网13 小时前
【WinForm UI控件系列】PieChart饼状图控件
ui·winformui控件·c#控件ui·桌面应用ui控件·gdi绘制控件
久爱物联网13 小时前
【WinForm UI控件系列】Blower 鼓风机控件
ui·ui控件·桌面应用控件·鼓风机控件·winfrom控件
久爱物联网14 小时前
【WinForm UI控件系列】Battery 电池电量控件
ui·winformui控件·桌面应用控件·c#控件ui·ui控件gdi
ZC跨境爬虫15 小时前
UI前端美化技能提升日志day5:从布局优化到CSS继承原理深度解析
前端·css·ui·html·状态模式
久爱物联网15 小时前
【WinForm UI控件系列】AlarmLight 报警灯\声光报警灯控件
ui·winformui控件·桌面应用控件·c# ui控件·gdi控件 net控件