在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大小
            }
        }
    }
}
相关推荐
John_ToDebug1 天前
深入解析 Chrome UI 布局配置的设计思想与实现机制
chrome·ui
代码小念1 天前
Pytest+selenium UI自动化测试实战实例(超详细)
selenium·ui·pytest
Aczone282 天前
Linux Framebuffer(帧缓冲)与基本 UI 绘制技术
linux·运维·ui
卖寂寞的小男孩3 天前
Spark执行计划与UI分析
ui·ajax·spark
★YUI★3 天前
学习游制作记录(背包UI以及各种物品的存储)8.12
学习·游戏·ui·unity·c#
凯子坚持 c4 天前
虚拟机一站式部署Claude Code &可视化UI界面
ui·编辑器·vim
伽蓝_游戏5 天前
UGUI源码剖析(5):事件的旅程——EventSystem的架构与输入处理管线
游戏·ui·unity·架构·c#·游戏引擎·.net
孤蓬&听雨6 天前
Axure基于中继器实现的组件库(导航菜单、动态表格)
ui·axure·动态表格·中继器·导航菜单
传奇开心果编程6 天前
【传奇开心果系列】Flet框架平面级联菜单侧边栏和和登录用户圆形头像自定义组件模板
python·ui·前端框架