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.
160 lines
4.6 KiB
160 lines
4.6 KiB
using LitJson; |
|
using System; |
|
using System.Collections; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using UnityEngine; |
|
using UnityEngine.AI; |
|
using UnityEngine.UI; |
|
|
|
public static class Util |
|
{ |
|
public static class EnumUtil<T> |
|
{ |
|
public static T Parse(string s) |
|
{ |
|
return (T)Enum.Parse(typeof(T), s); |
|
} |
|
|
|
public static int Length() |
|
{ |
|
return Enum.GetNames(typeof(T)).Length; |
|
} |
|
} |
|
public static class ListShuffle<T> |
|
{ |
|
public static List<T> Shuffle(List<T> list) |
|
{ |
|
System.Random rnd = new System.Random(); |
|
return list.OrderBy(a => rnd.Next()).ToList<T>(); |
|
} |
|
} |
|
public static class DropDownList<T> |
|
{ |
|
static public void DropList(Dropdown dropDown, int init = 1, List<Sprite> sprites = null) |
|
{ |
|
dropDown.options.Clear(); |
|
int cnt = 0; |
|
foreach (T enumItem in Enum.GetValues(typeof(T))) |
|
{ |
|
Sprite sprite = sprites == null ? null : (cnt < sprites.Count ? sprites[cnt] : null); |
|
Dropdown.OptionData option = new Dropdown.OptionData($"{enumItem}", sprite); |
|
dropDown.options.Add(option); |
|
|
|
cnt++; |
|
} |
|
if (init != -1) |
|
{ |
|
dropDown.value = init; |
|
dropDown.RefreshShownValue(); |
|
} |
|
} |
|
} |
|
public static class DropDownList |
|
{ |
|
static public void DropList(Dropdown dropDown, List<string> list, int init = 1, List<Sprite> sprites = null) |
|
{ |
|
dropDown.options.Clear(); |
|
int cnt = 0; |
|
foreach (string enumItem in list) |
|
{ |
|
Sprite sprite = sprites == null ? null : (cnt < sprites.Count ? sprites[cnt] : null); |
|
Dropdown.OptionData option = new Dropdown.OptionData($"{enumItem}", sprite); |
|
dropDown.options.Add(option); |
|
|
|
cnt++; |
|
} |
|
if (init != -1) |
|
{ |
|
dropDown.value = init; |
|
dropDown.RefreshShownValue(); |
|
} |
|
} |
|
} |
|
static public bool JsonDataContainsKey(JsonData data, string key) |
|
{ |
|
if (data == null) |
|
return false; |
|
|
|
if (!data.IsObject) |
|
return false; |
|
|
|
IDictionary tdictionary = data as IDictionary; |
|
if (tdictionary == null) |
|
return false; |
|
|
|
if (tdictionary.Contains(key)) |
|
return true; |
|
|
|
return false; |
|
} |
|
static public int GCD(int a, int b) |
|
{ |
|
a = Math.Abs(a); |
|
b = Math.Abs(b); |
|
|
|
// Pull out remainders. |
|
for (; ; ) |
|
{ |
|
int remainder = a % b; |
|
if (remainder == 0) return b; |
|
a = b; |
|
b = remainder; |
|
}; |
|
} |
|
|
|
static public void AnimationStop(Animation animation, string nameClip) |
|
{ |
|
animation[nameClip].time = 0; |
|
animation[nameClip].enabled = true; |
|
animation[nameClip].weight = 1; |
|
animation.Sample(); |
|
animation[nameClip].enabled = false; |
|
} |
|
static public void AnimationPlay(MonoBehaviour mono, Animation animation, string nameClip, bool REVERSE, System.Action OnEnd) |
|
{ |
|
animation[nameClip].speed = REVERSE ? -1 : 1; |
|
animation[nameClip].time = REVERSE ? animation[nameClip].length : 0; |
|
animation.Play(nameClip); |
|
|
|
if (mono != null) |
|
mono.StartCoroutine(PlayWait(animation, nameClip, OnEnd)); |
|
} |
|
static IEnumerator PlayWait(Animation animation, string nameClip, System.Action OnEnd) |
|
{ |
|
while (animation.isPlaying) |
|
yield return null; |
|
|
|
animation[animation.clip.name].speed = 1; |
|
|
|
if (OnEnd != null) |
|
OnEnd(); |
|
} |
|
|
|
static DateTime timeBase = new DateTime(2020, 1, 1, 0, 0, 0); |
|
static public long TimeStamp() |
|
{ |
|
var timeSpan = (DateTime.UtcNow - timeBase); |
|
return (long)timeSpan.TotalSeconds; |
|
} |
|
static public string TimeStamp(long timeStamp) |
|
{ |
|
return $"{(timeStamp / 60):D2}:{(timeStamp % 60):D2}"; |
|
} |
|
|
|
static public Vector3 PointCreate(float radius1, float radius2) |
|
{ |
|
Vector3 getPoint = UnityEngine.Random.onUnitSphere; |
|
getPoint.y = 0; |
|
|
|
return (getPoint * UnityEngine.Random.Range(radius1, radius2)); |
|
} |
|
static public Vector3 PointCreate(Vector3 position, float radius) |
|
{ |
|
Vector3 randomPos = UnityEngine.Random.insideUnitSphere * radius + position; |
|
NavMeshHit hit; |
|
NavMesh.SamplePosition(randomPos, out hit, radius, NavMesh.AllAreas); |
|
|
|
return hit.position; |
|
} |
|
}
|
|
|