using System.Collections.Generic; using System.Linq; using System.Reflection; using UnityEngine; namespace UltraCombos { public class Misc { public static object GetInstanceField(System.Type type, object instance, string fieldName) { var bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static; var field = type.GetField(fieldName, bindFlags); return field.GetValue(instance); } public static string NameOfCallingClass() { string fullName; System.Type declaringType; var skipFrames = 2; do { var method = new System.Diagnostics.StackFrame(skipFrames, false).GetMethod(); declaringType = method.DeclaringType; if (declaringType == null) { return method.Name; } skipFrames++; fullName = declaringType.FullName; } while (declaringType.Module.Name.Equals("mscorlib.dll", System.StringComparison.OrdinalIgnoreCase)); return fullName; } public static byte[] ObjectToByteArray(T obj) { if (obj == null) { return null; } var bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); using (var ms = new System.IO.MemoryStream()) { bf.Serialize(ms, obj); return ms.ToArray(); } } public static T ByteArrayToObject(byte[] arrBytes) { using (var ms = new System.IO.MemoryStream()) { var binForm = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); ms.Write(arrBytes, 0, arrBytes.Length); ms.Seek(0, System.IO.SeekOrigin.Begin); var obj = (T)binForm.Deserialize(ms); return obj; } } } public static class Auto { public static IEnumerable Collect(Transform target) where T : MonoBehaviour { return target.GetComponentsInChildren() .Where(c => c.transform.parent == target); } public static IEnumerable CollectGameObjects(Transform target) where T : MonoBehaviour { return Collect(target) .Select(c => c.gameObject); } public static List Generate(Transform target, T template, int count, string naming) where T : MonoBehaviour { if (target == null || template == null || count < 0) { return new List(); } var comps = Collect(target).Select(c => c.gameObject).ToList(); var stack = new Stack(); comps.ForEach(c => stack.Push(c)); while (stack.Count < count) { var go = Object.Instantiate(template.gameObject, target); go.name = $"{naming} - {stack.Count:D4}"; stack.Push(go); } while (stack.Count > count) { var go = stack.Pop(); #if UNITY_EDITOR Object.DestroyImmediate(go); #else Object.Destroy( go ); #endif } return stack.ToList(); } public static void Align(List objs, Vector3 spacing, Vector3 offset) { for (var i = 0; i < objs.Count; ++i) { var o = objs[i]; o.transform.localPosition = offset + spacing * i; } } public static void Destroy(Transform target) where T : MonoBehaviour { var objects = CollectGameObjects(target); foreach (var go in objects) { #if UNITY_EDITOR Object.DestroyImmediate(go); #else Object.Destroy( go ); #endif } ; } } public static class Extension { public static Vector2 Rotate(this Vector2 v, float delta) { return new Vector2( v.x * Mathf.Cos(delta) - v.y * Mathf.Sin(delta), v.x * Mathf.Sin(delta) + v.y * Mathf.Cos(delta) ); } public static long ToUnixTimeMilliseconds(this System.DateTime dateTime) { return ((System.DateTimeOffset)dateTime).ToUnixTimeMilliseconds(); } } }