using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using UnityEngine.UI; public class Controller : MonoBehaviour { private bool CanMove = false; //假若没有选好运动模式之前都是禁止运动的 private bool interact; private float time = 3f; //计时用变量,表示视线停留在phone图标上应该超过3秒后才能开始视选 private float time2 = 0;//画布的淡入淡出效果所用时间 public GameObject Player; //表示玩家 public GameObject Canvas; //表示画布 // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (!CanMove) { if(time2 < 3) { time2 += Time.deltaTime; Canvas.GetComponent().alpha = time2; } //进行ui交互的代码 SelectUI(); EyePick(); if (time < 0) //视选代码 { Player.GetComponent().enabled = true; SelectEyePick(); Invoke("Stay", 5f); } } else //开始移动的时候关掉画布 { time2 -= Time.deltaTime; Canvas.GetComponent().alpha = time2; if(time2 < 0) { Canvas.SetActive(false); this.gameObject.SetActive(false); } } } void SelectUI() { String currentButton = ""; var values = Enum.GetValues(typeof(KeyCode));//存储所有的按键 for (int x = 0; x < values.Length; x++) { if (Input.GetKeyDown((KeyCode)values.GetValue(x))) //当获得一个键值输入的时候 { currentButton = values.GetValue(x).ToString();//遍历并获取当前按下的按键 if (currentButton == "Mouse0") //按下的是触屏按钮 { Player.GetComponent().enabled = true; SelectTouch(); Invoke("Stay", 3f); } if(currentButton == "Joystick1Button0") //按下的是蓝牙的确认键 { Player.GetComponent().enabled = true; SelectBluetooth(); Invoke("Stay", 3f); } } } } void Stay() { Player.transform.FindChild("Dive_Camera").FindChild("Canvas").FindChild("Background3").gameObject.SetActive(true); CanMove = true; } void EyePick() { Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0)); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { if (hit.collider.name == "Eye") //判断碰撞物体的名字,这里是表示可以被交互的物体 { time -= Time.deltaTime; } else //不可以被交互的物体 { time = 3f; } } else //没有碰撞体的物体 { time = 3f; } } void SelectTouch() //选择触屏模式代码 { Text text1 = Canvas.transform.FindChild("BackGround").FindChild("Touch").FindChild("Text"). GetComponent(); text1.color = new Color(118f / 256f, 255f / 256f, 147f / 256f, 256f / 256f); Text text2 = Canvas.transform.FindChild("BackGround").FindChild("TitleText").GetComponent(); text2.text = String.Format("已经选择触屏模式"); Image img1 = Canvas.transform.FindChild("BackGround").FindChild("Touch"). GetComponent(); img1.color = new Color(118f / 256f, 255f / 256f, 147f / 256f, 256f / 256f); Canvas.transform.FindChild("BackGround").FindChild("Touch").FindChild("Image").gameObject.SetActive(true); } void SelectBluetooth() { Text text1 = Canvas.transform.FindChild("BackGround").FindChild("Bluetooth").FindChild("Text"). GetComponent(); text1.color = new Color(118f / 256f, 255f / 256f, 147f / 256f, 256f / 256f); Image img1 = Canvas.transform.FindChild("BackGround").FindChild("Bluetooth"). GetComponent(); img1.color = new Color(118f / 256f, 255f / 256f, 147f / 256f, 256f / 256f); Canvas.transform.FindChild("BackGround").FindChild("Bluetooth").FindChild("Image").gameObject.SetActive(true); Text text2 = Canvas.transform.FindChild("BackGround").FindChild("TitleText").GetComponent(); text2.text = String.Format("已经选择蓝牙模式"); } void SelectEyePick() { Text text1 = Canvas.transform.FindChild("BackGround").FindChild("Eye").FindChild("Text"). GetComponent(); text1.color = new Color(118f / 256f, 255f / 256f, 147f / 256f, 256f / 256f); Image img1 = Canvas.transform.FindChild("BackGround").FindChild("Eye"). GetComponent(); img1.color = new Color(118f / 256f, 255f / 256f, 147f / 256f, 256f / 256f); Canvas.transform.FindChild("BackGround").FindChild("Eye").FindChild("Image").gameObject.SetActive(true); Text text2 = Canvas.transform.FindChild("BackGround").FindChild("TitleText").GetComponent(); text2.text = String.Format("已经选择视选模式"); } }