触摸Unity3D中的输入
本文关键字:输入 Unity3D 触摸 | 更新日期: 2023-09-27 18:22:25
所以我在unity中做一个简单的棋盘游戏。我的过程是,当我按下一个按钮时,程序将生成一个从1-9开始的随机数,我的角色将根据得到的数字从他的初始位置更改到下一个位置。
我的流程运行良好。我的角色按照它应该的方式移动,但我的触摸输入有一个小问题。当我按下按钮时,它会生成一个数字,并在GUIText中显示一百万秒,然后当我松开按钮时,会再次变回0。我认为我的问题是,只有当我按住按钮时,结果数字才会出现在GUIText中,而当我松开按钮时,它又回到0。这不仅是问题所在,另一个问题是,当我按住按钮的时间更长时,它会不断生成和接受数字,所以它完全打乱了我的过程。我的问题是如何防止我的按钮在点击一次后被处理,以及如何在点击后在GUIText中显示结果数字。我已经在if语句上进行了实验,将其更改为不同的触摸阶段属性,但仍然无法正确执行。这是我的代码:
using UnityEngine;
using System.Collections;
public class dice_roll : MonoBehaviour {
public Texture2D roll_btn;
public Texture2D roll_btn2;
public static int num = 0;
public static string num_str = "";
public GUIText move_number;
public static int character_position = 0;
public static float y_axis = 1.18f;
public GameObject character;
public GameObject i;
public GameObject ii;
public GameObject iii;
public GameObject iv;
public GameObject v;
public GameObject vi;
public GameObject vii;
public GameObject viii;
public GameObject ix;
// Use this for initialization
void Start ()
{
guiTexture.texture = roll_btn;
}
// Update is called once per frame
void Update ()
{
move_number.text = "0";
foreach (Touch touch in Input.touches)
{
if (guiTexture.HitTest(touch.position))
{
num = Random.Range(1,10);
guiTexture.texture = roll_btn2;
num_str = num.ToString();
move_number.text = num_str;
character_position = character_position + num;
switch (character_position)
{
case 1 :
//character.transform.position = i.transform.position;
character.transform.position = new Vector3 (i.transform.position.x, y_axis, i.transform.position.z);
break;
case 2 :
character.transform.position = ii.transform.position;
character.transform.position = new Vector3 (ii.transform.position.x, y_axis, ii.transform.position.z);
break;
case 3 :
//character.transform.position = iii.transform.position;
character.transform.position = new Vector3 (iii.transform.position.x, y_axis, iii.transform.position.z);
break;
case 4 :
//character.transform.position = iv.transform.position;
character.transform.position = new Vector3 (iv.transform.position.x, y_axis, iv.transform.position.z);
break;
case 5 :
//character.transform.position = v.transform.position;
character.transform.position = new Vector3 (v.transform.position.x, y_axis, v.transform.position.z);
break;
case 6 :
//character.transform.position = vi.transform.position;
character.transform.position = new Vector3 (vi.transform.position.x, y_axis, vi.transform.position.z);
break;
case 7 :
//character.transform.position = vii.transform.position;
character.transform.position = new Vector3 (vii.transform.position.x, y_axis, vii.transform.position.z);
break;
case 8 :
//character.transform.position = vii.transform.position;
character.transform.position = new Vector3 (viii.transform.position.x, y_axis, viii.transform.position.z);
break;
case 9 :
//character.transform.position = vii.transform.position;
character.transform.position = new Vector3 (ix.transform.position.x, y_axis, ix.transform.position.z);
break;
}
}
}
}
}
我的GUI纹理roll_btn
是我正在按下的按钮。因此,我所指的问题是,当我按住按钮时,我的整数character_position
不断地将生成的数字相加。我想防止这种情况发生。
提前感谢。:D
Update()
方法每帧调用一次,您将在此处的每一帧void Update ()
{
move_number.text = "0";
}
因此,通过删除它应该可以很好地工作