在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大小
            }
        }
    }
}
相关推荐
for_ever_love__3 分钟前
UI学习:UICollectionView瀑布流
学习·ui·ios·objective-c·cocoa
xiaobai1783 小时前
pytest+playwright实现UI自动化(4)-上夹具fixture
ui·自动化·pytest·playwright
为何创造硅基生物5 小时前
LVGL 妙用 LV_OBJ_FLAG_FLOATING
ui
ANnianStriver7 小时前
PetLumina 04 — 管理后台 UI 全面升级
java·ui·ai编程
ANnianStriver8 小时前
PetLumina 05 — App 端 UI 效果应用
java·ui·ai编程
小小龙学IT8 小时前
Midscene.js:AI驱动的跨平台UI自动化革命
javascript·人工智能·ui
Wenzar_10 小时前
Playwright 实战:高可信 UI 回归验证流水线
java·ui
文创工作室1 天前
Adobe Illustrator 中文
ui·adobe·illustrator
烛衔溟1 天前
HarmonyOS 基础 UI 构建 —— 组件、布局与沉浸式效果
ui·华为·harmonyos
一次旅行1 天前
CopilotKit实战:用生成式UI打造智能Agent前端
前端·人工智能·ui