分享個Unity 電視遙控器按鈕事件控制原始碼,方便大家不用重復造輪子,
轉載請附原文連接:https://blog.csdn.net/qq_43505432/article/details/109031521
目錄
- 一、如何消除電視上的全屏提示彈窗
- 二、遙控器按鈕事件控制原始碼
- 下面展示 `暫停雙選`,
- 下面展示 `三選`,
- 下面展示 `上下左右鍵`,
一、如何消除電視上的全屏提示彈窗
在做unity手機游戲適配成電視游戲時,出現一個問題,在電視上打開unity打包出的apk時,頂上有全屏提示彈窗,
解決方法:在開始場景的任意一個腳本的start方法中加上一句代碼,
Screen.fullScreen = false;//關閉全屏提示
二、遙控器按鈕事件控制原始碼
遙控器的確定鍵是KeyCode.JoystickButton0
電腦鍵盤的空格鍵是KeyCode.Space
public GameObject yes_1;
public GameObject no_1;
private int choose_1;
private bool sign_1;
private Color yesColor;
private Color noColor;
private Vector3 big;
private Vector3 small;
private bool sign_2;
public GameObject yes_2;
public GameObject no_2;
private int choose_2;
void Start(){
choose_1 = 2;
choose_2 = 2;
yesColor = new Color(1, 1, 1, 1);
noColor = new Color(1, 1, 1, 90 / 255f);
big = new Vector3(1.15f, 1.15f, 1.15f);
small = new Vector3(1, 1, 1);
}
public void ExitOver()
{
if (sign_2)
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
no_2.GetComponent<Image>().color = yesColor;
yes_2.GetComponent<Image>().color = noColor;
choose_2 = 1;
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
yes_2.GetComponent<Image>().color = yesColor;
no_2.GetComponent<Image>().color = noColor;
choose_2 = 2;
}
if ((Input.GetKeyUp(KeyCode.Space) || Input.GetKeyUp(KeyCode.JoystickButton0)))
{
if (choose_2 == 1)
{
Application.Quit();
}
if (choose_2 == 2)
{
player.GetComponent<SpriteRenderer>().enabled = true;
GameRestart();
}
}
}
}
下面展示 暫停雙選,
public void PauseCon()
{
if (Input.GetKeyDown(KeyCode.Escape) && (gameCanvas.activeInHierarchy || pauseCanvas.activeInHierarchy))
{
GamePause();
choose_1 = 2;
no_1.GetComponent<Image>().color = noColor;
yes_1.GetComponent<Image>().color = yesColor;
sign_1 = true;
}
if (sign_1) {
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
no_1.GetComponent<Image>().color = yesColor;
yes_1.GetComponent<Image>().color = noColor;
choose_1 = 1;
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
yes_1.GetComponent<Image>().color = yesColor;
no_1.GetComponent<Image>().color = noColor;
choose_1 = 2;
}
if ((Input.GetKeyUp(KeyCode.Space) || Input.GetKeyUp(KeyCode.JoystickButton0)))
{
if (choose_1 == 1)
{
Application.Quit();
}
if (choose_1 == 2)
{
GameResume();
}
}
}
}
下面展示 三選,
public GameObject color1;
public GameObject color2;
public GameObject color3;
private int sign;//1 2 3
private Vector3 big;
private Vector3 small;
public void TVcontroller()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
Debug.Log("left");
if (sign == 2 || sign == 1)
{
color1.GetComponent<Transform>().localScale = big;
color2.GetComponent<Transform>().localScale = small;
color3.GetComponent<Transform>().localScale = small;
sign = 1;
}
if (sign == 3)
{
color2.GetComponent<Transform>().localScale = big;
color3.GetComponent<Transform>().localScale = small;
color1.GetComponent<Transform>().localScale = small;
sign = 2;
}
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
Debug.Log("right");
if (sign == 2)
{
color3.GetComponent<Transform>().localScale = big;
color2.GetComponent<Transform>().localScale = small;
color1.GetComponent<Transform>().localScale = small;
sign = 3;
}
if (sign == 1)
{
color2.GetComponent<Transform>().localScale = big;
color1.GetComponent<Transform>().localScale = small;
color3.GetComponent<Transform>().localScale = small;
sign = 2;
}
}
if (Input.GetKeyDown(KeyCode.JoystickButton0) || Input.GetKeyDown(KeyCode.Space))
{
switch (sign)
{
case 1:
ClickColorButton(color1);
break;
case 2:
ClickColorButton(color2);
break;
case 3:
ClickColorButton(color3);
break;
}
}
下面展示 上下左右鍵,
if(!Global.pause)
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
if (transform.position.x >= 1)
{
transform.position = new Vector3(transform.position.x - 1, transform.position.y, transform.position.z);
}
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
if (transform.position.x <= 6)
{
transform.position = new Vector3(transform.position.x + 1, transform.position.y, transform.position.z);
}
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
if (transform.position.y <= 7)
{
transform.position = new Vector3(transform.position.x, transform.position.y + 1, transform.position.z);
}
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
if (transform.position.y >= 1)
{
transform.position = new Vector3(transform.position.x, transform.position.y - 1, transform.position.z);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/171629.html
標籤:其他
上一篇:【C初階】-掃雷
