如果玩家没有在游戏场景中点击,几分钟后改变场景
changing scene after a couple of minutes if the player did not clicked in the game scene
我想检查用户是否点击了我的游戏对象之一(我有 15 个游戏对象),如果用户在 10 秒内没有点击其中一个,则显示文本,如果他点击了其中一个我的游戏对象他可以继续玩
代码如下:
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>
/// Attach this component to "your game objects" along with collider.
/// Also, you have to attach
/// - PhysicsRaycaster (for 3D)
/// - Physics2DRaycaster (for 2D)
/// to your Main Camera in order to detect Click.
/// </summary>
public class ClickDetector : MonoBehaviour, IPointerClickHandler
{
void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
{
IdleDetector.NotifyClick();
}
}
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// You may want to attach this component to empty GameObject.
/// You need only one instance of this component in the scene.
/// </summary>
public class IdleDetector : MonoBehaviour
{
[SerializeField] float _timeoutSeconds = 10;
[SerializeField] Text _console = default;
static float _timer = 0f;
static bool _isTimedout = false;
void Update()
{
_timer += Time.deltaTime;
if (_timer > _timeoutSeconds)
{
if (!_isTimedout)
{
// Show message.
string message = $"{_timeoutSeconds} seconds elapsed without click.";
Debug.Log(message);
if (_console)
{
_console.text = message;
}
_isTimedout = true;
}
// Do whatever you want on timeout.
}
else if (!_isTimedout && _console.text.Length > 0)
{
_console.text = "";
}
}
public static void NotifyClick()
{
Debug.Log("Click.");
_timer = 0f;
_isTimedout = false;
}
}
如果您想了解它的工作原理,请尝试使用 unitypackage。
我想检查用户是否点击了我的游戏对象之一(我有 15 个游戏对象),如果用户在 10 秒内没有点击其中一个,则显示文本,如果他点击了其中一个我的游戏对象他可以继续玩
代码如下:
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>
/// Attach this component to "your game objects" along with collider.
/// Also, you have to attach
/// - PhysicsRaycaster (for 3D)
/// - Physics2DRaycaster (for 2D)
/// to your Main Camera in order to detect Click.
/// </summary>
public class ClickDetector : MonoBehaviour, IPointerClickHandler
{
void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
{
IdleDetector.NotifyClick();
}
}
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// You may want to attach this component to empty GameObject.
/// You need only one instance of this component in the scene.
/// </summary>
public class IdleDetector : MonoBehaviour
{
[SerializeField] float _timeoutSeconds = 10;
[SerializeField] Text _console = default;
static float _timer = 0f;
static bool _isTimedout = false;
void Update()
{
_timer += Time.deltaTime;
if (_timer > _timeoutSeconds)
{
if (!_isTimedout)
{
// Show message.
string message = $"{_timeoutSeconds} seconds elapsed without click.";
Debug.Log(message);
if (_console)
{
_console.text = message;
}
_isTimedout = true;
}
// Do whatever you want on timeout.
}
else if (!_isTimedout && _console.text.Length > 0)
{
_console.text = "";
}
}
public static void NotifyClick()
{
Debug.Log("Click.");
_timer = 0f;
_isTimedout = false;
}
}
如果您想了解它的工作原理,请尝试使用 unitypackage。