This sample shows how to set up a simple character controller using the input system. As there is more than one way to do it, the sample illustrates several ways. Each demonstration is set up as a separate scene. The basic functionality in all the scenes is the same. You can move and look around and fire projectiles (colored cubes) into the scene. In some scenes, only gamepads are supported but the more involved demonstrations support several different inputs concurrently.
# SimpleDemo_UsingState
[Source](./SimpleController_UsingState.cs)
This starts off at the lowest level by demonstrating how to wire up input by polling input state directly in a `MonoBehaviour.Update` function. For simplicity's sake it only deals with gamepads but the same mechanism works in equivalent ways for other types of input devices (e.g. using `Mouse.current` and `Keyboard.current`).
The key APIs demonstrated here are `Gamepad.current` and `InputControl.ReadValue`.
```CSharp
public class SimpleController_UsingState : MonoBehaviour
{
//...
public void Update()
{
var gamepad = Gamepad.current;
if (gamepad == null)
return;
var move = Gamepad.leftStick.ReadValue();
//...
}
}
```
# SimpleDemo_UsingActions
[Source](./SimpleController_UsingActions.cs)
This moves one level higher and moves input over to "input actions". These are input abstractions that allow you to bind to input sources indirectly.
In this scene, the actions are embedded directly into the character controller component. This allows setting up the bindings for the actions directly in the inspector. To see the actions and their bindings, select the `Player` object in the hierarchy and look at the `SimpleController_UsingActions` component in the inspector.
The key APIs demonstrated here are `InputAction` and its `Enable`/`Disable` methods and its `ReadValue` method.
```CSharp
public class SimpleController_UsingActions : MonoBehaviour
{
public InputAction moveAction;
//...
public void OnEnable()
{
moveAction.Enable();
//...
}
public void OnDisable()
{
moveAction.Disable();
//...
}
public void Update()
{
var move = moveAction.ReadValue<Vector2>();
//...
}
}
```
The sample also demonstrates how to use a `Tap` and a `SlowTap` interaction on the fire action to implement a charged shooting mechanism. Note that in this case, we run the firing logic right from within the action using the action's `started`, `performed`, and `canceled` callbacks.
```CSharp
fireAction.performed +=
ctx =>
{
if (ctx.interaction is SlowTapInteraction)
{
StartCoroutine(BurstFire((int)(ctx.duration * burstSpeed)));
}
else
{
Fire();
}
m_Charging = false;
};
fireAction.started +=
ctx =>
{
if (ctx.interaction is SlowTapInteraction)
m_Charging = true;
};
fireAction.canceled +=
ctx =>
{
m_Charging = false;
};
```
# SimpleDemo_UsingActionAsset
[Source](./SimpleController_UsingActionAsset.cs)
As more and more actions are added, it can become quite tedious to manually set up and `Enable` and `Disable` all the actions. We could use an `InputActionMap` in the component like so
```CSharp
public class SimpleController : MonoBehaviour
{
public InputActionMap actions;
public void OnEnable()
{
actions.Enable();
}
public void OnDisable()
{
actions.Disable();
}
}
```
but then we would have to look up all the actions manually in the action map. A simpler approach is to put all our actions in a separate asset and generate a C# wrapper class that automatically performs the lookup for us.
To create such an `.inputactions` asset, right-click in the Project Browser and click `Create >> Input Actions`. To edit the actions, double-click the `.inputactions` asset and a separate window will come up. The asset we use in this example is [SimpleControls.inputactions](SimpleControls.inputactions).
When you select the asset, note that `Generate C# Class` is ticked in the import settings. This triggers the generation of [SimpleControls.cs](SimpleControls.cs) based on the `.inputactions` file.
Regarding the `SimpleController_UsingActionAsset` script, there are some notable differences.
```CSharp
public class SimpleController_UsingActionAsset
{
// This replaces the InputAction instances we had before with
// the generated C# class.
private SimpleControls m_Controls;
//...
public void Awake()
{
// To use the controls, we need to instantiate them.
// This can be done arbitrary many times. E.g. there
// can be multiple players each with its own SimpleControls
// instance.
m_Controls = new SimpleControls();
// The generated C# class exposes all the action map
// and actions in the asset by name. Here, we reference
// the `fire` action in the `gameplay` action map, for
// example.
m_Controls.gameplay.fire.performed +=
//...
}
//...
public void Update()
{
// Same here, we can just look the actions up by name.
var look = m_Controls.gameplay.look.ReadValue<Vector2>();
var move = m_Controls.gameplay.move.ReadValue<Vector2>();
//...
}
}
```
Just for kicks, this sample also adds keyboard and mouse control to the game.
# SimpleDemo_UsingPlayerInput
[Source](./SimpleController_UsingPlayerInput.cs)
Finally, we reached the highest level of the input system. While scripting input like in the examples above can be quick and easy, it becomes hard to manage when there can be multiple devices and/or multiple players in the game. This is where `PlayerInput` comes in.
`PlayerInput` automatically manages per-player device assignments and can also automatically handle control scheme switching in single player (e.g. when the player switches between a gamepad and mouse&keyboard).
In our case, we're not getting too much out of it since we don't have control schemes or multiple players but still, let's have a look.
The first thing you'll probably notice is that now there are two script components on the `Player` object, one being the usual `SimpleController` and the other being `PlayerInput`. The latter is what now refers to [SimpleControls.inputactions](SimpleControls.inputactions). It also has `gameplay` set as the `Default Action Map` so that the gameplay actions will get enabled right away when `PlayerInput` itself is enabled.
For getting callbacks, we have chosen `Invoke Unity Events` as the `Behavior`. If you expand the `Events` foldout in the inspector, you can see that `OnFire`, `OnMove`, and `OnLook` are added to the respective events. Each callback method here looks like the `started`, `performed`, and `canceled` callbacks we've already seen on `fireAction` before.
```CSharp
public class SimpleController_UsingPlayerInput : MonoBehaviour
{
private Vector2 m_Move;
//...
public void OnMove(InputAction.CallbackContext context)
{
m_Move = context.ReadValue<Vector2>();
}
//...
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
打开即用的结合Unity和WebRTC实现的远程投屏项目。 这是一个示例项目,场景testScene中包含了远程投屏最基本的组件,是学习Unity WebRTC远程投屏的极简案例。 引入了WebRTC相关的Sample,便于进一步学习。 使用方法: 1. Porject Settings中设定Web Server的通信方式和端口号 2. 在系统视图中用Command Line启动Web Server,后面加-p可以改端口。 3.运行项目后在浏览器输入自己设定好的Host IP+Port,就可以看到示例页面,点击示例页面中的Receive就可以从浏览器看到Unity窗口的画面。 如果想要更详细的指导关注我后搜索如下博文: 【unity】【WebRTC】从0开始创建一个Unity远程媒体流app-构建可同步场景
资源推荐
资源详情
资源评论
收起资源包目录
unity-远程画面-远程传输-媒体流-stream-信令服务-webRTC-项目源码-打开即用 (2000个子文件)
000449251c964c0170e8be3f595afe46 5KB
000b1ec92ff7ecff15d61272dd07235b 6KB
00227f0dc95451ee05d6884ca4be91b0 19KB
00355a9276721decd5c51ffe1ed37f9e 12KB
003bd8aba89d85106ad0fb02e2d8a20e 19KB
004779a5e5d4b92873e9dadcc338dadc 8KB
00664e3696e30172a18a5c35a2d87a44 32KB
006819f510eec5a6a84bcd1e3ba8a2e1 23KB
007850dd2a8f28284c36c043781043d7 6KB
007a9b9920982d72bf335586717599b5 19KB
008946e3bef96263a8c058ceb3ddb675 11KB
008aa1ccf2e6d32c0afc90af59be1dd2 5KB
009dc2b1927aae7c980ab7de6f3abc8f 6KB
00b8bdab23fba82ed2dcf0dae9bf5802 5KB
00c60626a848fae5b2ae29410ee22f98 5KB
00d5974b5138b9f6567d126ce3bca64f 6KB
00ef9a1916194b67e1acfa84e6213887 8KB
010e1314e47224b0679f61c44f2986d5 5KB
011329c5448cabb594a42ebef3312405 6KB
0115d9a4221131b82c70bacc56edde29 6KB
0116694aec95b9105a850d63b97d9242 5KB
0134ede379623ba705204605d912e702 22KB
0139513aa5b87a5831f1a385cc65b990 12KB
013c591fb0b89168e2065d54d828a7ee 5KB
01425e92f1b16009d315d1d3003690cc 22KB
0148abcaa66eed69a0959413cded8a1b 6KB
01495f300e5e68513172562b4fdaeb1e 4KB
014b2c59132d35c5eb93698dc85acc42 19KB
014f2e8c20386b330529d3cae5d88061 5KB
015e911de5e2856f444b42f6e2e7426a 19KB
016e446e2d59b9345528de60b3f587d7 7KB
0172c05421744352c858a21f8c9051d6 5KB
01767a69cad71e7c2db952c93e4206a7 4KB
0179c53a0c9645c29927ef5eaa14e923 5KB
0194c4501fb26ce6a29482e0c4ed0ac2 5KB
01967d07c36b9015ae37d30b6888bcd4 8KB
01a2f508612789314cce164ec91b73d5 17KB
01cafd33f1a40837974c922ba6ad3d9d 5KB
01d3a62dc2f5ed3ffcdade7837f613c4 6KB
01fb60c51b61c2dd09c406a3813387c7 9KB
03065c71ec6c4d33d4fdeb87d4c9df9a 7KB
031583d0ed335a6ece3f60c29a070531 24KB
0326ade7b97c79ac6f55760444bf9064 5KB
0338d3aaf4ef0ea37458c32199f71970 21KB
0347901a0ee9ae893ae5dc8bb53dbce6 7KB
034e73e8106dfda45baab2f9f1476054 6KB
0355b2817a19c2ae4559e62c4ac22d6d 11KB
0359ee4bc06bfe4663e863d86a8b36f5 5KB
03873a1b50e654995f0a60c59cc6ef08 9KB
0390bcd4d808ebf79942a6712c4732ee 6KB
03a052cd72cbaa3c2d58f22459f117a3 6KB
03b962a434498b9c3ecfa0f5593444e6 7KB
03d45b6dc9a51bf8c64480eaeeccc921 4KB
03d8f3b391331bc0826737f306c79464 5KB
07222ab9d7b7b0a05826d26a93f884b5 8KB
072fffc59c676ebe9b245a0d1cb678a2 7KB
073f19741f668375c26fb474c88779b5 9KB
0742b7e9ba0f35a75e475f5c3f6f7e3d 18KB
07434285e7b8b29e894197db2e15808c 6KB
07436195edba11ef03d35121a8aabb25 10KB
074afb99f3e927123e83b4fb0c08577a 6KB
074f3474dfd36908a2afa64f13585e48 13KB
0751e550a594b007b64bd58b27e40977 5KB
07835db43fecf156221379f27e47ee9e 9KB
0784f8e600f0a796cc041346e0876198 10KB
078bb7939e27e1537b225f433f6eabb3 22KB
0791b07760296babfc6f91ac6166c94f 19KB
079fb1531c4830dc9b2726a50c96f64c 19KB
07b30d502e1a8a5747bb7244ade5504e 8KB
07d02c0c7cb0ed8a306b5415250b59d2 34KB
07dee35a27fd882259de89173eb2ff12 6KB
07e2ba935dcae47b6f2bd1b61f726691 20KB
07e886b84c895bd588515c222bf4d865 7KB
07f79d7f95623cc6a7db4bcf598da4f6 4KB
09045d5f3c0ba6eb95483c3d1efd8379 6KB
09107154da71451fdc0a4f6b659e2f67 4KB
091256baad466550135839ad70770e84 5KB
09241100301b487a1a47dbc1af3e6aba 6KB
092cb4126e3d784fc5f8676c0dbaeb04 22KB
092f38aaf1b18b109d1d1dbae07b0e00 7KB
09303d8d2c8bb911420493d6550d7649 6KB
0935933780408d82367ad441c14b5f81 10KB
0945fb8b081057e508788c5daed16fcc 5KB
095395bc2c8ba87db594a522806696b9 10KB
0972f1d294b3116e0a1afa391e124967 6KB
0977813302c9d01d4de0d3ee36e9c4ee 6KB
0986c7cf7dbaa7b6af409adcd47c92ce 18KB
09896f935a0ddd8fa57de754d8099666 6KB
0995ca05b71c76afec88a75a7de43ebf 5KB
09cce7cf9c9baae8f0470b011a528011 41KB
09d47e9a9f6ee7a0e68f4e68c08f7e56 10KB
09e7ebef8aae9ed0bdeb1c1d3d164437 10KB
09ea9fe48cde54cc94fac13cde1c827d 7KB
09fcd630bb753bbe83dbf8907f7f28c9 6KB
0b0ad5cae72f110cdda85454a7789363 5KB
0b0bac9a0e3c8499ffa7e69cef5fbe7f 6KB
0b14d02007ecb471b3cddac1655089e5 8KB
0b4d6d9a6b92553f8b9c21f9a9857447 8KB
0b9bc4e9ee28f764da9ef6be68a27a0a 11KB
0b9e5fa770af7de7330690598d73e584 7KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
- 2301_768884392024-02-07资源很好用,有较大的参考价值,资源不错,支持一下。
每日出拳老爷子
- 粉丝: 1792
- 资源: 705
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 课设毕设基于SSM的农产品供销服务系统-LW+PPT+源码可运行.zip
- 课设毕设基于SSM的高校四六级报名管理系统-LW+PPT+源码可运行.zip
- 课设毕设基于SSM的高校二手交易平台-LW+PPT+源码可运行.zip
- 课设毕设基于SSM的毕业生就业信息管理系统-LW+PPT+源码可运行
- ZodGame.DLL
- 编译原理是什么以及学习了解编译原理的意义
- 0dc10bd6f58239f070aa65979b792057.cpp
- 什么是协同过滤算法以及学习了解协同过滤算法的意义是什么
- 什么是汉诺塔python以及学习汉诺塔python的意义
- 1plusx_1_proj_test_231125_答案.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功