123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- 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<CanvasGroup>().alpha = time2;
- }
- //进行ui交互的代码
- SelectUI();
- EyePick();
- if (time < 0) //视选代码
- {
- Player.GetComponent<EyePick>().enabled = true;
- SelectEyePick();
- Invoke("Stay", 5f);
- }
- }
- else //开始移动的时候关掉画布
- {
- time2 -= Time.deltaTime;
- Canvas.GetComponent<CanvasGroup>().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<ScreenInput>().enabled = true;
- SelectTouch();
- Invoke("Stay", 3f);
- }
- if(currentButton == "Joystick1Button0") //按下的是蓝牙的确认键
- {
- Player.GetComponent<BluethoothInput>().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<Text>();
- text1.color = new Color(118f / 256f, 255f / 256f, 147f / 256f, 256f / 256f);
- Text text2 = Canvas.transform.FindChild("BackGround").FindChild("TitleText").GetComponent<Text>();
- text2.text = String.Format("已经选择触屏模式");
- Image img1 = Canvas.transform.FindChild("BackGround").FindChild("Touch").
- GetComponent<Image>();
- 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<Text>();
- text1.color = new Color(118f / 256f, 255f / 256f, 147f / 256f, 256f / 256f);
- Image img1 = Canvas.transform.FindChild("BackGround").FindChild("Bluetooth").
- GetComponent<Image>();
- 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<Text>();
- text2.text = String.Format("已经选择蓝牙模式");
- }
- void SelectEyePick()
- {
- Text text1 = Canvas.transform.FindChild("BackGround").FindChild("Eye").FindChild("Text").
- GetComponent<Text>();
- text1.color = new Color(118f / 256f, 255f / 256f, 147f / 256f, 256f / 256f);
- Image img1 = Canvas.transform.FindChild("BackGround").FindChild("Eye").
- GetComponent<Image>();
- 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<Text>();
- text2.text = String.Format("已经选择视选模式");
- }
- }
|