using System; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine.ResourceManagement.AsyncOperations; using UnityEngine.ResourceManagement.ResourceLocations; using UnityEngine.ResourceManagement.Util; namespace UnityEngine.ResourceManagement.ResourceProviders { /// /// Base class for IResourceProvider. /// public abstract class ResourceProviderBase : IResourceProvider, IInitializableObject { /// /// The unique identifier of the provider. /// protected string m_ProviderId; /// /// The extra behavior of the provider. /// protected ProviderBehaviourFlags m_BehaviourFlags = ProviderBehaviourFlags.None; /// public virtual string ProviderId { get { if (string.IsNullOrEmpty(m_ProviderId)) m_ProviderId = GetType().FullName; return m_ProviderId; } } /// public virtual bool Initialize(string id, string data) { m_ProviderId = id; return !string.IsNullOrEmpty(m_ProviderId); } /// public virtual bool CanProvide(Type t, IResourceLocation location) { return GetDefaultType(location).IsAssignableFrom(t); } /// /// Converts information about the resource provider to a formatted string. /// /// Returns information about the resource provider. public override string ToString() { return ProviderId; } /// /// Release the specified object that was created from the specified location. /// /// The location of the object /// The object to release. public virtual void Release(IResourceLocation location, object obj) { } /// /// Get the default type of object that this provider can provide. /// /// The Resource location that points to the asset you want the default type of. /// The default Type of of the object if one is found, null otherwise. public virtual Type GetDefaultType(IResourceLocation location) { return typeof(object); } /// /// Provide the object specified in the provideHandle. /// /// Contains all data needed to provide the requested object. public abstract void Provide(ProvideHandle provideHandle); /// public virtual AsyncOperationHandle InitializeAsync(ResourceManager rm, string id, string data) { BaseInitAsyncOp baseInitOp = new BaseInitAsyncOp(); baseInitOp.Init(() => Initialize(id, data)); return rm.StartOperation(baseInitOp, default); } ProviderBehaviourFlags IResourceProvider.BehaviourFlags { get { return m_BehaviourFlags; } } class BaseInitAsyncOp : AsyncOperationBase { private Func m_CallBack; public void Init(Func callback) { m_CallBack = callback; } /// protected override bool InvokeWaitForCompletion() { m_RM?.Update(Time.unscaledDeltaTime); if (!HasExecuted) InvokeExecute(); return true; } protected override void Execute() { if (m_CallBack != null) Complete(m_CallBack(), true, ""); else Complete(true, true, ""); } } } /// /// Contains options used in Resource Provider load requests. ProviderLoadRequestOptions are used to specify /// parameters such as whether or not to ignore load failures and UnityWebRequest timeouts. /// [Serializable] public class ProviderLoadRequestOptions { [SerializeField] private bool m_IgnoreFailures = false; [SerializeField] private int m_WebRequestTimeout = 0; [SerializeField] private string m_LocalCachePath; /// /// Provides serialization and deserialization support for objects /// using the format. /// public class SerializationAdatapter : BinaryStorageBuffer.ISerializationAdapter { IEnumerable BinaryStorageBuffer.ISerializationAdapter.Dependencies => null; struct Data { public bool ignoreFailures; public int requestTimeout; public uint localCachePathOffset; } object BinaryStorageBuffer.ISerializationAdapter.Deserialize(BinaryStorageBuffer.Reader reader, Type t, uint offset, out uint size) { ProviderLoadRequestOptions opts = new ProviderLoadRequestOptions(); var data = reader.ReadValue(offset, out var dataSize); opts.m_IgnoreFailures = data.ignoreFailures; opts.m_WebRequestTimeout = data.requestTimeout; opts.LocalCachePath = reader.ReadString(data.localCachePathOffset, out var lcpSize, '/', false); size = dataSize + lcpSize; return opts; } uint BinaryStorageBuffer.ISerializationAdapter.Serialize(BinaryStorageBuffer.Writer writer, object val) { var opts = val as ProviderLoadRequestOptions; var data = new Data { ignoreFailures = opts.m_IgnoreFailures, requestTimeout = opts.m_WebRequestTimeout, localCachePathOffset = writer.WriteString(opts.m_LocalCachePath, '/') }; return writer.Write(data); } } /// /// Creates a memberwise clone of a given ProviderLoadRequestOption. /// /// The newly created ProviderLoadRequestOption object public ProviderLoadRequestOptions Copy() { return (ProviderLoadRequestOptions)this.MemberwiseClone(); } /// /// IgnoreFailures for provider load requests /// public bool IgnoreFailures { get { return m_IgnoreFailures; } set { m_IgnoreFailures = value; } } /// /// UnityWebRequest Timeout /// public int WebRequestTimeout { get => m_WebRequestTimeout; set => m_WebRequestTimeout = value; } /// /// Local cache path, relative to Application.temporaryCachePath /// public string LocalCachePath { get => m_LocalCachePath; set => m_LocalCachePath = value; } } }