Controller.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using UnityEngine.UI;
  6. public class Controller : MonoBehaviour {
  7. private bool CanMove = false; //假若没有选好运动模式之前都是禁止运动的
  8. private bool interact;
  9. private float time = 3f; //计时用变量,表示视线停留在phone图标上应该超过3秒后才能开始视选
  10. private float time2 = 0;//画布的淡入淡出效果所用时间
  11. public GameObject Player; //表示玩家
  12. public GameObject Canvas; //表示画布
  13. // Use this for initialization
  14. void Start () {
  15. }
  16. // Update is called once per frame
  17. void Update () {
  18. if (!CanMove)
  19. {
  20. if(time2 < 3)
  21. {
  22. time2 += Time.deltaTime;
  23. Canvas.GetComponent<CanvasGroup>().alpha = time2;
  24. }
  25. //进行ui交互的代码
  26. SelectUI();
  27. EyePick();
  28. if (time < 0) //视选代码
  29. {
  30. Player.GetComponent<EyePick>().enabled = true;
  31. SelectEyePick();
  32. Invoke("Stay", 5f);
  33. }
  34. }
  35. else //开始移动的时候关掉画布
  36. {
  37. time2 -= Time.deltaTime;
  38. Canvas.GetComponent<CanvasGroup>().alpha = time2;
  39. if(time2 < 0)
  40. {
  41. Canvas.SetActive(false);
  42. this.gameObject.SetActive(false);
  43. }
  44. }
  45. }
  46. void SelectUI()
  47. {
  48. String currentButton = "";
  49. var values = Enum.GetValues(typeof(KeyCode));//存储所有的按键
  50. for (int x = 0; x < values.Length; x++)
  51. {
  52. if (Input.GetKeyDown((KeyCode)values.GetValue(x))) //当获得一个键值输入的时候
  53. {
  54. currentButton = values.GetValue(x).ToString();//遍历并获取当前按下的按键
  55. if (currentButton == "Mouse0") //按下的是触屏按钮
  56. {
  57. Player.GetComponent<ScreenInput>().enabled = true;
  58. SelectTouch();
  59. Invoke("Stay", 3f);
  60. }
  61. if(currentButton == "Joystick1Button0") //按下的是蓝牙的确认键
  62. {
  63. Player.GetComponent<BluethoothInput>().enabled = true;
  64. SelectBluetooth();
  65. Invoke("Stay", 3f);
  66. }
  67. }
  68. }
  69. }
  70. void Stay()
  71. {
  72. Player.transform.FindChild("Dive_Camera").FindChild("Canvas").FindChild("Background3").gameObject.SetActive(true);
  73. CanMove = true;
  74. }
  75. void EyePick()
  76. {
  77. Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
  78. RaycastHit hit;
  79. if (Physics.Raycast(ray, out hit))
  80. {
  81. if (hit.collider.name == "Eye") //判断碰撞物体的名字,这里是表示可以被交互的物体
  82. {
  83. time -= Time.deltaTime;
  84. }
  85. else //不可以被交互的物体
  86. {
  87. time = 3f;
  88. }
  89. }
  90. else //没有碰撞体的物体
  91. {
  92. time = 3f;
  93. }
  94. }
  95. void SelectTouch() //选择触屏模式代码
  96. {
  97. Text text1 = Canvas.transform.FindChild("BackGround").FindChild("Touch").FindChild("Text").
  98. GetComponent<Text>();
  99. text1.color = new Color(118f / 256f, 255f / 256f, 147f / 256f, 256f / 256f);
  100. Text text2 = Canvas.transform.FindChild("BackGround").FindChild("TitleText").GetComponent<Text>();
  101. text2.text = String.Format("已经选择触屏模式");
  102. Image img1 = Canvas.transform.FindChild("BackGround").FindChild("Touch").
  103. GetComponent<Image>();
  104. img1.color = new Color(118f / 256f, 255f / 256f, 147f / 256f, 256f / 256f);
  105. Canvas.transform.FindChild("BackGround").FindChild("Touch").FindChild("Image").gameObject.SetActive(true);
  106. }
  107. void SelectBluetooth()
  108. {
  109. Text text1 = Canvas.transform.FindChild("BackGround").FindChild("Bluetooth").FindChild("Text").
  110. GetComponent<Text>();
  111. text1.color = new Color(118f / 256f, 255f / 256f, 147f / 256f, 256f / 256f);
  112. Image img1 = Canvas.transform.FindChild("BackGround").FindChild("Bluetooth").
  113. GetComponent<Image>();
  114. img1.color = new Color(118f / 256f, 255f / 256f, 147f / 256f, 256f / 256f);
  115. Canvas.transform.FindChild("BackGround").FindChild("Bluetooth").FindChild("Image").gameObject.SetActive(true);
  116. Text text2 = Canvas.transform.FindChild("BackGround").FindChild("TitleText").GetComponent<Text>();
  117. text2.text = String.Format("已经选择蓝牙模式");
  118. }
  119. void SelectEyePick()
  120. {
  121. Text text1 = Canvas.transform.FindChild("BackGround").FindChild("Eye").FindChild("Text").
  122. GetComponent<Text>();
  123. text1.color = new Color(118f / 256f, 255f / 256f, 147f / 256f, 256f / 256f);
  124. Image img1 = Canvas.transform.FindChild("BackGround").FindChild("Eye").
  125. GetComponent<Image>();
  126. img1.color = new Color(118f / 256f, 255f / 256f, 147f / 256f, 256f / 256f);
  127. Canvas.transform.FindChild("BackGround").FindChild("Eye").FindChild("Image").gameObject.SetActive(true);
  128. Text text2 = Canvas.transform.FindChild("BackGround").FindChild("TitleText").GetComponent<Text>();
  129. text2.text = String.Format("已经选择视选模式");
  130. }
  131. }