在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实战笔记1 小时前
汽车制造业HMI设计特点:高节拍生产的界面优化
ui·信息可视化·性能优化·自动化·汽车·交互
夏河始溢1 小时前
一七九、WebRTC介绍
前端·人工智能·ui
muddjsv21 小时前
从用户需求到产品体验:UI/UX 设计核心方法论与实战指南
ui
一起养小猫1 天前
Axure day2 基础教程完整指南
ui·axure·photoshop
AI_零食1 天前
红蓝之辨:基于 Flutter 的动静脉血动力学可视化系统开发
flutter·ui·华为·harmonyos·鸿蒙
灵感菇_1 天前
深入解析 Android事件分发机制
android·ui
尤老师FPGA2 天前
使用ZYNQ芯片和LVGL框架实现用户高刷新UI设计系列教程(第四十五讲)
android·java·ui
CodeCraft Studio2 天前
从框架到体验:Qt + Qtitan 构建制造业嵌入式UI整体解决方案
开发语言·qt·ui·gui·嵌入式开发·hmi·制造业嵌入式ui
AI_零食2 天前
鸿蒙的flutter框架表达:生命律动系统
学习·flutter·ui·华为·harmonyos·鸿蒙
AI_零食2 天前
鸿蒙跨端框架 Flutter 学习 Day 6:Future 在 UI 渲染中的心跳逻辑
学习·flutter·ui·华为·harmonyos·鸿蒙