Example #1
0
		public void RecycleAsset (GameObject asset)
		{
			AssetInfo assetInfo = asset.GetComponent<AssetInfo>();

			if (assetInfo != null)
			{
				AssetPool pool = null;
				assetPoolDic.TryGetValue(assetInfo.type, out pool);

				if (pool != null)
				{
					pool.Add(assetInfo);
				}
				else
				{
					Debug.LogError("Asset can't find its pool: " + asset.ToString() + " of type: " + assetInfo.type);
				}
			}
			else
			{
				Debug.LogError("Wrong asset to recycle: " + asset.ToString());
			}
		}
        /// <summary>
        /// Retrieves a component from the current GameObject which matches with the method inparameter.
        /// The method tries to find out which of the objects components the method belongs to and returns it.
        /// </summary>
        /// <param name="currentObject">The object the method is connected to.</param>
        /// <param name="methodName">The name of the method which needs to be found and invoked.</param>
        /// <param name="inparameters">Optional inparameters for the method.</param>
        private Component GetAssemblyComponent(GameObject currentObject, string methodName, object[] inparameters)
        {
            Component[] objectComponentList = currentObject.GetComponents(typeof(MonoBehaviour));
            int componentObjectCount = objectComponentList.Length;

            Assembly[] referencedAssemblies = System.AppDomain.CurrentDomain.GetAssemblies();
            int referenceAssemblyObjectCount = referencedAssemblies.Length;

            for (int i = 0; i < componentObjectCount; ++i)
            {
                if (objectComponentList[i] == null)
                    continue;

                System.Type componentType = objectComponentList[i].GetType();
                string componentNameString = componentType.ToString();

                for (int assemblyObjectIterator = 0; assemblyObjectIterator < referenceAssemblyObjectCount; ++assemblyObjectIterator)
                {
                    System.Type assemblyComponentType = referencedAssemblies[assemblyObjectIterator].GetType(componentNameString);
                    if (assemblyComponentType != null && methodName != string.Empty)
                    {
                        MethodInfo foundMethodObject = null;
                        try
                        {
                            foundMethodObject = assemblyComponentType.GetMethod(methodName);
                        }
                        catch
                        {
                            Debug.LogWarning(this + " - Multiple methods with the same name (" + methodName + ") was found. The method can not choose which of these methods to get. Please recheck the mentod names.");
                        }

                        if (foundMethodObject != null)
                            return objectComponentList[i];
                    }
                }
            }

            StringBuilder errorString = new StringBuilder();
            errorString.Append(this.ToString());
            errorString.Append(" - ");
            errorString.Append(this.gameObject.ToString());
            errorString.Append(" - No valid component was found for '");
            errorString.Append(currentObject.ToString());
            errorString.Append("' with the method '");
            errorString.Append(methodName.ToString());
            errorString.Append("'.");
            errorString.Append("Make sure the method in the affect script is set as public, and not private.");
            Debug.LogError(errorString.ToString());
            return null;
        }
        /// <summary>
        /// Returns and adds a object to the existing object pool.
        /// </summary>
        /// <param name="objectType">The search type you want to search for existing object pool.</param>
        /// <param name="currentGameObject">GameObject to search for.</param>
        /// <returns>True if the object was successfully added to the object pool. False otherwise.</returns>
        public bool ReturnObjectToObjectPool(GetObjectByType objectType, GameObject currentGameObject)
        {
            if (currentGameObject == null)
                return false;

            int internalListCount	= ObjectPoolList.Count;
            int index				= 0;
            while(index < internalListCount)
            {
                string currentObjectTypeString = string.Empty;
                string returningObjectString = string.Empty;

                if(ObjectPoolList[index] != null)
                {
                    switch (objectType)
                    {
                    case GetObjectByType.Tag:
                        currentObjectTypeString = ObjectPoolList[index].tag;
                        returningObjectString = currentGameObject.tag;
                        break;
                    case GetObjectByType.Name:
                        currentObjectTypeString = ObjectPoolList[index].name;
                        returningObjectString = currentGameObject.name;
                        break;
                    case GetObjectByType.GameObject:
                        currentObjectTypeString = ObjectPoolList[index].ToString();
                        returningObjectString = currentGameObject.ToString();
                        break;
                    default:
                        return false;
                    }

                    if (string.Equals(currentObjectTypeString, returningObjectString) == true)
                    {
                        if(currentGameObject != null)
                        {
                            DeactivateObject(currentGameObject);
                            m_internalObjectPool[index].Add(currentGameObject);
                            return true;
                        }
                    }
                }
                index += 1;
            }

            return false;
        }