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.
150 lines
3.5 KiB
150 lines
3.5 KiB
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>(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<T>(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<T> Collect<T>(Transform target) where T : MonoBehaviour
|
|
{
|
|
return target.GetComponentsInChildren<T>()
|
|
.Where(c => c.transform.parent == target);
|
|
}
|
|
|
|
public static IEnumerable<GameObject> CollectGameObjects<T>(Transform target) where T : MonoBehaviour
|
|
{
|
|
return Collect<T>(target)
|
|
.Select(c => c.gameObject);
|
|
}
|
|
|
|
public static List<GameObject> Generate<T>(Transform target, T template, int count, string naming) where T : MonoBehaviour
|
|
{
|
|
if (target == null || template == null || count < 0)
|
|
{
|
|
return new List<GameObject>();
|
|
}
|
|
|
|
var comps = Collect<T>(target).Select(c => c.gameObject).ToList();
|
|
var stack = new Stack<GameObject>();
|
|
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<GameObject> 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<T>(Transform target) where T : MonoBehaviour
|
|
{
|
|
var objects = CollectGameObjects<T>(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();
|
|
}
|
|
}
|
|
}
|
|
|