问题描述
我在屏幕上使用 KeyCode 映射了 26 个字母,当我在键盘上按“B”时,字母 B 会在我的屏幕上亮起。
这就是我想要做的:
我想交换物理键的位置,所以“A”和“D”会在屏幕上交换位置。 然后,当我按下实际的键盘“D”时,我希望注册的输入为 A,A 将突出显示。 最后,逐渐增加正在交换的密钥数量。 到目前为止,我用这段代码得到了 1 和 3。
public class Shuffle : MonoBehavIoUr
public float letterMoveTime = 1f;
public List<GameObject> keys;
IEnumerator shuffleCoroutine;
Typer progressCounter;
private void Awake()
{
keys = new List<GameObject>();
keys.AddRange(GameObject.FindGameObjectsWithTag("EditorOnly"));
// each gameobject keys have this tag.
shuffleCoroutine = null;
progressCounter = GameObject.FindGameObjectWithTag("Finish").GetComponent<Typer>();
}
private void Update()
{
progressCounter = GameObject.FindGameObjectWithTag("Finish").GetComponent<Typer>();
}
public void StartShuffle() // call this on button click
{
if (shuffleCoroutine != null) return;
shuffleCoroutine = DoShuffle();
StartCoroutine(shuffleCoroutine);
}
IEnumerator DoShuffle()
{
List<Vector2> startPos = new List<Vector2>();
List<Vector2> endPos = new List<Vector2>();
//List<KeyCode> startKeyCode = new List<KeyCode>(); this does not work.
//List<KeyCode> endKeyCode = new List<KeyCode>(); this does not work.
foreach (GameObject letter in keys)
{
startPos.Add(letter.transform.position);
endPos.Add(letter.transform.position);
//startKeyCode.Add(letter.GetComponent<KeyCode>()); this does not work.
//endKeyCode.Add(letter.GetComponent<KeyCode>()); this does not work.
}
// shuffle endPos
for (int i = 0; i < Mathf.Ceil(progressCounter.progressCounter/5); i++)
{
Vector2 temp = endPos[i];
int swapIndex = Random.Range(i,endPos.Count);
endPos[i] = endPos[swapIndex];
endPos[swapIndex] = temp;
//KeyCode tempKey = endKeyCode[i];
//endKeyCode[i] = endKeyCode[swapIndex];
//endKeyCode[swapIndex] = tempKey;
}
float elapsedtime = 0f;
while (elapsedtime < letterMoveTime)
{
// wait for next frame
yield return null;
// move each letter
elapsedtime = Mathf.Min(letterMoveTime,elapsedtime + Time.deltaTime);
float t = elapsedtime / letterMoveTime;
for (int i = 0; i < startPos.Count; i++)
{
keys[i].transform.position = Vector2.Lerp(startPos[i],endPos[i],t);
//startKeyCode[i] = endKeyCode[i];
}
}
// allow shuffling to occur again
shuffleCoroutine = null;
}
有没有人有好的方法来做到这一点?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)