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游戏项目-3D迷宫(免费)
共20361个文件
meta:6328个
cs:3994个
png:1439个
需积分: 0 202 下载量 140 浏览量
2023-04-24
09:14:08
上传
评论 13
收藏 599.42MB ZIP 举报
温馨提示
Unity游戏项目_3D迷宫(免费)。包括游戏项目和打包后PC端游戏,对Unity有兴趣的小伙伴福利到了!
资源推荐
资源详情
资源评论
收起资源包目录
Unity游戏项目-3D迷宫(免费) (20361个子文件)
000449251c964c0170e8be3f595afe46 5KB
000bb3e798baa5dbaec1175ec2a03172 18KB
00119454a8a7868edbf0599b9c8d1d01 27KB
0025118eefd335204c3aff5587e8e8cc 30KB
00277e70776eb4effa0fc0e3d10a6778 19KB
00342a1f96d4b2d1af4890865196315e 11KB
004779a5e5d4b92873e9dadcc338dadc 8KB
00583dedd1063ddff971ca4e14da8f68 5KB
0060bbd4cfe5d17a9c7df3ac830eebf9 22KB
006a264b0744e53fd9d8301fa9aa0954 30KB
007850dd2a8f28284c36c043781043d7 6KB
008aa1ccf2e6d32c0afc90af59be1dd2 5KB
00ae48f8e70fe49a6ba18952f9fe558d 7KB
00b16ce4d4fb86c6c5c630fb7146fe0a 6KB
00c60626a848fae5b2ae29410ee22f98 5KB
00d5974b5138b9f6567d126ce3bca64f 6KB
011329c5448cabb594a42ebef3312405 6KB
0115d9a4221131b82c70bacc56edde29 6KB
0116694aec95b9105a850d63b97d9242 5KB
011a5eff0215f29197db7be92255544c 7KB
0132029b22ed7a26295691dceeed3f0e 7KB
0139513aa5b87a5831f1a385cc65b990 12KB
013c591fb0b89168e2065d54d828a7ee 5KB
0148abcaa66eed69a0959413cded8a1b 6KB
01495f300e5e68513172562b4fdaeb1e 4KB
014f2e8c20386b330529d3cae5d88061 5KB
01675a931bb87e3a61efbef561c651a9 16KB
016a0cab06fb693a3750e2881f45c787 9KB
016e446e2d59b9345528de60b3f587d7 7KB
0172c05421744352c858a21f8c9051d6 5KB
01767a69cad71e7c2db952c93e4206a7 4KB
0179c53a0c9645c29927ef5eaa14e923 5KB
017ba790a7e16b29d15721c85c68e647 34KB
0194c4501fb26ce6a29482e0c4ed0ac2 5KB
01967d07c36b9015ae37d30b6888bcd4 8KB
019bed391750fce5b19c6cb486705bce 5KB
01a2f508612789314cce164ec91b73d5 17KB
01cafd33f1a40837974c922ba6ad3d9d 5KB
01d3a62dc2f5ed3ffcdade7837f613c4 6KB
01dff1bcdf3822b01adc5ad0f016f5ad 9KB
01fb60c51b61c2dd09c406a3813387c7 9KB
02081fec500c755fd6f2c1f46ae3dd9f 5KB
0221f9db9290835b3df8b8e210ea735c 19KB
022201fa4bca4ef873ad08211bf6c62a 6KB
024960e3ce09249457724e94a55e1ee7 6KB
024d7d1ce72c168f251b1d2b1f82c8b5 5KB
0258167540df827c0dacacf7d41334d5 16KB
02769f455d817a81b43d11d34f213aca 27KB
0278d3c8aed94bbda16d1d099b9df79a 29KB
027d6460ec55e7acca8d8cbcb3efe15b 18KB
028391864e7655d339941f6aeaaa26f7 5KB
028968b7b7423dfd75f1a2d4ee5f240e 6KB
029e50b319fc0270fc645c51fb458c3a 5KB
029e8a33b707508911d1d204d4a5ce67 5KB
02ac94b4d9e6d9b11da8454dc81e9239 9KB
02bb8d8a611d5f973a1e7a0a5429af32 348KB
02e23c2c848671bb7d388c1c5646a67f 5KB
02fcde45d49ece71e7c50507dd4bde34 61KB
030b0ef7c0eb58b1507e5a980083e1c6 5KB
031583d0ed335a6ece3f60c29a070531 24KB
031b272ae9d106f6253242af5a9e46d3 22KB
0326ade7b97c79ac6f55760444bf9064 5KB
034e73e8106dfda45baab2f9f1476054 6KB
0363c8e477a7a7049432a9f11963b7bf 3KB
0376dad70bf9443072ab27a2f97a85fc 19KB
0390bcd4d808ebf79942a6712c4732ee 6KB
03a052cd72cbaa3c2d58f22459f117a3 6KB
03b4e653281acb026c93620b4bbf42f0 12KB
03b962a434498b9c3ecfa0f5593444e6 7KB
03c2710ac6b2d62b69db1044569faf97 7KB
03d45b6dc9a51bf8c64480eaeeccc921 4KB
03d8f3b391331bc0826737f306c79464 5KB
040b4ea4b8f455590f330c3aaeca4aa9 19KB
041141fbde25c3cafefb4d730eeb7945 4KB
0419dc02638e2aa58a80e2a5e5855589 29KB
0432ea6cf0f4536ec51a16f7d243b667 7KB
043a475677c5f1518b274144de75ad93 5KB
04480fc0aa23bbce79891d9e5b978827 16KB
044e7b933f24b196141f0092d0b42d46 6KB
04522f67400861da9dafdaa5779d440f 24KB
046b1bfc58a85d796b4f34819fb60097 286KB
04768edd29903858db2287036ce86764 27KB
047beece5cab68beda434747ab183252 4KB
04a5742f68b81e7d0af3f9e3e5c8c246 22KB
04a5fc8521844e848f41886208bb9a20 4KB
04a7c62fd7d971fc3d97c7b418cbe95f 11KB
04b024ea19ebf72368525fcb2fc7ca0c 5KB
04b4ae30a68e49e01901f24b04c4a725 22KB
04bc6d9fd82164986098585c4b0441b0 27KB
04be47758786a4efe6b38d06eb26bee6 6KB
04e507ebfa836da679bf2d636403de6a 50KB
04e7a7d912cc7d26119f40241356e16f 19KB
04fad932120dc0abc980ff7c694c00c8 10KB
050689b25bac9cb486defd35de715c6b 18KB
05187eecaf4fabdf5f11b5e63cd50aa4 6KB
052539e10686be38d899485063051b92 5KB
0527620b49478cf9315e9316a268fd09 5KB
052ce51fbbed23860f77b00d71103bb2 6KB
0530d8c1b9d482940f9416fd51c99960 19KB
053b44afb8eb59c38b3e4e09828ab8e7 8KB
共 20361 条
- 1
- 2
- 3
- 4
- 5
- 6
- 204
资源评论
@黑树叶
- 粉丝: 7
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功