第24天Uinty输入控制、时间

输入控制

输入设备:键盘、鼠标、手柄、Vr手柄、凝视

按键输入

返回值是bool类型

每一帧持续监测按键按下:Input.GetKey(KeyCode.W)

按键按下瞬间的一帧变为true:Input.GetKeyDown(KeyCode.W)

按键抬起瞬间的一帧变为true:Input.GetKeyUp(KeyCode.W)

鼠标输入

返回值是boole类型

参数:0-左键 1-右键 2-中键

每一帧持续监测按键按下:Input.GetMouseButton(0)

按键按下瞬间的一帧变为true:Input.GetMouseButtonDown(1)

按键抬起瞬间的一帧变为true:Input.GetMouseButtonUp(2)

if(Input.GetKey(KeyCode.Q))
{
    Debug.Log("QQQQQQ");            //监听长按
}
if(Input.GetKeyDown(KeyCode.W))
{
    Debug.Log("WWWW");              //监听键盘按下
}
if(Input.GetKeyUp(KeyCode.A))
{
    Debug.Log("AAA");               //监听键盘抬起
}
if(Input.GetMouseButton(0))
{
    Debug.Log("左键");                //监听鼠标长按 0.左键 1右键 2中键
}
if(Input.GetMouseButtonUp(1))
{
    Debug.Log("右键抬起");              //监听鼠标抬起
}
if(Input.GetMouseButtonDown(2))
{
    Debug.Log("中键按下");              //监听鼠标按下
}

轴值输入

GetAxis

获取虚拟轴值

取值范围(-1 -  1)之间

水平轴值:Horizontal AD←→

垂直轴值:Vertical WS↑↓

GetAxisRow:针对水平和垂直轴值有效 直接从0-1或从0-(-1) 没有过度值

鼠标轴值

鼠标横向移动值:Mouse X

鼠标垂直移动值:Mouse Y

鼠标滚轮移动值:Mouse ScrollWheel

//GetAxis 获取虚拟轴值 
float h = Input.GetAxis("Horizontal");      //水平轴输入 取值范围:-1~1之间
float v = Input.GetAxis("Vertical");         //垂直轴值
float h2 = Input.GetAxisRaw("Horizontal");  //水平值输入,直接从0~1或者0~-1 没有过渡值
float v2 = Input.GetAxisRaw("Vertical");    //垂直轴值 
//鼠标轴值
float m1 = Input.GetAxisRaw("Mouse X");     //鼠标横向移动值
float m2 = Input.GetAxis("Mouse X");     
float y1 = Input.GetAxis("Mouse Y");        //鼠标纵向移动值
float sc = Input.GetAxis("Mouse ScrollWheel");  //鼠标滚轮值

快速定位相机到Scene视图位置

调整好角度,Ctrl+Shift+F 

OnGUI界面

1.GUI 2.NGUI:非官方插件 3.UGUI

GUI函数

专门绘制GUI的生命周期函数  private void OnGUI(){}

GUI代码必须要放在ONGUI里调用

GUI.Label 绘制文字

GUI.DrawTexture:绘制图片

GUI.Button:绘制按钮

    // Update is called once per frame
    void Update () {
       

        imgRect = new Rect(0, 0, Screen.width, Screen.height);      //铺满游戏窗口,因为窗口可以拉伸 放在Update中最好

    }
    Rect labelRect;                 //定义位置和大小参数
    Rect imgRect;                   //图片位置和大小参数
    public Texture img;             //定义图形文件 Texture
    Rect buttonRect;                //按钮位置
    GUIStyle fontStyle;
    // Use this for initialization
    void Start()
    {                                               //Rect(x,y,width,height)
        labelRect = new Rect(Screen.width / 2 - 150, 0, 100, 100);       //如图控制台坐标,左上角为(0,0),右侧为x轴正方向,下方为y轴正方向 
        buttonRect = new Rect(Screen.width / 2 - 50, Screen.height / 3 * 2, 100, 100);// Screen.width 获取屏幕最大宽度
        fontStyle = new GUIStyle();
        fontStyle.fontSize = 80;                        //设置字体大小


    }

    private void OnGUI()
    {
        GUI.DrawTexture(imgRect, img);              //GUI.DrawTexture(位置参数,图片) 设置背景图
        GUI.Label(labelRect, "坦克大战",fontStyle);           //GUI.Label(位置参数,字符串)  界面标题
        GUI.Button(buttonRect, "开始",fontStyle);             //GUI.Button(位置参数,字符串或者图片)  按钮
    }

结果:

坐标转换

屏幕坐标系

屏幕左下角为原点

向右为X正方向

向上为Y正方向

屏幕转世界

ScreenToWorldPoint:需要加个深度 

世界转屏幕

WorldToScreenPoint

if(Input.GetKeyDown(KeyCode.Space))
{
    Debug.Log(Input.mousePosition);               //获取屏幕坐标
    Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition+new Vector3(0,0,10)); //屏幕坐标转为世界坐标 需要加深度 new Vector3(0,0,10)
    Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);                     //世界坐标转为屏幕坐标
    Debug.Log(worldPos);
    Debug.Log(screenPos);
}

Time函数

Time.delaTime

每帧运行时间

受时间缩放影响

Time.fixedDeltaTime

Time.time

项目的运行时间积累

受时间缩放影响

        Debug.Log(Time.deltaTime);              //获取每帧运行时间  受时间缩放的影响
        Debug.Log(Time.fixedDeltaTime);           //获取fixed每帧运行的时间 不受时间缩放的影响
        Debug.Log(Time.time);                   //获取项目运行的时间 受时间缩放的影响

时间与移动

Time.timeScale

当前游戏的时间缩放值

加速游戏过程(>1)

暂停游戏(=0)

游戏慢镜头回放(0<值<1)

不受时间缩放影响的属性

Time.unscaledDelataTime

TIme.unscaledTime

原文地址:https://www.cnblogs.com/yifengs/p/14151208.html