知識點
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"));}}
}