知识点
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Lesson11 : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){#region 注意,输入内容一定写在Update中#endregion#region 知识点一 鼠标在屏幕的位置//print(Input.mousePosition);//鼠标原点在屏幕的左下角,且x向右,z轴为0#endregion#region 知识点二 检测鼠标输入//0是左键1是右键2是中键if(Input.GetMouseButtonDown (0)){print("鼠标左键按下了");}if((Input .GetMouseButtonUp (0))){print("鼠标左键抬起了");}if(Input .GetMouseButton (1)){print("右键按下了");}//获取鼠标滚轮值//返回值是一个vector2向量,鼠标中建滚懂,会改变 Y值//-1是往下滚,1是往上滚print(Input.mouseScrollDelta);#endregion#region 知识点三 键盘检测//键盘按下if(Input.GetKeyDown(KeyCode.W)){print("W键按下");}//键盘抬起Input.GetKeyUp(KeyCode.W);//键盘长安Input.GetKey (KeyCode.W);#endregion#region 知识点四 检测默认轴输入//我们学习鼠标 键盘输入 主要是用来//控制玩家 比如 旋转 位移等等//所以Unity提供了 更方便的方法 来帮助我们控制 对象的 位移和旋转//键盘AD按下时 返回 -1到1之间的变换//相当于 得到得这个值 就是我们的 左右方向 我们可以通过它来控制 对象左右移动 或者左右旋转//print(Input.GetAxis("Horizontal"));//键盘sw按下时 返回 -1到1之间的变换//得到得这个值 就是我们的 上下方向 我们可以通过它来控制 对象上下移动 或者上下旋转print(Input.GetAxis("Vertical"));//鼠标横向移动时 -1 到 1 左 右print(Input.GetAxis("Mouse X"));//鼠标竖向移动时 -1 到 1 下 上print(Input.GetAxis("Mouse Y"));//GetAxisRaw方法 和 GetAxis使用方式相同//只不过 它的返回值 只会是 -1 0 1 不会有中间值#endregion}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Lesson12 : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){#region 知识点一 静态属性#region 常用//当前屏幕分辨率Resolution r= Screen.currentResolution;print("当前屏幕分辨率的宽" + r.width + "高" + r.height);//屏幕窗口当前宽高print(Screen.width);print(Screen.height);//这得到的是当前窗口的宽高 不是设备分辨率的宽高//一般写代码 要 用窗口宽高 做计算是就用他们//屏幕休眠模式Screen.sleepTimeout = SleepTimeout.NeverSleep;#endregion#region 不常用//运行时是否全屏模式Screen.fullScreen = true;//窗口模式//独占全屏FullScreenMode.ExclusiveFullScreen//全屏窗口FullScreenMode.FullScreenWindow//最大化窗口FullScreenMode.MaximizedWindow//窗口模式FullScreenMode.WindowedScreen.fullScreenMode = FullScreenMode.Windowed;//移动设备屏幕转向相关//允许自动旋转为左横向 Home键在左//允许自动旋转为右横向 Home键在右//允许自动旋转到纵向 Home键在下#endregion#region 知识点二 静态方法//设置分辨率 一般移动设备不使用Screen.SetResolution(1920, 1080, true);#endregion #endregion}// Update is called once per framevoid Update(){}
}
练习题
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class TankMove : MonoBehaviour
{public int moveSpeed = 10;public int rotateSpeed = 50;public int headRetateSpeed = 50;public int cremaRetateSpeed = 30;public Transform head;public Transform turret;public Transform crema;// Start is called before the first frame updatevoid Start(){//写一个方法,控制坦克的移动,炮台的转向}// Update is called once per framevoid Update(){Move();}public void Move(){this.transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime * Input.GetAxis("Vertical"),Space.World );this.transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime * Input.GetAxis("Horizontal"));//坦克头的转向head.Rotate(Vector3.up * headRetateSpeed * Time.deltaTime * Input.GetAxis("Mouse X"));//鼠标中键控制坦克炮管抬起放下turret.Rotate(Vector3.forward * headRetateSpeed * Time.deltaTime * Input.mouseScrollDelta.y );//控制摄像头看向坦克,按下鼠标左键移动鼠标可以观测坦克if(Input .GetMouseButton (1)){crema.RotateAround(this.transform.position, Vector3.up, cremaRetateSpeed * Time.deltaTime*Input .GetAxis ("Mouse X"));}}
}