/// <summary> /// 异步实例化资源。 /// </summary> /// <param name="assetName">要实例化资源的名称。</param> /// <param name="instantiateAssetCallbacks">实例化资源回调函数集。</param> /// <param name="userData">用户自定义数据。</param> public void InstantiateAsset(string assetName, InstantiateAssetCallbacks instantiateAssetCallbacks, object userData) { if (string.IsNullOrEmpty(assetName)) { throw new GameFrameworkException("Asset name is invalid."); } if (instantiateAssetCallbacks == null) { throw new GameFrameworkException("Instantiate asset callbacks is invalid."); } m_ResourceLoader.InstantiateAsset(assetName, instantiateAssetCallbacks, userData); }
/// <summary> /// 异步实例化资源。 /// </summary> /// <param name="assetName">要实例化资源的名称。</param> /// <param name="instantiateAssetCallbacks">实例化资源回调函数集。</param> /// <param name="userData">用户自定义数据。</param> public void InstantiateAsset(string assetName, InstantiateAssetCallbacks instantiateAssetCallbacks, object userData) { ResourceInfo?resourceInfo = null; string[] dependencyAssetNames = null; string[] scatteredDependencyAssetNames = null; string resourceChildName = null; if (!CheckAsset(assetName, out resourceInfo, out dependencyAssetNames, out scatteredDependencyAssetNames, out resourceChildName)) { string errorMessage = string.Format("Can not instantiate asset '{0}'.", assetName); if (instantiateAssetCallbacks.InstantiateAssetFailureCallback != null) { instantiateAssetCallbacks.InstantiateAssetFailureCallback(assetName, LoadResourceStatus.NotReady, errorMessage, userData); return; } throw new GameFrameworkException(errorMessage); } InstantiateAssetTask mainTask = new InstantiateAssetTask(assetName, resourceInfo.Value, dependencyAssetNames, scatteredDependencyAssetNames, resourceChildName, instantiateAssetCallbacks, userData); foreach (string dependencyAssetName in dependencyAssetNames) { if (!LoadDependencyAsset(dependencyAssetName, mainTask, userData)) { string errorMessage = string.Format("Can not load dependency asset '{0}' when instantiate asset '{1}'.", dependencyAssetName, assetName); if (instantiateAssetCallbacks.InstantiateAssetFailureCallback != null) { instantiateAssetCallbacks.InstantiateAssetFailureCallback(assetName, LoadResourceStatus.DependencyError, errorMessage, userData); return; } throw new GameFrameworkException(errorMessage); } } m_TaskPool.AddTask(mainTask); }