在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大小
            }
        }
    }
}
相关推荐
L、2182 小时前
Flutter + OpenHarmony 分布式能力融合:实现跨设备 UI 共享与协同控制(终极篇)
javascript·分布式·flutter·ui·智能手机·harmonyos
鸿蒙开发工程师—阿辉2 小时前
HarmonyOS 5 极致动效实验室:给 UI 注入“物理动效”
ui·华为·harmonyos
lin625342219 小时前
Android右滑解锁UI,带背景流动渐变动画效果
android·ui
hnlgzb21 小时前
material3和xml的UI会相差很大么?
xml·ui
kylinmin1 天前
卸载微软电脑管家:一次性彻底移除
前端·ui·xhtml
Wiktok1 天前
【WIT】解决导入pywinauto相关库会导致程序UI界面(tkinter/pyside6)浏览文件等操作卡住问题
python·ui·pywinauto
lin62534221 天前
Android九宫格,1张图到9张图适配;图片自定义UI
android·ui·kotlin
解局易否结局1 天前
鸿蒙UI开发中Flutter的现状与鸿蒙系统UI生态未来方向
flutter·ui·harmonyos
晚霞的不甘1 天前
Flutter + OpenHarmony 设计系统实战:构建统一、美观、无障碍的跨端 UI 体系
flutter·ui