我目前正在使用Visual Studio在C#中创建一个应用程序.我想创建一些代码,以便当变量的值为1时,执行某段代码.我知道我可以使用if语句,但问题是该值将在异步进程中更改,因此从技术上讲,if语句可以在值更改之前被忽略.
是否可以创建事件处理程序,以便在变量值更改时触发事件?如果是这样,我该怎么做?
完全有可能我误解了if语句是如何工作的!任何帮助将非常感激.
您如何找到自某个变量发生变化以来的时间?以一个布尔变量为例,您如何找到自上次更改以来的时间?我想使用布尔变量作为触发器(当它为真时激活触发器),但仅在经过一段精确的,恒定的时间(例如0.5秒)后才更改为真(它只能从false更改为真正).
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class hitRegistration : MonoBehaviour
{
AudioSource hitSound;
private bool hitState = false;
// Use this for initialization
void Start()
{
hitSound = gameObject.GetComponent<AudioSource>();
}
void OnMouseOver()
{
Debug.Log("Mouse is over game object.");
if (Input.GetKey(KeyCode.X) && hitState == false)
{
hitSound.Play();
hitState = true;
}
}
private void OnMouseExit()
{
Debug.Log("Mouse is no longer over game object.");
if (hitState == true)
{
// sound clip gets cut if the cursor leaves before …Run Code Online (Sandbox Code Playgroud)