【100个 Unity实用技能】

您所在的位置:网站首页 手机屏幕坐标获取插件 【100个 Unity实用技能】

【100个 Unity实用技能】

2024-05-16 15:55| 来源: 网络整理| 查看: 265

Unity 小科普

老规矩,先介绍一下 Unity 的科普小知识:

Unity是 实时3D互动内容创作和运营平台 。包括游戏开发、美术、建筑、汽车设计、影视在内的所有创作者,借助 Unity 将创意变成现实。Unity 平台提供一整套完善的软件解决方案,可用于创作、运营和变现任何实时互动的2D和3D内容,支持平台包括手机、平板电脑、PC、游戏主机、增强现实和虚拟现实设备。 也可以简单把 Unity 理解为一个游戏引擎,可以用来专业制作游戏! 🎬 博客主页:https://xiaoy.blog.csdn.net 🎥 本文由 呆呆敲代码的小Y 原创,首发于 CSDN🙉 🎄 学习专栏推荐:Unity系统学习专栏 🌲 游戏制作专栏推荐:游戏制作 🌲Unity实战100例专栏推荐:Unity 实战100例 教程 🏅 欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正! 📆 未来很长,值得我们全力奔赴更美好的生活✨ ------------------❤️分割线❤️------------------------- Unity 实用小技能学习Unity InputSystem拿到触摸屏幕的坐标,鼠标的坐标等

在Unity的新输入系统InputSystem中,获取键盘鼠标的API发生了变化,不再是之前用Input.就可以拿到了。

本文将在InputSystem中获取键盘鼠标的新API做一个简单总结整理。

键盘相关 键盘事件监听

代码语言:javascript复制void Update() { if (Keyboard.current.spaceKey.wasPressedThisFrame) { Debug.Log("空格键按下"); } if(Keyboard.current.aKey.wasReleasedThisFrame) { Debug.Log("A键抬起"); } if(Keyboard.current.spaceKey.isPressed) { Debug.Log("空格按下"); } if(Keyboard.current.anyKey.wasPressedThisFrame) { Debug.Log("任意键按下"); } }

键盘事件绑定

代码语言:javascript复制 void Start() { Keyboard.current.onTextInput += (c) => { Debug.Log("通过Lambda表达式" + c); }; Keyboard.current.onTextInput += KeyboardInput; } private void KeyboardInput(char c) { Debug.Log("监听" + c); }

鼠标相关:

鼠标坐标

代码语言:javascript复制void Update { if(Mouse.current.rightButton.wasPressedThisFrame) { Debug.Log("鼠标右键按下"); } if(Mouse.current.middleButton.wasPressedThisFrame) { Debug.Log("鼠标中建按下"); } if(Mouse.current.forwardButton.wasPressedThisFrame) { Debug.Log("鼠标前键按下"); } if(Mouse.current.backButton.wasPressedThisFrame) { Debug.Log("鼠标后键按下"); } //获取鼠标屏幕坐标(左下角为(0,0) Debug.Log(Mouse.current.position.ReadValue()); //两帧之间的偏移 Debug.Log(Mouse.current.delta.ReadValue()); //获取鼠标滚轮坐标 Debug.Log(Mouse.current.scroll.ReadValue()); }

鼠标事件绑定

代码语言:javascript复制 void InputTest() { GameInput inputAction = new GameInput();//GameInput为场景中的InputSystem控制器 inputAction.Enable(); inputAction.Gameplay.MouseDown.performed += ctx => { Debug.Log("按下:" + UnityEngine.InputSystem.Mouse.current.position.ReadValue()); }; inputAction.Gameplay.MouseDrag.performed += ctx => { Debug.Log("拖拽:" + UnityEngine.InputSystem.Mouse.current.position.ReadValue()); }; inputAction.Gameplay.MouseUp.performed += ctx => { Debug.Log("抬起:" + UnityEngine.InputSystem.Mouse.current.position.ReadValue()); }; }

触摸屏相关

代码语言:javascript复制void Update { Touchscreen ts = Touchscreen.current; if (ts == null) { return; } else { TouchControl tc = ts.touches[0]; if(tc.press.wasPressedThisFrame) { Debug.Log("按下"); } if(tc.press.wasReleasedThisFrame) { Debug.Log("抬起"); } if(tc.press.isPressed) { Debug.Log("按住"); } if(tc.tap.isPressed) { } //点击次数 Debug.Log(tc.tapCount); //点击位置 Debug.Log(tc.position.ReadValue()); //第一次接触位置 Debug.Log(tc.startPosition.ReadValue()); //得到的范围 Debug.Log(tc.radius.ReadValue()); //偏移位置 Debug.Log(tc.delta.ReadValue()); //返回TouchPhase: None,Began,Moved,Ended,Canceled,Stationary Debug.Log(tc.phase.ReadValue()); //判断状态 UnityEngine.InputSystem.TouchPhase tp = tc.phase.ReadValue(); switch (tp) { //无 case UnityEngine.InputSystem.TouchPhase.None: break; //开始接触 case UnityEngine.InputSystem.TouchPhase.Began: break; //移动 case UnityEngine.InputSystem.TouchPhase.Moved: break; //结束 case UnityEngine.InputSystem.TouchPhase.Ended: break; //取消 case UnityEngine.InputSystem.TouchPhase.Canceled: break; //静止 case UnityEngine.InputSystem.TouchPhase.Stationary: break; } }

手柄相关

代码语言:javascript复制Gamepad handle = Gamepad.current; if(handle==null) { return; } Vector2 leftDir= handle.leftStick.ReadValue();//左手柄坐标 Vector2 rightDir= handle.rightStick.ReadValue();//右手柄坐标 //左摇杆按下抬起 if(Gamepad.current.leftStickButton.wasPressedThisFrame) { } if (Gamepad.current.leftStickButton.wasReleasedThisFrame) { } if (Gamepad.current.leftStickButton.isPressed) { } //右摇杆按下抬起 if (Gamepad.current.rightStickButton.wasPressedThisFrame) { } if (Gamepad.current.rightStickButton.wasReleasedThisFrame) { } if (Gamepad.current.rightStickButton.isPressed) { } if(Gamepad.current.dpad.left.wasPressedThisFrame) { } if (Gamepad.current.dpad.left.wasReleasedThisFrame) { } if (Gamepad.current.dpad.left.isPressed) { } //右侧三角方块/XYAB按键 //Gamepad.current.buttonEast; //Gamepad.current.buttonWest; //Gamepad.current.buttonSouth; //Gamepad.current.buttonEast; if (Gamepad.current.buttonNorth.wasPressedThisFrame) { } if (Gamepad.current.buttonNorth.wasReleasedThisFrame) { } if (Gamepad.current.buttonNorth.isPressed) { } //手柄中央键 if(Gamepad.current.startButton.wasPressedThisFrame) { } if(Gamepad.current.selectButton.wasPressedThisFrame) { } //肩键 if(Gamepad.current.leftShoulder.wasPressedThisFrame) { } if (Gamepad.current.rightShoulder.wasPressedThisFrame) { } if(Gamepad.current.leftTrigger.wasPressedThisFrame) { } if(Gamepad.current.rightTrigger.wasPressedThisFrame) { }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3