您好,堆栈溢出用户!我正在统一2018.2.2f1中创建一个游戏。一切都很顺利,但是我在程序中错放了代码行的顺序->这导致了infite while循环的出现。我不知道它将是无限的,所以我运行了该程序。现在,当我单击图标时,我的统一窗口是不可选择的,当我进入窗口时,所有内容都被冻结。我将通过任务管理器关闭程序,但是我没有保存文件,甚至无法保存项目。在这种状态下,我有什么方法可以保存项目吗?我的团结并没有崩溃,只是僵住了。 如果可以的话,这是我的固定C#代码(我在Visual Studio中):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour {
public float speed = 10f;
private Vector3 velocity = new Vector3();
private string direction = "none";
private bool canMoveAgain = true;
private bool go = false;
// Use this for initialization
void Start () {
speed = 10f;
}
// Update is called once per frame
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.name == "wall (3)" && direction == "down")
{
go = false;
}
if (collision.collider.name == "wall (2)" && direction == "up")
{
go = false;
}
if (collision.collider.name == "wall" && direction == "left")
{
go = false;
}
if (collision.collider.name == "wall (1)" && direction == "right")
{
go = false;
}
}
void Update () {
if (Input.GetKeyDown(KeyCode.DownArrow) && canMoveAgain)
{
direction = "down";
go = true;
canMoveAgain = false;
while (go)
{
velocity = new Vector3(0,-1,0);
transform.position += velocity;
}
velocity = new Vector3(0, 0, 0);
direction = "none";
} else if (Input.GetKeyDown(KeyCode.UpArrow) && canMoveAgain)
{
direction = "up";
go = true;
canMoveAgain = false;
while (go)
{
velocity = new Vector3(0, +1, 0);
transform.position += velocity;
}
velocity = new Vector3(0, 0, 0);
direction = "none";
} else if (Input.GetKeyDown(KeyCode.LeftArrow) && canMoveAgain)
{
direction = "left";
go = true;
canMoveAgain = false;
while (go)
{
velocity = new Vector3(-1, 0, 0);
transform.position += velocity;
}
velocity = new Vector3(0, 0, 0);
direction = "none";
} else if (Input.GetKeyDown(KeyCode.RightArrow) && canMoveAgain)
{
direction = "right";
go = true;
canMoveAgain = false;
while (go)
{
velocity = new Vector3(+1, 0, 0);
transform.position += velocity;
}
velocity = new Vector3(0, 0, 0);
direction = "none";
}
}
}
答案 0 :(得分:1)
你做
while (go)
{
velocity = new Vector3(+1, 0, 0);
transform.position += velocity;
}
但是在循环内,go
从未设置为false。
OnCollisionEnter
从未执行,因为while
仍在执行=>冻结。
由于Update
被称为每一帧,因此您应该在检查输入..
if(go)
块中。
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.name == "wall (3)" && direction == "down"
|| collision.collider.name == "wall (2)" && direction == "up"
|| collision.collider.name == "wall" && direction == "left"
|| collision.collider.name == "wall (1)" && direction == "right")
{
go = false;
velocity = Vector3.zero;
direction = "none"
}
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.DownArrow) && canMoveAgain)
{
direction = "down";
go = true;
canMoveAgain = false;
velocity = -Vector3.up;
}
else if (Input.GetKeyDown(KeyCode.UpArrow) && canMoveAgain)
{
direction = "up";
go = true;
canMoveAgain = false;
velocity = Vector3.up;
}
else if (Input.GetKeyDown(KeyCode.LeftArrow) && canMoveAgain)
{
direction = "left";
go = true;
canMoveAgain = false;
velocity = -Vector3.right;
}
else if (Input.GetKeyDown(KeyCode.RightArrow) && canMoveAgain)
{
direction = "right";
go = true;
canMoveAgain = false;
velocity = Vector3.right;
}
if(go)
{
transform.position += velocity;
}
}
请注意,当前您的代码还取决于帧频。您可能想使用类似
的方法public float moveSpeed;
...
transform.position += velocity * moveSpeed * Time.deltaTime;
对于direction
,我宁愿推荐enum
之类的
private enum MoveDirection
{
None,
Up,
Down,
Left,
Right
}
并使用它,例如喜欢
direction = MoveDirection.Up;
和
if(direction == MoveDirection.Up)
比比较字符串更有效。
注意:在智能手机上键入内容,因此没有保修,但我希望这个主意会清楚
答案 1 :(得分:0)
首先。不要关闭计算机。
您可以做一些事情。 幸运的是,当您按下run时,您的代码已经有了备份。 它在Temp文件夹中。如果再次按运行,将替换临时文件夹。将整个项目复制到安全的地方。
尝试查看Temp文件夹是否具有正确的备份。
您还有另一种选择。附加一个调试器,它可以是Visual Studio或MonoDevelop。将它们连接到Unity后,只需按Pause。而且您可以自由保存自己的东西。
希望您早日找到答案。