Exemplo n.º 1
0
		public override bool Convert(ConvertOperation convert)
		{
			List<Component> availData = convert.Perform<Component>().ToList();
			availData.Sort((Component a, Component b) => a.RequiresComponent(b.GetType()) ? 1 : 0);

			// Generate objects
			foreach (Component cmp in availData)
			{
				if (convert.IsObjectHandled(cmp)) continue;
				Type cmpType = cmp.GetType();

				// Create or retrieve GameObject
				GameObject gameObj = null;
				{
					// First try to get one from the resultset that has an open slot for this kind of Component
					if (gameObj == null)
						gameObj = convert.Result.OfType<GameObject>().FirstOrDefault(g => g.GetComponent(cmpType) == null);
					// Still none? Create a new GameObject
					if (gameObj == null)
					{
						gameObj = new GameObject();

						// Come up with a suitable name
						string nameSuggestion = null;
						{
							// Be open for suggestions
							if (nameSuggestion == null)
								nameSuggestion = convert.TakeSuggestedResultName(cmp);
							// Use a standard name
							if (nameSuggestion == null)
								nameSuggestion = cmpType.Name;
						}

						gameObj.Name = nameSuggestion;
					}
				}

				// Make sure all requirements are met
				foreach (Type t in Component.GetRequiredComponents(cmpType))
					gameObj.AddComponent(t);

				// Make sure no other Component of this Type is already added
				gameObj.RemoveComponent(cmpType);

				// Add Component
				gameObj.AddComponent(cmp.GameObj == null ? cmp : cmp.Clone());

				convert.AddResult(gameObj);
				convert.MarkObjectHandled(cmp);
			}

			return false;
		}
Exemplo n.º 2
0
        public override bool Convert(ConvertOperation convert)
        {
            List <Component> availData = convert.Perform <Component>().ToList();

            availData.Sort((Component a, Component b) => a.RequiresComponent(b.GetType()) ? 1 : 0);

            // Generate objects
            foreach (Component cmp in availData)
            {
                if (convert.IsObjectHandled(cmp))
                {
                    continue;
                }
                Type cmpType = cmp.GetType();

                // Create or retrieve GameObject
                GameObject gameObj = null;
                {
                    // First try to get one from the resultset that has an open slot for this kind of Component
                    if (gameObj == null)
                    {
                        gameObj = convert.Result.OfType <GameObject>().FirstOrDefault(g => g.GetComponent(cmpType) == null);
                    }
                    // Still none? Create a new GameObject
                    if (gameObj == null)
                    {
                        gameObj = new GameObject();

                        // Come up with a suitable name
                        string nameSuggestion = null;
                        {
                            // Be open for suggestions
                            if (nameSuggestion == null)
                            {
                                nameSuggestion = convert.TakeSuggestedResultName(cmp);
                            }
                            // Use a standard name
                            if (nameSuggestion == null)
                            {
                                nameSuggestion = cmpType.Name;
                            }
                        }

                        gameObj.Name = nameSuggestion;
                    }
                }

                // Make sure all requirements are met
                foreach (Type t in Component.GetRequiredComponents(cmpType))
                {
                    gameObj.AddComponent(t);
                }

                // Make sure no other Component of this Type is already added
                gameObj.RemoveComponent(cmpType);

                // Add Component
                gameObj.AddComponent(cmp.GameObj == null ? cmp : cmp.Clone());

                convert.AddResult(gameObj);
                convert.MarkObjectHandled(cmp);
            }

            return(false);
        }