当前位置: 首页 > news >正文

Input输入和Screen相关

知识点

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

http://www.lryc.cn/news/593605.html

相关文章:

  • 轻松学习C++:基本语法解析
  • 从丢包到恢复:TCP重传机制的底层逻辑全解
  • 将HTML+JS+CSS数独游戏包装为安卓App
  • 微服务学习(六)之分布式事务
  • 华为擎云L420安装LocalSend
  • Java大视界:Java大数据在智能医疗电子健康档案数据挖掘与健康服务创新>
  • kafka--基础知识点--6.1--LEO、HW、LW
  • LeetCode Hot100【7. 整数反转】
  • 创意 C++ 文本冒险战斗游戏代码
  • Uniapp之自定义图片预览
  • 下一场范式革命:Transformer架构≠最终解法
  • Spring IOC容器在Web环境中是如何启动的(源码级剖析)?
  • Java多线程进阶
  • Node.js net.Socket.destroy()深入解析
  • [spring6: AspectMetadata AspectInstanceFactory]-源码解析
  • 零基础学习性能测试第二章-监控体系
  • OllyDbg技巧学习
  • Redis 如何保证高并发与高可用
  • Python爬虫实战:研究pefile库相关技术
  • PCB 混合介质叠层:材料特性匹配与性能提升的技术解析
  • 1. Spring AI概述
  • OSPF高级特性之Overflow
  • 【c++】提升用户体验:问答系统的交互优化实践——关于我用AI编写了一个聊天机器人……(12)
  • Buildroot vs Yocto:SDK 构建机制的核心差异与实践案例
  • 多线程 示例
  • QT窗口(8)-QFileDiag
  • esp32 sd卡
  • Kubernetes常用命令总结
  • MySQL 深度性能优化配置实战指南
  • 单例模式的设计与实现