Unity源码分享之 电视遥控器按钮事件控制

38 篇文章 2 订阅

分享个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);
                }
            }
        }
  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小程小程,永不消沉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值