例如有三个按钮ABC,功能分别是Debug打印"This is A"/“This is B"/"This is C"。现在需要用户按顺序点击ABC,顺序正确时实现相应按键功能,否则另外打印"Wrong",然后要求用户继续按顺序点击按钮。
例:用户先点击A,控制台打印”This is A“;然后点击C,打印”Wrong“;再点击A,打印”Wrong“;点击B,打印"This is B";最后点击C打印"This is C",完成。
public class StepCtrl : MonoBehaviour
{
public GameObject knife, workBench;
public Transform workBenchPos_des;
private int key, step;
private bool isClick;
private string button;
private void OnEnable()
{
StartCoroutine(Step());
}
private void Start()
{
button = "";
isClick = false;
}
public void onClick(string name)
{
isClick = true;
button = name;
}
private void Update()
{
key = model.getStep();
if (!model.ishandle && step > 2)
{
step = 2;
}
if (!model.isRotate && step > 4)
{
step = 4;
}
if (step == 6 && Vector3.Distance(knife.transform.position, workBenchPos_des.position) > 0.5f)
{
step = 5;
}
if (step == 8 && Vector3.Distance(knife.transform.position, workBenchPos_des.position) < 0.5f)
{
step = 7;
}
}
bool OperationCheck(int s)//
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log(s);
if (key == s)//按下的按键等于当前步骤按键
{
Debug.Log("按下正确按钮");
return true;
}
else if(key < s)
{
if (key == 5 && s == 7)
{
return true;
}
return false;
}
else
{
error.text = "<color=red>当前位置还用不到此按钮</color>";
return false;
}
}
return false;
}
IEnumerator Step()
{
Debug.Log("开始协程");
step = 1;
while (step < 9)
{
yield return new WaitForSeconds(0.1f);
yield return StartCoroutine("Step"+step);
step++;
}
notice.text = "<color=green>恭喜完成练习!!!!</color>";
Debug.Log("恭喜完成练习");
}
IEnumerator Step1()
{
Debug.Log("开始协程一");
if (button == "归零" && isClick)
{
Debug.Log("请点击【归零】按钮");
step--;
isClick = false;
}
OperationCheck(1);
yield return null;
}
IEnumerator Step2()
{
Debug.Log("开始协程二");
if (button == "手动模式" && isClick)
{
Debug.Log("请点击【手动模式】按钮");
step--;
isClick = false;
}
OperationCheck(2);
yield return null;
}
IEnumerator Step3()
{
Debug.Log("开始协程三");
Debug.Log("请点击【高速/低速】按钮");
if (button == "高速/低速" && isClick)
{
Debug.Log("key: "+key);
Debug.Log("step: "+step);
step--;
isClick = false;
}
OperationCheck(3);
yield return null;
}
IEnumerator Step4()
{
Debug.Log("开始协程四");
if (button == "轴起/轴停" && isClick)
{
Debug.Log("请点击【轴启/轴停】按钮");
step--;
isClick = false;
}
OperationCheck(4);
yield return null;
}
IEnumerator Step5()
{
Debug.Log("开始协程五");
if (Vector3.Distance(knife.transform.position, workBenchPos_des.position) > 0.5f)
{
Debug.Log("请点击【x,y,z】按钮(第五步)");
step--;
}
OperationCheck(5);
yield return null;
}
IEnumerator Step6()
{
Debug.Log("开始协程六");
if (x != 0 || y != 0 || z != 0)
{
Debug.Log("请点击【xy->0,z->0】按钮(第六步)");
step--;
}
OperationCheck(6);
yield return null;
}
IEnumerator Step7()
{
Debug.Log("开始协程七");
if (Vector3.Distance(knife.transform.position, workBenchPos_des.position) <= 0.5f)
{
Debug.Log("请点击【x,y,z】按钮(第七步)");
step--;
}
OperationCheck(7);
yield return null;
}
IEnumerator Step8()
{
Debug.Log("开始协程八");
if (key!=8)
{
step--;
}
Debug.Log("请点击【回零】按钮");
OperationCheck(8);
yield return null;
}
}
}