在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大小
            }
        }
    }
}
相关推荐
工业HMI实战笔记28 分钟前
机床设备HMI:加工参数可视化与故障诊断界面
ui·性能优化·自动化·汽车·交互
钛态4 小时前
Flutter for OpenHarmony 实战:Pretty Dio Logger — 网络请求监控利器
flutter·microsoft·ui·华为·架构·harmonyos
沛沛老爹5 小时前
Vue3+TS实战:基于策略模式的前端动态脱敏UI组件设计与实现
前端·ui·vue3·数据安全·策略模式·动态渲染·前端脱敏
钛态6 小时前
Flutter for OpenHarmony 实战:Supabase — 跨平台后端服务首选
flutter·ui·华为·架构·harmonyos
小疙瘩18 小时前
element-ui 中 el-upload 多文件一次性上传的实现
javascript·vue.js·ui
UI设计兰亭妙微1 天前
资金运营管理监控系统交互设计、UI设计及VUE开发
vue.js·ui·交互
工业HMI实战笔记1 天前
包装机械HMI:快速换型与配方管理的界面解决方案
ui·性能优化·自动化·汽车·交互
熊猫钓鱼>_>1 天前
【开源鸿蒙跨平台开发先锋训练营】Day 12:全场景适配与异常防护——构建高可靠的鸿蒙跨端体验
react native·ui·华为·开源·app·harmonyos·鸿蒙
m5655bj1 天前
通过 Python 删除 Excel 中的空白行列
python·ui·excel