You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.2 KiB
41 lines
1.2 KiB
using System; |
|
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using UnityEngine.UI; |
|
|
|
|
|
public class WaterUsedValue : MonoBehaviour { |
|
/// <summary> |
|
/// 已用水量统计 |
|
/// </summary> |
|
public static event Func<FloatData, FloatData> getAllUsedWater; |
|
private FloatData data = new FloatData(0); |
|
private void OnEnable() |
|
{ |
|
data.Clear(); |
|
if(getAllUsedWater != null) |
|
{ |
|
data = getAllUsedWater(data); |
|
} |
|
GetComponent<Text>().text = data.value > 1000 ? Math.Round(data.value / 1000,2).ToString() + " T" : Math.Round(data.value,2).ToString() + " L"; |
|
StartCoroutine(GetUsedWaterOnInternal()); |
|
} |
|
private void OnDisable() |
|
{ |
|
StopCoroutine(GetUsedWaterOnInternal()); |
|
} |
|
IEnumerator GetUsedWaterOnInternal() |
|
{ |
|
while (true) |
|
{ |
|
yield return new WaitForSeconds(1.0f); |
|
data.Clear(); |
|
if (getAllUsedWater != null) |
|
{ |
|
data = getAllUsedWater(data); |
|
} |
|
GetComponent<Text>().text = data.value > 1000 ? Math.Round(data.value / 1000, 2).ToString() + " T" : Math.Round(data.value, 2).ToString() + " L"; |
|
} |
|
} |
|
}
|
|
|