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. /// /// /// 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; private int m_WebRequestTimeout = 0; /// /// 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; } } }