- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想检测水平滑动,以便用户可以向左或向右滑动以切换一堆游戏角色。向左滑动 1 次切换/显示上一个字符,向右滑动 1 次切换/显示下一个字符。
Input.GetAxis("Horizontal")
会根据您是向左还是向右滑动,给出一个介于 -1 和 1 之间的范围。
如果我将 Debug.Log(Input.GetAxis("Horizontal"))
添加到 void update()
它会在每次滑动时多次打印出轴范围/按键。
如何获取单个(第一个)值 Input.GetAxis("Horizontal")
返回值?
我想做这样的事情:
void Update() {
float swipe = Input.GetAxis("Horizontal");
if (swipe > 0) {
ToggleRight ();
} else if (swipe < 0) {
ToggleLeft();
}
}
这是否可能或是否有实现水平滑动的方法?
最佳答案
您遇到的问题是,在进行滑动时,您的函数在每个游戏周期都会被调用,您的函数最终会被调用多次。基本上,您希望有一种方法来过滤更新事件流并仅选择相关事件。
一种方法是使用 Reactive Extensions。您可以下载响应式扩展的统一兼容版本 here .
完成此操作后,您将能够将统一输入转换为可观察流。以下是您可以如何做到这一点:
using UnityEngine;
// You'll need to include these namespaces.
using UniRx;
using System.Linq;
using System;
// This will be responsible for converting unity input
// into a stream of events.
public class SwipeListener : MonoBehaviour
{
// Rx Subjects allow you to create an observable stream of your
// own. You can push events to the stream by calling OnNext().
private Subject<float> _subject;
// We'll expose the above stream via this property for a couple reasons.
// 1) If we exposed the subject directly, other code could push events to
// our stream by calling OnNext(). Preventing that helps avoid bugs.
// 2) We'll be filtering the stream to avoid having too many swipe-events.
public IObservable<float> InputStream { get; private set; }
// Event values will be grouped by threes and averaged
// to smooth out noise.
[SerializeField]
private int _bufferCount = 3;
// Average displacement level below which no swipe event
// will be triggered.
[SerializeField]
private float _minimumThreshhold = 15;
// To prevent back-to-back swipe events.
[SerializeField]
private float _slidingTimeoutWindowSeconds = .25f;
private void Update()
{
// We push values to our stream by calling OnNext() on our
// Subject in each game step.
_subject.OnNext(GetInputValue());
}
// Here is where we get the value that will be pushed into the stream.
// For this demonstration, I'm using the mouse-position. You could easily
// override this and return touch positions instead.
protected virtual float GetInputValue()
{
return Input.mousePosition.x;
}
public SwipeListener()
{
_subject = new Subject<float>();
// This is where the real magic happens. IObservable supports many of the
// same LINQ operations as IEnumerable.
IObservable<float> filtered = _subject
// Convert stream of horizontal mouse positions into a stream of
// mouse speed values.
.Pairwise()
.Select(x => x.Current - x.Previous)
// Group events and average them to smooth out noise. The group size
// can be configured in the inspector.
.Buffer(_bufferCount)
.Select(x => x.Average())
// Filter out events if the mouse was not moving quickly enough. This
// can be configured in the inspector. You'll want to play around with this.
.Where(x => Mathf.Abs(x) > _minimumThreshhold);
// Now we'll apply a sliding timeout window to throttle our stream. this
// will help prevent multiple back-to-back swipe events when you swipe once.
// I've split the event stream into two separate streams so we can throttle
// left and right swipes separately. This will help ensure that there isn't
// unnecessary lag when swiping left after having swiped right, or vice-versa.
TimeSpan seconds = TimeSpan.FromSeconds(_slidingTimeoutWindowSeconds);
IObservable<float> left = filtered
.Where(x => x < 0)
.Throttle(seconds);
IObservable<float> right = filtered
.Where(x => x > 0)
.Throttle(seconds);
// Now that we've throttled left and right swipes separately, we can merge
// them back into a single stream.
InputStream = left.Merge(right);
}
}
现在您所要做的就是编写一个使用上述脚本的脚本。以下是您可以如何执行此操作的示例:
using UnityEngine;
using System.Collections;
using UniRx;
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(SwipeListener))]
public class ColorFlasher : MonoBehaviour
{
[SerializeField]
private Material _left;
[SerializeField]
private Material _right;
private Material _normal;
[SerializeField]
private float _flashPeriod = .1f;
private MeshRenderer _meshRenderer;
private SwipeListener _inputListener;
void Start()
{
_meshRenderer = GetComponent<MeshRenderer>();
_normal = _meshRenderer.material;
_inputListener = GetComponent<SwipeListener>();
_inputListener.InputStream
.Subscribe(x => StartCoroutine(FlashColor(x)));
_inputListener.InputStream.Subscribe(x => Debug.Log(x));
}
private IEnumerator FlashColor(float swipe)
{
Material material = _normal;
if (swipe > 0)
material = _right;
else if (swipe < 0)
material = _left;
_meshRenderer.material = material;
yield return new WaitForSeconds(_flashPeriod);
_meshRenderer.material = _normal;
}
}
上面的脚本只有一个立方体,只要检测到滑动,它就会短暂地闪烁不同的颜色。
以上示例的完整版本可在 on my github 上获得。 .
关于c# - 我可以使用 Input.GetAxis ("Horizontal") 来检测 Unity 中的水平滑动吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42150941/
一个屏幕上有多个横向的Recyclerview。我想在第一个水平 Recyclerview 的第一个项目上执行 espresso click 事件。请告诉我如何实现它。 onView(withId(
我目前正在为一个网站开发移动样式表,但我遇到了一个我无法解释的奇怪问题: 即使我的样式表中不再有“宽度”定义,Android 浏览器会在右侧留下大量空白区域,导致出现滚动条。 我不知道是什么原因导致的
我有一个由 graphviz 工具制作的无向图(现在我正在使用 sfdp ): digraph structs { node [shape=Mrecord, URL="index_ne
关闭。这个问题需要details or clarity .它目前不接受答案。 想改善这个问题吗?通过 editing this post 添加详细信息并澄清问题. 8年前关闭。 Improve thi
假设我有一行单元格,其中包含以下值: B2: "banana" C2: "orange" D2: "apple" 我把这个范围变成一个二维数组。我的主要代码在Word文档中: dim MySheet
我正在创建一个基于位置的提醒,EKReminder。 对于坐标,我使用 CLGeocoder 将地址转换为 CLLocation。 就提醒本身而言,我认为有两个因素决定了提醒将触发的“区域”(半径/圆
我有一个问题可以用一个很像 SO 的系统来模拟:帖子和标签。要获得我的帖子,我可能会这样做: SELECT title, body, author FROM posts WHERE id = ? 然后
我希望将结果生成到水平联合中。我能够组合在一起并生成结果。但是,我需要以预期结果格式显示结果。 SQL (SELECT kpa_id AS 'KPA', SUM(weightage) AS '
我正在研究 CMS 模板,并尝试找出这是否可行。我在网上找不到任何东西,也许我只是使用了错误的关键字。 给出这个模型: http://img833.imageshack.us/img833/4979/
我有一个 LTR 方向的 Bootstrap v4.0.0 模板,我想将整个模板更改为 RTL 方向,我使用了以下这些方法: body { direction:rtl; } html { dire
嗯,几秒钟前还好,但现在当我指向“水平”时,它总是说“水平无法解析或不是一个字段”。我已经删除了旧项目并创建了一个新项目,但仍然遇到这个问题。有人能告诉我发生了什么事吗?谢谢。我正在使用 eclips
我正在寻找一种仅在 html 表格 View 中水平或垂直滚动同时保持始终可见的标题和行的方法。最好我想要类似于 this 的东西但在没有 ember 或 coffeescript 的纯 Java
每当我需要定位某些东西使其垂直和/或水平时,我通常会求助于 flexbox,如果那不是解决方案,我会使用 position: absolute 和 top:50%; 和/或 left:50%; 与 t
我想创建一个从右向左滚动然后从左侧消失并再次从右侧重新出现的 TextView。我可以使用动画??谢谢 最佳答案 我相信您希望您的 TextView 成为选取框。如果是这样,我就是这样做的: 在 XM
我只需要在屏幕上平行放置两个按钮(每个按钮的宽度为 50dp)。第一个应该有 margin left 10dp 并且放置没问题。 但是第二个按钮应该放置在屏幕中,并在中间(水平)向左留出 30dp 的
新的Bootstrap 三个文档explains the grid behaviour作为超小型设备的“始终水平”。这是什么意思?在小型设备上,所有列肯定会垂直堆叠在一起吗?在这里让我失望的是我(缺乏
我需要一些帮助来隐藏我的水平滚动条并仍然能够滚动。我使用过 webkit,但在 IE 和 firefox 中不起作用。我看到很多关于垂直滚动条的帮助,但不适用于水平滚动条。有帮助吗? 更新:我创建了一
为什么不支持水平滚动?
在 examples page当您搜索(CTRL+F)图片库并单击第一张图片时,您可以使用下一个/上一个箭头浏览此图片库。它们之间的动画是垂直的——图像垂直飞行。如何让它们水平飞行? 在我找到的帮助中
我在移动网站上使用 Owl Carousel。 当用户水平滚动然后停止时,内容立即停止移动(或停止后不久)。 是否可以添加一些惯性? 我尝试了文档中公开的不同选项,例如 smartSpeed , fl
我是一名优秀的程序员,十分优秀!