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-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)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
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-03-22 19:05:26 +00: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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Texture2D ToTexture(this Color c)
|
|
|
|
|
|
{
|
|
|
|
|
|
Texture2D result = new Texture2D(1, 1);
|
|
|
|
|
|
result.SetPixels(new[]{c});
|
|
|
|
|
|
result.Apply();
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|