AutoInjectComponent.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.IO;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEngine;
  6. namespace Rokid.UXR
  7. {
  8. /// <summary>
  9. /// Auto inject compoent
  10. /// </summary>
  11. public class AutoInjectComponent
  12. {
  13. /// <summary>
  14. /// Find method
  15. /// </summary>
  16. /// <param name="transform"></param>
  17. /// <param name="filedInfos"></param>
  18. /// <param name="obj"></param>
  19. private static void Find(Transform transform, Dictionary<FieldInfo, string> filedInfos, object obj, Dictionary<FieldInfo, string> flag)
  20. {
  21. foreach (Transform child in transform)
  22. {
  23. foreach (KeyValuePair<FieldInfo, string> pair in filedInfos)
  24. {
  25. var fieldName = pair.Key.ToString().Split(' ')[1].ToLower();
  26. string objName = string.IsNullOrEmpty(pair.Value) ? fieldName : pair.Value;
  27. if (objName.ToLower() == child.name.ToLower())
  28. {
  29. string[] names = pair.Key.FieldType.ToString().Split('.');
  30. string typeName = names[names.Length - 1];
  31. if (typeName.ToLower() == "gameobject")
  32. {
  33. pair.Key.SetValue(obj, child.gameObject);
  34. }
  35. else
  36. {
  37. pair.Key.SetValue(obj, child.GetComponent(typeName));
  38. }
  39. flag.Add(pair.Key, pair.Value);
  40. }
  41. }
  42. foreach (var key in flag.Keys)
  43. {
  44. filedInfos.Remove(key);
  45. }
  46. flag.Clear();
  47. if (child.childCount > 0 && filedInfos.Count > 0)
  48. {
  49. Find(child, filedInfos, obj, flag);
  50. }
  51. }
  52. }
  53. /// <summary>
  54. /// 自动注入组件(通过Autowrited定义的对象名(如果对象名为空,则使用字段名)和场景的中的对象名称匹配选择)
  55. /// 注意名称查找不区分大小写
  56. /// </summary>
  57. /// <param name="tsf">绑定组件的对象tsf</param>
  58. /// <param name="obj">组件实例</param>
  59. public static void AutoInject(Transform tsf, object obj)
  60. {
  61. var type = obj.GetType();
  62. List<FieldInfo> allFiledInfos = new List<FieldInfo>();
  63. Dictionary<FieldInfo, string> needInjectFieldInfos = new Dictionary<FieldInfo, string>();
  64. allFiledInfos.AddRange(type.GetFields().ToList()); //找到类中的所有的公共字段
  65. allFiledInfos.AddRange(type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic).ToList()); //找到类中的所有的私有字段
  66. for (int i = 0; i < allFiledInfos.Count; i++)
  67. {
  68. //获取所有的特性
  69. object[] objects = allFiledInfos[i].GetCustomAttributes(true);
  70. try
  71. {
  72. for (int j = 0; j < objects.Length; j++)
  73. {
  74. if (objects[j].GetType() == typeof(Autowrited))
  75. {
  76. Autowrited autowrited = (Autowrited)objects[j];
  77. needInjectFieldInfos.Add(allFiledInfos[i], autowrited.targertObjName);
  78. break;
  79. }
  80. }
  81. }
  82. catch (System.Exception e)
  83. {
  84. RKLog.Info(e.ToString());
  85. }
  86. }
  87. Find(tsf, needInjectFieldInfos, obj, new Dictionary<FieldInfo, string>());
  88. foreach (var fileInfo in needInjectFieldInfos.Keys)
  89. {
  90. if (fileInfo.GetValue(obj) == null)
  91. {
  92. RKLog.Info(string.Format("{0},{1},没有找到目标组件", obj, fileInfo.ToString().Split(' ')[1]));
  93. }
  94. }
  95. }
  96. }
  97. }