|
@@ -5,25 +5,46 @@ using System;
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
public class Controller : MonoBehaviour {
|
|
|
+
|
|
|
+ public enum InputType
|
|
|
+ {
|
|
|
+ None,
|
|
|
+ Eye,
|
|
|
+ Click,
|
|
|
+ Joystick,
|
|
|
+ }
|
|
|
+
|
|
|
private bool CanMove = false; //假若没有选好运动模式之前都是禁止运动的
|
|
|
private bool interact;
|
|
|
+ private InputType inputType;
|
|
|
|
|
|
|
|
|
private float time = 3f; //计时用变量,表示视线停留在phone图标上应该超过3秒后才能开始视选
|
|
|
private float time2 = 0;//画布的淡入淡出效果所用时间
|
|
|
public GameObject Player; //表示玩家
|
|
|
public GameObject Canvas; //表示画布
|
|
|
-
|
|
|
+ public Transform arrowTrans;
|
|
|
+ public RectTransform selectUI;
|
|
|
+ public GameObject alertPanel;
|
|
|
|
|
|
|
|
|
// Use this for initialization
|
|
|
void Start () {
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+ alertPanel.SetActive (false);
|
|
|
+ selectUI.gameObject.SetActive (true);
|
|
|
}
|
|
|
|
|
|
// Update is called once per frame
|
|
|
void Update () {
|
|
|
+
|
|
|
+ if (alertPanel.gameObject.activeSelf)
|
|
|
+ {
|
|
|
+ CheckArrow ();
|
|
|
+ CheckAlert ();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
if (!CanMove)
|
|
|
{
|
|
|
if(time2 < 3)
|
|
@@ -31,6 +52,7 @@ public class Controller : MonoBehaviour {
|
|
|
time2 += Time.deltaTime;
|
|
|
Canvas.GetComponent<CanvasGroup>().alpha = time2;
|
|
|
}
|
|
|
+ CheckArrow ();
|
|
|
//进行ui交互的代码
|
|
|
SelectUI();
|
|
|
EyePick();
|
|
@@ -47,15 +69,83 @@ public class Controller : MonoBehaviour {
|
|
|
Canvas.GetComponent<CanvasGroup>().alpha = time2;
|
|
|
if(time2 < 0)
|
|
|
{
|
|
|
- Canvas.SetActive(false);
|
|
|
+ Canvas.GetComponent<CanvasGroup>().alpha = 1f;
|
|
|
this.gameObject.SetActive(false);
|
|
|
+ arrowTrans.gameObject.SetActive (false);
|
|
|
+ selectUI.gameObject.SetActive (false);
|
|
|
+ alertPanel.gameObject.SetActive (false);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void CheckAlert()
|
|
|
+ {
|
|
|
+ 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.tag == "alertyes") //判断碰撞物体的tag,这里是表示可以被交互的物体
|
|
|
+ {
|
|
|
+ switch (inputType)
|
|
|
+ {
|
|
|
+ case InputType.Click:
|
|
|
+ if (Input.GetKeyDown (KeyCode.Mouse0))
|
|
|
+ {
|
|
|
+ alertPanel.SetActive (false);
|
|
|
+ selectUI.gameObject.SetActive (true);
|
|
|
+ Player.GetComponent<ScreenInput> ().enabled = true;
|
|
|
+ SelectTouch ();
|
|
|
+ Invoke ("Stay", 3f);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if(hit.collider.tag == "alertno")
|
|
|
+ {
|
|
|
+ switch (inputType)
|
|
|
+ {
|
|
|
+ case InputType.Click:
|
|
|
+ if (Input.GetKeyDown (KeyCode.Mouse0))
|
|
|
+ {
|
|
|
+ alertPanel.SetActive (false);
|
|
|
+ selectUI.gameObject.SetActive (true);
|
|
|
+ inputType = InputType.None;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void CheckArrow()
|
|
|
+ {
|
|
|
+ Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint (Camera.main, selectUI.position);
|
|
|
+ float x = screenPoint.x - Screen.width / 2f;
|
|
|
+ float y = screenPoint.y - Screen.height / 2f;
|
|
|
+
|
|
|
+ float dis = Mathf.Sqrt (x * x + y * y);
|
|
|
+ if (dis > 500)
|
|
|
+ {
|
|
|
+ arrowTrans.gameObject.SetActive (true);
|
|
|
+ float theta = Mathf.Atan2 (y, x);
|
|
|
+ float angle = Mathf.Rad2Deg * theta;
|
|
|
+ arrowTrans.localEulerAngles = new Vector3 (0, 0, angle);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ arrowTrans.gameObject.SetActive (false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
void SelectUI()
|
|
|
{
|
|
|
+ if (inputType == InputType.Click)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
String currentButton = "";
|
|
|
var values = Enum.GetValues(typeof(KeyCode));//存储所有的按键
|
|
|
for (int x = 0; x < values.Length; x++)
|
|
@@ -65,15 +155,15 @@ public class Controller : MonoBehaviour {
|
|
|
currentButton = values.GetValue(x).ToString();//遍历并获取当前按下的按键
|
|
|
if (currentButton == "Mouse0") //按下的是触屏按钮
|
|
|
{
|
|
|
- Player.GetComponent<ScreenInput>().enabled = true;
|
|
|
- SelectTouch();
|
|
|
- Invoke("Stay", 3f);
|
|
|
+ inputType = InputType.Click;
|
|
|
+ selectUI.gameObject.SetActive (false);
|
|
|
+ alertPanel.gameObject.SetActive (true);
|
|
|
}
|
|
|
if(currentButton == "Joystick1Button0") //按下的是蓝牙的确认键
|
|
|
{
|
|
|
- Player.GetComponent<BluethoothInput>().enabled = true;
|
|
|
- SelectBluetooth();
|
|
|
- Invoke("Stay", 3f);
|
|
|
+ Player.GetComponent<BluethoothInput> ().enabled = true;
|
|
|
+ SelectBluetooth ();
|
|
|
+ Invoke ("Stay", 3f);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -82,7 +172,7 @@ public class Controller : MonoBehaviour {
|
|
|
void Stay()
|
|
|
{
|
|
|
|
|
|
- Player.transform.FindChild("Dive_Camera").FindChild("Canvas").FindChild("Background3").gameObject.SetActive(true);
|
|
|
+ Player.transform.Find("Dive_Camera").Find("Canvas").Find("Background3").gameObject.SetActive(true);
|
|
|
CanMove = true;
|
|
|
}
|
|
|
|
|
@@ -111,46 +201,46 @@ public class Controller : MonoBehaviour {
|
|
|
|
|
|
void SelectTouch() //选择触屏模式代码
|
|
|
{
|
|
|
- Text text1 = Canvas.transform.FindChild("BackGround").FindChild("Touch").FindChild("Text").
|
|
|
+ Text text1 = Canvas.transform.Find("BackGround").Find("Touch").Find("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>();
|
|
|
+ Text text2 = Canvas.transform.Find("BackGround").Find("TitleText").GetComponent<Text>();
|
|
|
text2.text = String.Format("已经选择触屏模式");
|
|
|
- Image img1 = Canvas.transform.FindChild("BackGround").FindChild("Touch").
|
|
|
+ Image img1 = Canvas.transform.Find("BackGround").Find("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);
|
|
|
+ Canvas.transform.Find("BackGround").Find("Touch").Find("Image").gameObject.SetActive(true);
|
|
|
}
|
|
|
|
|
|
void SelectBluetooth()
|
|
|
{
|
|
|
- Text text1 = Canvas.transform.FindChild("BackGround").FindChild("Bluetooth").FindChild("Text").
|
|
|
+ Text text1 = Canvas.transform.Find("BackGround").Find("Bluetooth").Find("Text").
|
|
|
GetComponent<Text>();
|
|
|
text1.color = new Color(118f / 256f, 255f / 256f, 147f / 256f, 256f / 256f);
|
|
|
|
|
|
- Image img1 = Canvas.transform.FindChild("BackGround").FindChild("Bluetooth").
|
|
|
+ Image img1 = Canvas.transform.Find("BackGround").Find("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);
|
|
|
+ Canvas.transform.Find("BackGround").Find("Bluetooth").Find("Image").gameObject.SetActive(true);
|
|
|
|
|
|
- Text text2 = Canvas.transform.FindChild("BackGround").FindChild("TitleText").GetComponent<Text>();
|
|
|
+ Text text2 = Canvas.transform.Find("BackGround").Find("TitleText").GetComponent<Text>();
|
|
|
text2.text = String.Format("已经选择蓝牙模式");
|
|
|
}
|
|
|
|
|
|
void SelectEyePick()
|
|
|
{
|
|
|
- Text text1 = Canvas.transform.FindChild("BackGround").FindChild("Eye").FindChild("Text").
|
|
|
+ Text text1 = Canvas.transform.Find("BackGround").Find("Eye").Find("Text").
|
|
|
GetComponent<Text>();
|
|
|
text1.color = new Color(118f / 256f, 255f / 256f, 147f / 256f, 256f / 256f);
|
|
|
|
|
|
- Image img1 = Canvas.transform.FindChild("BackGround").FindChild("Eye").
|
|
|
+ Image img1 = Canvas.transform.Find("BackGround").Find("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);
|
|
|
+ Canvas.transform.Find("BackGround").Find("Eye").Find("Image").gameObject.SetActive(true);
|
|
|
|
|
|
- Text text2 = Canvas.transform.FindChild("BackGround").FindChild("TitleText").GetComponent<Text>();
|
|
|
+ Text text2 = Canvas.transform.Find("BackGround").Find("TitleText").GetComponent<Text>();
|
|
|
text2.text = String.Format("已经选择视选模式");
|
|
|
}
|
|
|
|