Esempio n. 1
0
        virtual protected void FindSceneServiceRegistrations()
        {
            foreach (Transform childT in this.transform)
            {
                ServiceRegistration reg = childT.GetComponent <ServiceRegistration>();                // TODO: should support multiple (if not why?)
                if (reg != null)
                {
                    // this service has a custom ServiceRegistration component...
                    reg.SetServiceRegistration(this);
                }
                else
                {
                    using (var comps = ListPool <Component> .Get()) {
                        childT.GetComponents <Component>(comps);

                        bool didRegister = false;
                        foreach (var c in comps)
                        {
                            foreach (var attr in c.GetType().GetCustomAttributes(false))
                            {
                                var regAttr = attr as ServiceInterfaceAttribute;
                                if (regAttr == null)
                                {
                                    continue;
                                }

                                new ComponentServiceRegistration(c, regAttr.serviceInterface ?? c.GetType()).SetServiceRegistration(this);

                                didRegister = true;
                            }
                        }

                        if (didRegister)
                        {
                            continue;
                        }

                        // can we auto register?
                        if (comps.Count == 2)
                        {
                            // we can auto register if there is only one (non-transform) component attached
                            Component service = comps[0] is Transform ? comps[1] : comps[0];

                            new ComponentServiceRegistration(service).SetServiceRegistration(this);
                        }
                        else
                        {
                            Debug.LogWarning("Encountered service object that has multiple non-transform components. Use a ServiceRegistration component!");
                        }
                    }
                }
            }
        }