コード例 #1
0
        public void InjectComponentFromObject(object obj, FieldInfo fieldInfo)
        {
            var injectComponentFromObject = Attribute.GetCustomAttribute(fieldInfo, typeof(InjectComponentFromObjectAttribute)) as InjectComponentFromObjectAttribute;

            if (ReferenceEquals(injectComponentFromObject, null))
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(injectComponentFromObject.ObjectName))
            {
                throw new ArgumentException("Attribute value is empty", "objectName");
            }

            var componentName = injectComponentFromObject.ObjectName + fieldInfo.Name;

            if (objectPooling.Contains(componentName))
            {
                var injectedComponent = objectPooling.GetObject(componentName) as Component;
                fieldInfo.SetValue(obj, injectedComponent);
            }
            else
            {
                var injectedComponent = gameService.Find(injectComponentFromObject.ObjectName, fieldInfo.FieldType);
                if (ReferenceEquals(injectedComponent, null))
                {
                    throw new NullReferenceException(string.Format("Could not find: {0}. Or component {1} at object with name {0} is not added", injectComponentFromObject.ObjectName, fieldInfo.FieldType));
                }

                objectPooling.AddObject(componentName, injectedComponent);
                fieldInfo.SetValue(obj, injectedComponent);
            }
        }
コード例 #2
0
        public void InjectComponentFromObject(object obj, FieldInfo fieldInfo)
        {
            var injectComponentFromObject = Attribute.GetCustomAttribute(fieldInfo, typeof(InjectComponentFromObjectAttribute)) as InjectComponentFromObjectAttribute;

            if (ReferenceEquals(injectComponentFromObject, null))
            {
                return;
            }

            var componentName = injectComponentFromObject.ObjectName + fieldInfo.Name;

            if (objectPooling.Contains(componentName))
            {
                var injectedComponent = objectPooling.GetObject(componentName) as Component;
                fieldInfo.SetValue(obj, injectedComponent);
            }
            else
            {
                var injectedComponent = gameService.Find(injectComponentFromObject.ObjectName, fieldInfo.FieldType);
                objectPooling.AddObject(componentName, injectedComponent);
                fieldInfo.SetValue(obj, injectedComponent);
            }
        }