using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using UnityEngine;
/// <summary>
/// api中心,处理api相关的内容,不含ws
/// </summary>
public class APICenterManager : Singleton<APICenterManager>
{
public void Start()
{
TestBtnManager.Instance.AddTestBtn("模型收到web调用_没有回调", "API", () =>
{
JsonData json = new JsonData();
//demo.SetJsonType(JsonType.Object);
json["id"] = "1231232";
json["cn"] = "Camera";
json["fn"] = "Test";
json["p"] = "我是参数,英文ASSSaaa1123";
收到消息(json.ToJson());
});
TestBtnManager.Instance.AddTestBtn("模型收到web调用_有回调", "API", () =>
{
JsonData json = new JsonData();
//demo.SetJsonType(JsonType.Object);
json["id"] = "1231232444";
json["cn"] = "Camera";//类名,物体
json["fn"] = "Test_CallBack";//方法名
json["p"] = "我是参数,英文ASSSaaa1123";//参数
收到消息(json.ToJson());
});
TestBtnManager.Instance.AddTestBtn("2", "API", () =>
{
//{
// List<string> STRS = GetFields<API_Camera>(API_Camera.Instance);
// foreach (string item in STRS)
// {
// Debug.Log(item);
// }
//}
//{
// List<string> STRS = GetProperties<API_Camera>(API_Camera.Instance);
// foreach (string item in STRS)
// {
// Debug.Log(item);
// }
//}
});
}
public void 反射字符转类名()
{
string className = "API_Camera";
Type t;
t = Type.GetType(className);
object obj = t.Assembly.CreateInstance(className);
UnityEngine.Debug.Log(obj);//这个是空,下面的也能运行?????
// var obj = System.Activator.CreateInstance(t);
MethodInfo[] info = t.GetMethods();
for (int i = 0; i < info.Length; i++)
{
// info[i].Invoke(obj, null);
}
for (int i = 0; i < info.Length; i++)
{
string str = info[i].Name;
UnityEngine.Debug.Log("方法名:" + str);
}
//foreach (var item in t.GetRuntimeProperties())
//{
// if (!item.CanRead || !item.GetMethod.IsPublic)
// {
// continue;
// }
// // Debug.Log($"属性名称:{item.Name},类型:{item.PropertyType.Name},值:{item.GetValue()}");
//}
//var fields = t.GetRuntimeFields().Where(f => f.IsPublic).ToArray();
//foreach (var item in fields)
//{
// Debug.Log($"字段名称:{item.Name},类型:{item.FieldType.Name},值:{item.GetValue(data)}");
//}
MethodInfo method = t.GetMethod("j");
method.Invoke(obj, null);
}
Dictionary<string, object> API类库 = new Dictionary<string, object>();
public void 收到消息(string message)
{
// API_Camera _API_Camera = new API_Camera();
UnityEngine.Debug.Log("消息:"+message);
JsonData json = JsonMapper.ToObject(message);
ApiModel apiModel = new ApiModel();
apiModel.className = Regex.Unescape(json["cn"].ToJson().Replace("\"", ""));
apiModel.functionName = Regex.Unescape(json["fn"].ToJson().Replace("\"", ""));
apiModel.parameter = Regex.Unescape(json["p"].ToJson().Replace("\"", ""));
print(apiModel.functionName);
print(apiModel.parameter);
if (((IDictionary)json).Contains("id"))
{
//就说明有回调的。
apiModel.id = Regex.Unescape((json["id"].ToJson().Replace("\"", "")));
apiModel.type = "callback";
}
else
{
apiModel.type = "call";
}
//-----------------------------------------------------------------------------------------------
//处理消息
//首先,获取类名,需要转化一下。比如Camera转化为API_Camera
string className = "API_" + apiModel.className;
if (!API类库.ContainsKey(className))
{
Type t = Type.GetType(className);
object obj = t.Assembly.CreateInstance(className);
API类库.Add(className, obj);
}
UnityEngine.Debug.Log(apiModel.functionName+":hahaha");
UnityEngine.Debug.Log(className + ":488484");
MethodInfo method = Type.GetType(className).GetMethod(apiModel.functionName);
method.Invoke(API类库[className], new object[1] { apiModel });
Thread.CurrentThread.ManagedThreadId.ToString();
{
// Debug.Log(className);
//Type t = Type.GetType(className);
// Debug.Log(t);
//FieldInfo filedInfo1 = t.GetField("ind");
//Debug.Log(filedInfo1);
//Debug.Log(filedInfo1.Name);
//PropertyInfo propertyInfo = t.GetProperty("Instance", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
//Debug.Log(propertyInfo);
//Debug.Log(propertyInfo.GetType().Name);
// object obj = t.Assembly.CreateInstance(className);//应该调用单例
// var obj = System.Activator.CreateInstance(t);
//Debug.Log(obj);
//Debug.Log(obj.GetType().Name);
//Debug.Log(obj.ToString());
// MethodInfo method = t.GetMethod(apiModel.functionName);
//method.Invoke(obj, new object[1] { apiModel });
}
// 模型统一管理.Instance.Get(className).;不采用这个方式,效率慢,还不好控制。
}
public void 发送消息(ApiModel apiModel)
{
if (apiModel.type == "call")
{
JsonData json = new JsonData();
//demo.SetJsonType(JsonType.Object);
json["id"] = apiModel.id;
json["callFunc"] = apiModel.callFunc;
if (apiModel.callParam.IsNotNullOrEmpty())
{
UnityEngine.Debug.Log(10010);
json["callParam"] = apiModel.callParam;
}
RTCAPIServerManager.Instance.Send_All(json.ToJson());
}
else if (apiModel.type == "callback")
{
JsonData json = new JsonData();
//demo.SetJsonType(JsonType.Object);
json["id"] = apiModel.id;
Debug.Log(apiModel.callBackParam+":sdjashda");
if (apiModel.callBackParam.IsNotNullOrEmpty())
{
json["callBackParam"] = apiModel.callBackParam;
}
RTCAPIServerManager.Instance.Send_All(json.ToJson());
}
}
public List<string> GetProperties<T>(T t)
{
List<string> ListStr = new List<string>();
if (t == null)
{
return ListStr;
}
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (properties.Length <= 0)
{
return ListStr;
}
foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name; //名称
object value = item.GetValue(t, null); //值
if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{
ListStr.Add(name);
}
else
{
GetProperties(value);
}
}
return ListStr;
}
public List<string> GetFields<T>(T t)
{
List<string> ListStr = new List<string>();
if (t == null)
{
return ListStr;
}
System.Reflection.FieldInfo[] fields = t.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
if (fields.Length <= 0)
{
return ListStr;
}
foreach (System.Reflection.FieldInfo item in fields)
{
string name = item.Name; //名称
object value = item.GetValue(t); //值
if (item.FieldType.IsValueType || item.FieldType.Name.StartsWith("String"))
{
ListStr.Add(name);
}
else
{
GetFields(value);
}
}
return ListStr;
}
public List<Action> 事件队列s = new List<Action>();
public void Update()
{
foreach (Action item in 事件队列s)
{
item?.Invoke();
}
事件队列s.Clear();
}
}
public class ApiModel
{
public string id { get; set; }
public string type { get; set; }//callBack,和call。回调和直接调用
//下面是web-u3d的字段
public string className { get; set; }
public string functionName { get; set; }
public string parameter { get; set; }
//下面是web-u3d之后回调的参数
public string callBackParam { get; set; }//如果需要回调,则需要有回调的参数
//下面是unity直接调用web的方法和参数
public string callFunc { get; set; }//如果不是回调,可能会用到,如果是回调,就不需要理财
public string callParam { get; set; }
}
联系客服