예제 #1
0
 public static void Inject(MonoBehaviour mono)
 {
     foreach (var item in interfaces.Values)
     {
         ReflectEx.Inject(mono, item);
     }
 }
예제 #2
0
 static void ReInjectInterface(Type type, IInterface inter)
 {
     foreach (var item in injected)
     {
         ReflectEx.Inject(interfaces[item], inter);
     }
     foreach (var item in monoInjected)
     {
         ReflectEx.Inject(item, inter);
     }
 }
예제 #3
0
 static IInterface FindInterface(Type type)
 {
     foreach (var item in interfaces.Values)
     {
         if (type.IsAssignableFrom(item.GetType()))
         {
             if (ReflectEx.Compare(type, item.GetType()) <= 0)
             {
                 return(item);
             }
         }
     }
     return(null);
 }
예제 #4
0
        public static IInterface Instance(Type type)
        {
            var instence = FindInterface(type);

            if (instence == null)
            {
                instence = ReflectEx.Instance(type) as IInterface;
                var init = (instence as IInitializable);
                if (init != null)
                {
                    initializables.Enqueue(init);
                }
            }
            return(instence);
        }
예제 #5
0
        void InjectSence()
        {
            Stopwatch stopwatch = null;

            if (debug)
            {
                stopwatch = new Stopwatch();
                stopwatch.Start();
            }
            ClearNullObj();
            foreach (var assembly in ReflectEx.workAssemblies)
            {
                foreach (var item in assembly.GetTypes())
                {
                    if (item == typeof(IInterface) || interfaces.ContainsKey(item))
                    {
                        continue;
                    }
                    if (typeof(IInterface).IsAssignableFrom(item) && item.IsInterface)
                    {
                        var instence = Instance(item);
                        //if (instence == null)
                        //{
                        //    instence = ReflectEx.Instance(item) as IInterface;
                        //    var init = (instence as IInitializable);
                        //    if (init != null)
                        //    {
                        //        if (!initializables.Contains(init))
                        //        {
                        //            //UnityEngine.Debug.Log($"find init {init}");
                        //            initializables.Enqueue(init);
                        //        }
                        //    }
                        //}
                        //UnityEngine.Debug.Log($"{item},{instence}");
                        if (instence != null)
                        {
                            interfaces.Add(item, instence);
                        }
                    }
                }
            }


            foreach (var obj in interfaces)
            {
                if (injected.Contains(obj.Key))
                {
                    continue;
                }
                injected.Add(obj.Key);
                foreach (var inter in interfaces.Values)
                {
                    ReflectEx.Inject(obj.Value, inter);
                }
            }
            if (isInjectScene)
            {
                foreach (var obj in Resources.FindObjectsOfTypeAll <MonoBehaviour>())
                {
                    if (!obj.gameObject.scene.IsValid() || obj.GetType().FullName.Contains("UnityEngine"))
                    {
                        continue;
                    }
                    if (monoInjected.Contains(obj))
                    {
                        continue;
                    }
                    monoInjected.Add(obj);
                    Inject(obj);
                    //Debug.Log(obj);
                }
            }
            //foreach (var item in initializables)
            //{
            //    item.Initialize();
            //}
            //initializables.Clear();
            while (initializables.Count > 0)
            {
                initializables.Dequeue().Initialize();
            }

            if (debug)
            {
                stopwatch.Stop();
                UnityEngine.Debug.Log(stopwatch.Elapsed);
            }
        }