更改 Unity 中已弃用的代码片段 (BoxCollider2D)

Changing a deprecated code piece in Unity (BoxCollider2D)

我正在尝试使用 Unity 中的长方体碰撞器功能,但它似乎已被弃用。

我收到消息: "UnityEngine.BoxCollider2D.center" is obsolete. BoxCollider2D.center has been deprecated. Use BoxCollider2D.offset instead (UnityUpgradable)

我一直在尝试在屏幕的边缘设置墙。这是代码:

//Move each wall to its edge location:

topWall.size = new Vector2 (mainCam.ScreenToWorldPoint (new Vector3 (Screen.width * 2f, 0f, 0f)).x, 1f);
topWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3 ( 0f, Screen.height, 0f)).y + 0.5f);

bottomWall.size = new Vector2 (mainCam.ScreenToWorldPoint (new Vector3 (Screen.width * 2, 0f, 0f)).x, 1f);
bottomWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3( 0f, 0f, 0f)).y - 0.5f);

leftWall.size = new Vector2(1f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);;
leftWall.center = new Vector2(mainCam.ScreenToWorldPoint(new Vector3(0f, 0f, 0f)).x - 0.5f, 0f);

rightWall.size = new Vector2(1f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);
rightWall.center = new Vector2(mainCam.ScreenToWorldPoint(new Vector3(Screen.width, 0f, 0f)).x + 0.5f, 0f);
//Move the players to a fixed distance from the edges of the screen:
Player01.position.x = mainCam.ScreenToWorldPoint (new Vector3 (75f, 0f, 0f)).x;
Player02.position.x = mainCam.ScreenToWorldPoint (new Vector3 (Screen.width -75f, 0f, 0f)).x;

TopWallBottomWallLeftWallRightWall 当然都是 BoxCollider2D.

类型

我应该如何更改我的代码才能不收到此错误消息?感谢您的帮助。

"BoxCollider2D.center has been deprecated. Use BoxCollider2D.offset instead"

替换:

topWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3 ( 0f, Screen.height, 0f)).y + 0.5f);

与:

topWall.offset = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3 ( 0f, Screen.height, 0f)).y + 0.5f);

当然也要更改为 BottomWall、LeftWall 和 RightWall。