【问题标题】:Changing a deprecated code piece in Unity (BoxCollider2D)在 Unity (BoxCollider2D) 中更改已弃用的代码段
【发布时间】:2015-05-04 14:37:38
【问题描述】:

我正在尝试在 Unity 中使用 box collider 功能,但它似乎已被弃用。

我收到消息: "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 类型。

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

【问题讨论】:

    标签: unity3d 2d deprecated obsolete


    【解决方案1】:

    “BoxCollider2D.center 已被弃用。请改用 BoxCollider2D.offset”

    替换:

    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 进行更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-14
      • 2012-01-24
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多