码迷,mamicode.com
首页 > 其他好文 > 详细

ugui用户定义操作按键

时间:2017-06-14 12:58:12      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:upper   com   div   blog   one   img   用户   frame   思路   

界面很简单,只创建了一Image,Image下边有一个Text。基本思路是点击Image,Text清空,进入修改状态,然后用户按下任意键,按下的任意键极为修改后的键

技术分享

然后下面的脚本是挂在Image下面的

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;

public class ChangeButton : MonoBehaviour, IPointerClickHandler
{

    private KeyCode defult = KeyCode.A;
    private bool willchange;
    // Use this for initialization
    void Start()
    {
        if (GetComponentInChildren<Text>().text != defult.ToString().ToUpper())
        {
            GetComponentInChildren<Text>().text = defult.ToString().ToUpper();
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(defult))
        {
            Debug.Log(defult + "被按下");
        }
    }
    void OnGUI()
    {
        GUI.Label(new Rect(10, 10, 300, 30), "当前按键为:" + defult.ToString().ToUpper());
        if (willchange)
        {
            GUI.Label(new Rect(10, 50, 300, 30), "按任意键修改按键");

            if (Input.anyKeyDown)
            {
                Event e = Event.current;
                Debug.Log(e.keyCode);
                Debug.Log(e);
                if (e.isKey)
                {
                    defult = e.keyCode;
                    GetComponentInChildren<Text>().text = defult.ToString().ToUpper();
                    willchange = false;
                }
            }
        }
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        willchange = true;
        GetComponentInChildren<Text>().text = "";
    }
}

  

ugui用户定义操作按键

标签:upper   com   div   blog   one   img   用户   frame   思路   

原文地址:http://www.cnblogs.com/lanrenqilanming/p/7007939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!