0
这是影像UI的精灵形象取决于键切换代码触发(S)按下:代码if语句被忽略
using UnityEngine;
using UnityEngine.UI;
public class ImageController : MonoBehaviour {
public Sprite left;
public Sprite topleft;
public Sprite backleft;
public Sprite right;
public Sprite topright;
public Sprite backright;
public Sprite top;
public Sprite back;
public Sprite nothing;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
if (Input.GetKey(KeyCode.UpArrow))
{
if (Input.GetKey(KeyCode.LeftArrow))
{
Debug.Log("HI"); // Not ignored??
GetComponent<Image>().sprite = topleft; // Ignored
}
if (Input.GetKey(KeyCode.RightArrow))
{
GetComponent<Image>().sprite = topright;
}
else
{
GetComponent<Image>().sprite = top;
}
}
if (Input.GetKey(KeyCode.DownArrow))
{
if (Input.GetKey(KeyCode.LeftArrow))
{
GetComponent<Image>().sprite = backleft;
}
if (Input.GetKey(KeyCode.RightArrow))
{
GetComponent<Image>().sprite = backright;
}
else
{
GetComponent<Image>().sprite = back;
}
}
}
}
中的第一个嵌套的代码if语句(在这种情况下, ,if (Input.GetKey(KeyCode.LeftArrow)) {}
)将不起作用(图像不会改变)。我已经在if语句中添加了Debug.Log("HI")
以查看它是否有效并且是否有效。改变条件也不起作用。所有其他嵌套if语句都可以正常工作。另请注意,图像已正确放置在Unity编辑器中。
那么为什么行GetComponent<Image>().sprite = topleft;
被完全忽略,虽然if
陈述的条件都满足?
请解释为什么要嵌套从键盘读取的'if'语句。这不是一个好主意。 – Programmer
注:我没有把这个放在游戏开发堆栈交换中,因为问题完全是基于代码而不是游戏本身 –
@Programmer这是为了确保两个键目前被按住。将它们分开将使程序假定两个键中只有一个被按住,从而显示错误的图像。 –