Unity原始碼分享之 電視遙控器按鈕事件控制

2020-10-13 13:00:32

分享個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);
                }
            }
        }