private void LoadBinarySuccessCallback(string dataAssetName, byte[] dataBytes, float duration, object userData) { try { if (!m_DataProviderHelper.ReadData(m_Owner, dataAssetName, dataBytes, 0, dataBytes.Length, userData)) { throw new GameFrameworkException(Utility.Text.Format("Load data failure in data provider helper, data asset name '{0}'.", dataAssetName)); } if (m_ReadDataSuccessEventHandler != null) { ReadDataSuccessEventArgs loadDataSuccessEventArgs = ReadDataSuccessEventArgs.Create(dataAssetName, duration, userData); m_ReadDataSuccessEventHandler(this, loadDataSuccessEventArgs); ReferencePool.Release(loadDataSuccessEventArgs); } } catch (Exception exception) { if (m_ReadDataFailureEventHandler != null) { ReadDataFailureEventArgs loadDataFailureEventArgs = ReadDataFailureEventArgs.Create(dataAssetName, exception.ToString(), userData); m_ReadDataFailureEventHandler(this, loadDataFailureEventArgs); ReferencePool.Release(loadDataFailureEventArgs); return; } throw; } }
private void LoadAssetOrBinaryFailureCallback(string dataAssetName, LoadResourceStatus status, string errorMessage, object userData) { string appendErrorMessage = Utility.Text.Format("Load data failure, data asset name '{0}', status '{1}', error message '{2}'.", dataAssetName, status, errorMessage); if (m_ReadDataFailureEventHandler != null) { ReadDataFailureEventArgs loadDataFailureEventArgs = ReadDataFailureEventArgs.Create(dataAssetName, appendErrorMessage, userData); m_ReadDataFailureEventHandler(this, loadDataFailureEventArgs); ReferencePool.Release(loadDataFailureEventArgs); return; } throw new GameFrameworkException(appendErrorMessage); }
/// <summary> /// 读取数据。 /// </summary> /// <param name="dataAssetName">内容资源名称。</param> /// <param name="priority">加载数据资源的优先级。</param> /// <param name="userData">用户自定义数据。</param> public void ReadData(string dataAssetName, int priority, object userData) { if (m_ResourceManager == null) { throw new GameFrameworkException("You must set resource manager first."); } if (m_DataProviderHelper == null) { throw new GameFrameworkException("You must set data provider helper first."); } HasAssetResult result = m_ResourceManager.HasAsset(dataAssetName); switch (result) { case HasAssetResult.AssetOnDisk: case HasAssetResult.AssetOnFileSystem: m_ResourceManager.LoadAsset(dataAssetName, priority, m_LoadAssetCallbacks, userData); break; case HasAssetResult.BinaryOnDisk: m_ResourceManager.LoadBinary(dataAssetName, m_LoadBinaryCallbacks, userData); break; case HasAssetResult.BinaryOnFileSystem: int dataLength = m_ResourceManager.GetBinaryLength(dataAssetName); EnsureCachedBytesSize(dataLength); if (dataLength != m_ResourceManager.LoadBinaryFromFileSystem(dataAssetName, s_CachedBytes)) { throw new GameFrameworkException(Utility.Text.Format("Load binary '{0}' from file system with internal error.", dataAssetName)); } try { if (!m_DataProviderHelper.ReadData(m_Owner, dataAssetName, s_CachedBytes, 0, dataLength, userData)) { throw new GameFrameworkException(Utility.Text.Format("Load data failure in data provider helper, data asset name '{0}'.", dataAssetName)); } if (m_ReadDataSuccessEventHandler != null) { ReadDataSuccessEventArgs loadDataSuccessEventArgs = ReadDataSuccessEventArgs.Create(dataAssetName, 0f, userData); m_ReadDataSuccessEventHandler(this, loadDataSuccessEventArgs); ReferencePool.Release(loadDataSuccessEventArgs); } } catch (Exception exception) { if (m_ReadDataFailureEventHandler != null) { ReadDataFailureEventArgs loadDataFailureEventArgs = ReadDataFailureEventArgs.Create(dataAssetName, exception.ToString(), userData); m_ReadDataFailureEventHandler(this, loadDataFailureEventArgs); ReferencePool.Release(loadDataFailureEventArgs); return; } throw; } break; default: throw new GameFrameworkException(Utility.Text.Format("Data asset '{0}' is '{1}'.", dataAssetName, result)); } }