2020-03-22 19:05:26 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
using MessagePack;
|
|
|
|
|
|
using Unity.Mathematics;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using Random = UnityEngine.Random;
|
|
|
|
|
|
|
|
|
|
|
|
using static Unity.Mathematics.math;
|
|
|
|
|
|
|
|
|
|
|
|
public static class UnityExtensions
|
|
|
|
|
|
{
|
2020-06-10 02:45:10 +01:00
|
|
|
|
public static T FindInParents<T>(this GameObject go) where T : Component
|
|
|
|
|
|
{
|
|
|
|
|
|
if (go == null) return null;
|
|
|
|
|
|
var comp = go.GetComponent<T>();
|
|
|
|
|
|
|
|
|
|
|
|
if (comp != null)
|
|
|
|
|
|
return comp;
|
|
|
|
|
|
|
|
|
|
|
|
Transform t = go.transform.parent;
|
|
|
|
|
|
while (t != null && comp == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
comp = t.gameObject.GetComponent<T>();
|
|
|
|
|
|
t = t.parent;
|
|
|
|
|
|
}
|
|
|
|
|
|
return comp;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static bool ContainsWorldPoint(this RectTransform rectTransform, Vector3 point)
|
|
|
|
|
|
{
|
|
|
|
|
|
Vector2 localMousePosition = rectTransform.InverseTransformPoint(point);
|
|
|
|
|
|
return rectTransform.rect.Contains(localMousePosition);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-28 04:21:04 +01:00
|
|
|
|
public static void BroadcastAll(string fun, System.Object msg) {
|
|
|
|
|
|
GameObject[] gos = (GameObject[])GameObject.FindObjectsOfType(typeof(GameObject));
|
|
|
|
|
|
foreach (GameObject go in gos) {
|
|
|
|
|
|
if (go && go.transform.parent == null) {
|
|
|
|
|
|
go.gameObject.BroadcastMessageExt<MonoBehaviour>(fun, msg, SendMessageOptions.DontRequireReceiver);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-22 19:05:26 +00:00
|
|
|
|
public static void BroadcastMessageExt<T>(this GameObject go, string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver)
|
|
|
|
|
|
{
|
|
|
|
|
|
var monoList = new List<T>();
|
|
|
|
|
|
go.GetComponentsInChildren(true, monoList);
|
2020-04-28 04:21:04 +01:00
|
|
|
|
//monoList.Add();
|
2020-03-22 19:05:26 +00:00
|
|
|
|
foreach (var component in monoList)
|
|
|
|
|
|
{
|
2020-04-29 11:27:55 +01:00
|
|
|
|
// try
|
|
|
|
|
|
// {
|
2020-03-22 19:05:26 +00:00
|
|
|
|
Type type = component.GetType();
|
|
|
|
|
|
|
|
|
|
|
|
MethodInfo method = type.GetMethod(methodName, BindingFlags.Instance |
|
|
|
|
|
|
BindingFlags.NonPublic |
|
|
|
|
|
|
BindingFlags.Public |
|
|
|
|
|
|
BindingFlags.Static);
|
|
|
|
|
|
|
2020-04-28 01:50:50 +01:00
|
|
|
|
if(method!=null)
|
|
|
|
|
|
method.Invoke(component, new[] { value });
|
2020-04-29 11:27:55 +01:00
|
|
|
|
// }
|
|
|
|
|
|
// catch (Exception e)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// //Re-create the Error thrown by the original SendMessage function
|
|
|
|
|
|
// if (options == SendMessageOptions.RequireReceiver)
|
|
|
|
|
|
// Debug.LogError("SendMessage " + methodName + " has no receiver!");
|
|
|
|
|
|
//
|
|
|
|
|
|
// Debug.LogError(e.Message);
|
|
|
|
|
|
// }
|
2020-03-22 19:05:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Texture2D ToTexture(this Color c)
|
|
|
|
|
|
{
|
|
|
|
|
|
Texture2D result = new Texture2D(1, 1);
|
|
|
|
|
|
result.SetPixels(new[]{c});
|
|
|
|
|
|
result.Apply();
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2020-11-29 22:50:12 +00:00
|
|
|
|
|
|
|
|
|
|
private const int GradSteps = 32;
|
|
|
|
|
|
public static Texture2D ToTexture(this Gradient g)
|
|
|
|
|
|
{
|
|
|
|
|
|
var tex = new Texture2D(GradSteps, 1) {wrapMode = TextureWrapMode.Clamp};
|
|
|
|
|
|
for (int x = 0; x < GradSteps; x++)
|
|
|
|
|
|
{
|
|
|
|
|
|
tex.SetPixel(x, 0, g.Evaluate( (float)x / GradSteps));
|
|
|
|
|
|
}
|
|
|
|
|
|
tex.Apply();
|
|
|
|
|
|
return tex;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-01 20:38:28 +00:00
|
|
|
|
public static Gradient ToGradient(this float4[] keys, bool sharp = false)
|
2020-11-29 22:50:12 +00:00
|
|
|
|
{
|
|
|
|
|
|
var grad = new Gradient();
|
2020-12-01 20:38:28 +00:00
|
|
|
|
grad.mode = sharp ? GradientMode.Fixed : GradientMode.Blend;
|
2020-11-29 22:50:12 +00:00
|
|
|
|
grad.alphaKeys = new[] {new GradientAlphaKey(1, 0), new GradientAlphaKey(1, 1)};
|
2020-12-10 23:20:38 +00:00
|
|
|
|
grad.colorKeys = keys.Select(k => new GradientColorKey(k.xyz.ToColor(), k.w)).ToArray();
|
2020-11-29 22:50:12 +00:00
|
|
|
|
return grad;
|
|
|
|
|
|
}
|
2021-01-05 02:56:17 +00:00
|
|
|
|
|
|
|
|
|
|
public static AnimationCurve ToCurve(this float4[] keys)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new AnimationCurve(keys.Select(v => new Keyframe(v.x, v.y, v.z, v.w)).ToArray());
|
|
|
|
|
|
}
|
2020-11-27 22:47:33 +00:00
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
|
public static Color ToColor(this float3 v) => new Color(v.x,v.y,v.z);
|
2021-03-11 11:18:21 +00:00
|
|
|
|
public static float3 ToFloat3(this Color c) => float3(c.r,c.g,c.b);
|
2020-12-10 23:20:38 +00:00
|
|
|
|
|
2020-12-18 13:48:03 +00:00
|
|
|
|
public static Vector3 Flatland(this Vector2 v, float y = 0) => new Vector3(v.x,y,v.y);
|
2020-11-27 22:47:33 +00:00
|
|
|
|
public static Vector2 Flatland(this Vector3 v) => new Vector2(v.x,v.z);
|
2020-03-22 19:05:26 +00:00
|
|
|
|
}
|