예제 #1
0
        /// <summary>
        /// Injects named UI objects and Emitter to all systems added to EcsSystems.
        /// </summary>
        /// <param name="ecsSystems">EcsSystems group.</param>
        /// <param name="emitter">EcsUiEmitter instance.</param>
        /// <param name="skipNoExists">Not throw exception if named action not registered in emitter.</param>
        /// <param name="skipOneFrames">Skip OneFrame-event cleanup registration.</param>
        public static EcsSystems InjectUi(this EcsSystems ecsSystems, EcsUiEmitter emitter, bool skipNoExists = false, bool skipOneFrames = false)
        {
            if (!skipOneFrames)
            {
                InjectOneFrames(ecsSystems);
            }
            ecsSystems.Inject(emitter);
            emitter.World = ecsSystems.World;
            var uiNamedType   = typeof(EcsUiNamedAttribute);
            var goType        = typeof(GameObject);
            var componentType = typeof(Component);
            var systems       = ecsSystems.GetAllSystems();

            for (int i = 0, iMax = systems.Count; i < iMax; i++)
            {
                var system     = systems.Items[i];
                var systemType = system.GetType();
                foreach (var f in systemType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
                {
                    // skip statics or fields without [EcsUiNamed] attribute.
                    if (f.IsStatic || !Attribute.IsDefined(f, uiNamedType))
                    {
                        continue;
                    }
                    var name = ((EcsUiNamedAttribute)Attribute.GetCustomAttribute(f, uiNamedType)).Name;
#if DEBUG
                    if (string.IsNullOrEmpty(name))
                    {
                        throw new Exception($"Cant Inject field \"{f.Name}\" at \"{systemType}\" due to [EcsUiNamed] \"Name\" parameter is invalid.");
                    }
                    if (!(f.FieldType == goType || componentType.IsAssignableFrom(f.FieldType)))
                    {
                        throw new Exception($"Cant Inject field \"{f.Name}\" at \"{systemType}\" due to [EcsUiNamed] attribute can be applied only to GameObject or Component type.");
                    }
                    if (!skipNoExists && !emitter.GetNamedObject(name))
                    {
                        throw new Exception($"Cant Inject field \"{f.Name}\" at \"{systemType}\" due to there is no UI action with name \"{name}\".");
                    }
#endif
                    var go = emitter.GetNamedObject(name);
                    // GameObject.
                    if (f.FieldType == goType)
                    {
                        f.SetValue(system, go);
                        continue;
                    }
                    // Component.
                    if (componentType.IsAssignableFrom(f.FieldType))
                    {
                        f.SetValue(system, go != null ? go.GetComponent(f.FieldType) : null);
                    }
                }
            }
            return(ecsSystems);
        }
예제 #2
0
        public static EcsSystems Inject(this EcsSystems systems, params object[] injects)
        {
            if (injects == null)
            {
                injects = Array.Empty <object> ();
            }
            IEcsSystem[] allSystems   = null;
            var          systemsCount = systems.GetAllSystems(ref allSystems);
            var          shared       = systems.GetShared <object> ();
            var          sharedType   = shared?.GetType();

            for (var i = 0; i < systemsCount; i++)
            {
                var system = allSystems[i];
                foreach (var f in system.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
                {
                    // skip statics.
                    if (f.IsStatic)
                    {
                        continue;
                    }
                    // EcsWorld.
                    if (InjectWorld(f, system, systems))
                    {
                        continue;
                    }
                    // EcsPool.
                    if (InjectPool(f, system, systems))
                    {
                        continue;
                    }
                    // Shared.
                    if (InjectShared(f, system, shared, sharedType))
                    {
                        continue;
                    }
                    // Inject.
                    if (InjectCustomData(f, system, injects))
                    {
                        continue;
                    }
                }
            }

            return(systems);
        }
예제 #3
0
        void OnInitSystemsGUI(EcsSystems systemsGroup)
        {
            var systems = systemsGroup.GetAllSystems();

            EditorGUI.indentLevel++;
            for (var i = 0; i < systems.Count; i++)
            {
                var item = systems.Items[i];
                if (item is IEcsInitSystem)
                {
                    var asSystems = item as EcsSystems;
                    EditorGUILayout.LabelField(asSystems != null ? $"[{asSystems.Name ?? asSystems.GetType ().Name}]" : systems.Items[i].GetType().Name);
                    if (asSystems != null)
                    {
                        OnInitSystemsGUI(asSystems);
                    }
                }
            }
            EditorGUI.indentLevel--;
        }