forked from ericpzh/MiniRealisticAirways
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.cs
92 lines (83 loc) · 2.83 KB
/
Util.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
namespace MiniRealisticAirways
{
public static class ReflectionExtensions
{
public static T GetFieldValue<T>(this object obj, string name)
{
// Set the flags so that private and public fields from instances will be found
var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
var field = obj.GetType().GetField(name, bindingFlags);
return (T)field?.GetValue(obj);
}
public static void SetFieldValue<T>(this object obj, string name, T value)
{
// Set the flags so that private and public fields from instances will be found
var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
var field = obj.GetType().GetField(name, bindingFlags);
field.SetValue(obj, value);
}
}
public static class Animation
{
public static IEnumerator BlinkCoroutine(SpriteRenderer spriteRenderer)
{
while (true)
{
spriteRenderer.enabled = false;
yield return new WaitForSecondsRealtime(0.4f);
spriteRenderer.enabled = true;
yield return new WaitForSecondsRealtime(0.4f);
}
}
public static IEnumerator BlinkFastCoroutine(SpriteRenderer spriteRenderer)
{
while (true)
{
spriteRenderer.enabled = false;
yield return new WaitForSecondsRealtime(0.2f);
spriteRenderer.enabled = true;
yield return new WaitForSecondsRealtime(0.2f);
}
}
public static bool Blink()
{
return DateTimeOffset.Now.ToUnixTimeMilliseconds() % 500 < 250;
}
public static bool BlinkLong()
{
return DateTimeOffset.Now.ToUnixTimeMilliseconds() % 500 < 100;
}
public static void SetPixel(bool cond, int x, int y, Color color, ref Texture2D texture)
{
if (cond)
{
texture.SetPixel(x, y, color);
}
else
{
texture.SetPixel(x, y, Color.clear);
}
}
public static Color gaugeColor = new Color(255, 255, 255, 0.5f);
}
public static class Utils
{
public static void Shuffle<T>(ref List<T> list)
{
System.Random rng = new System.Random();
int n = list.Count;
while (n > 1) {
n--;
int k = rng.Next(n + 1);
T item = list[k];
list[k] = list[n];
list[n] = item;
}
}
}
}