-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPCManager.cs
54 lines (48 loc) · 1.48 KB
/
RPCManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Reflection;
using System.Linq;
namespace UtilPack4Unity
{
public class RPCManager : MonoBehaviour
{
public event Action<object[]> OnRaisedEvent;
public void Call(string methodName, object[] args)
{
var mi = typeof(RPCManager).GetMethod(methodName);
if (mi == null) return;
mi.Invoke(this, args);
}
private void Execute(string componentType, string methodName, object[] args)
{
var type = Type.GetType(componentType);
if (type == null) return;
var objects = FindObjectsOfType(type);
foreach (var obj in objects)
{
try
{
var component = Convert.ChangeType(obj, type);
if (component == null) continue;
var mi = type.GetMethod(methodName);
mi.Invoke(component, args);
}
catch
{
}
}
}
private void Send(string gameObjectName, string methodName, object[] args)
{
var go = GameObject.Find(gameObjectName);
if (go == null) return;
go.SendMessage(methodName, args);
}
private void Raise(object[] args)
{
if (OnRaisedEvent != null) OnRaisedEvent(args);
}
}
}