c# - 当计数等于 12 时如何停止计时器
问题描述
我正在学习统一教程,我想知道当你达到 12 个计数时如何停止计时器。我目前有两个正在使用的脚本。
- 播放器控制器
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;
public class PlayerController : MonoBehaviour
{
public float speed;
public TextMeshProUGUI countText;
public GameObject winTextObject;
private float movementX;
private float movementY;
private Rigidbody rb;
private int count;
// At the start of the game..
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winTextObject.SetActive(false);
}
void FixedUpdate()
{
// Create a Vector3 variable, and assign X and Z to feature the horizontal and vertical float variables above
Vector3 movement = new Vector3(movementX, 0.0f, movementY);
rb.AddForce(movement * speed);
}
void OnTriggerEnter(Collider other)
{
// ..and if the GameObject you intersect has the tag 'Pick Up' assigned to it..
if (other.gameObject.CompareTag("Pickup"))
{
other.gameObject.SetActive(false);
// Add one to the score variable 'count'
count = count + 1;
// Run the 'SetCountText()' function (see below)
SetCountText();
}
if (other.gameObject.CompareTag("SpeedPickup"))
{
other.gameObject.SetActive(false);
speed = speed + 25;
count = count + 1;
}
}
void OnMove(InputValue value)
{
Vector2 v = value.Get<Vector2>();
movementX = v.x;
movementY = v.y;
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 12)
{
winTextObject.SetActive(true);
}
}
}
第二个脚本 Timer.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Timer : MonoBehaviour
{
[SerializeField]
private TextMeshProUGUI timerUILabel; //drag UI Text object here via Inspector
private float t_offset = 0f; // set to nothing, can be set to an offset if needed.
private int t_minutes;
private int t_seconds;
private int t_milliseconds;
private bool finished = false;
private void Update()
{
float t = Time.time - t_offset;
//Debug.Log("currentTime in seconds = " + t);
t_minutes = ((int)t / 60); // t(seconds) / 60 = total minutes
t_seconds = ((int)t % 60); // t(seconds) % 60 = remaining seconds
t_milliseconds = ((int)(t * 100)) % 100; // (total seconds * 1000) % 1000 = remaining milliseconds
//display the text in a 00:00:00 format
timerUILabel.text = string.Format("{0:00}:{1:00}:{2:00}", t_minutes, t_seconds, t_milliseconds);
}
}
我的问题是
- 我是否需要将 Timer.cs 脚本导入 PlayerController 才能使此功能正常工作?如果有怎么办?(是否像使用 using 语句一样简单?)
- 我想我需要添加一个额外的条件来将获胜条件从假更改为真。那是正确的道路吗?
感谢你的帮助!
解决方案
为什么不使用协程?
private int _time;
private Coroutine _cr;
void Start ()
{
//Call this to start it and save coroutine to a variable
_cr = StartCoroutine(Timer());
//Then to stop it call:
//StopCoroutine(_cr);
//And read the value of _time to get seconds since start.
}
private IEnumerator Timer ()
{
yield return new WaitForSeconds(1);
_time++;
_cr = StartCoroutine(Timer());
}
推荐阅读
- python - 如何在 Python 中委派 Google Admin SDK 中的联系人
- java - Hibernate 无法延迟初始化角色集合 无法初始化代理 - 没有会话
- mysql - 如何在mysql中用id替换值
- spring-boot - 使用 webtestclient 的 Multipart/form-data 的 Spring REST 文档
- datatables - 没有初始数据时如何为单元格设置默认属性
- debian - “apt depends”在哪里记录?
- image - 图像溢出 Flutter
- vb.net - 尝试从 VB.Net 应用程序以编程方式安装服务 - 调用 SC.exe 失败
- linux - 是否可以根据按下和释放或与其他键一起按下来改变字母键的行为?
- command-line - $PATH 搞砸了。这是如何发生的以及如何更改回默认值