using System.IO; using System.Collections.Generic; using System.Linq; using System.Reflection; using UnityEngine; namespace Rokid.UXR { /// /// Auto inject compoent /// public class AutoInjectComponent { /// /// Find method /// /// /// /// private static void Find(Transform transform, Dictionary filedInfos, object obj, Dictionary flag) { foreach (Transform child in transform) { foreach (KeyValuePair pair in filedInfos) { var fieldName = pair.Key.ToString().Split(' ')[1].ToLower(); string objName = string.IsNullOrEmpty(pair.Value) ? fieldName : pair.Value; if (objName.ToLower() == child.name.ToLower()) { string[] names = pair.Key.FieldType.ToString().Split('.'); string typeName = names[names.Length - 1]; if (typeName.ToLower() == "gameobject") { pair.Key.SetValue(obj, child.gameObject); } else { pair.Key.SetValue(obj, child.GetComponent(typeName)); } flag.Add(pair.Key, pair.Value); } } foreach (var key in flag.Keys) { filedInfos.Remove(key); } flag.Clear(); if (child.childCount > 0 && filedInfos.Count > 0) { Find(child, filedInfos, obj, flag); } } } /// /// 自动注入组件(通过Autowrited定义的对象名(如果对象名为空,则使用字段名)和场景的中的对象名称匹配选择) /// 注意名称查找不区分大小写 /// /// 绑定组件的对象tsf /// 组件实例 public static void AutoInject(Transform tsf, object obj) { var type = obj.GetType(); List allFiledInfos = new List(); Dictionary needInjectFieldInfos = new Dictionary(); allFiledInfos.AddRange(type.GetFields().ToList()); //找到类中的所有的公共字段 allFiledInfos.AddRange(type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic).ToList()); //找到类中的所有的私有字段 for (int i = 0; i < allFiledInfos.Count; i++) { //获取所有的特性 object[] objects = allFiledInfos[i].GetCustomAttributes(true); try { for (int j = 0; j < objects.Length; j++) { if (objects[j].GetType() == typeof(Autowrited)) { Autowrited autowrited = (Autowrited)objects[j]; needInjectFieldInfos.Add(allFiledInfos[i], autowrited.targertObjName); break; } } } catch (System.Exception e) { RKLog.Info(e.ToString()); } } Find(tsf, needInjectFieldInfos, obj, new Dictionary()); foreach (var fileInfo in needInjectFieldInfos.Keys) { if (fileInfo.GetValue(obj) == null) { RKLog.Info(string.Format("{0},{1},没有找到目标组件", obj, fileInfo.ToString().Split(' ')[1])); } } } } }