usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassBoxColliderAdjust:MonoBehaviour{publicbool AdjustBoxCollider =false;privateBoxCollider2D boxCollider2D;privateRectTransform gameObject;// Use this for initializationvoidStart(){
gameObject =this.GetComponent<RectTransform>();
boxCollider2D =this.GetComponent<BoxCollider2D>();}// Update is called once per framevoidUpdate(){if(boxCollider2D ==null){
Debug.Log("can't find collider");return;}else{if(AdjustBoxCollider ==true){
boxCollider2D.offset = gameObject.rect.center;//把box collider设置到物体的中心
boxCollider2D.size =newVector2(gameObject.rect.width, gameObject.rect.height);//改变collider大小}}}}