Пример #1
0
 /// <summary>
 /// Removes an existing resource handle
 /// </summary>
 /// <remarks>
 /// <para>Note that no fuzzy type matching is done; it will only remove from the list for the exact type</para>
 /// </remarks>
 public void RemoveResourceHandle <T>(ResourceHandle handle)
 {
     RemoveResourceHandle(handle, typeof(T));
 }
Пример #2
0
        //gets the best-fit, highest-priority resource by name
        private T GetResource <T>(int depth = 0, ResourceObject root = null) where T : UnityEngine.Object
        {
            //no!
            //if (ResourceHandles.ContainsKey(typeof(T)))
            //    return ((ResourceHandle<T>)GetBestResource(ResourceHandles[typeof(T)])).Resource;

            if (TypeBestResourceCache.TryGetValue(typeof(T), out var cachedValue))
            {
                return((T)cachedValue.Resource); //TODO check for null? necessary for redirects I think
            }
            //explore for type to make sure we have that
            ExploreForType <T>();

            //then scan and get the highest priority of assignable types
            List <ResourceHandle> possibleResourceHandles = new List <ResourceHandle>();

            foreach (var kvp in ResourceHandleLists)
            {
                if (typeof(T).IsAssignableFrom(kvp.Key) && kvp.Value.Count > 0)
                {
                    possibleResourceHandles.Add(GetBestResource(kvp.Value));
                }
            }

            if (possibleResourceHandles.Count == 0)
            {
                return(null);
            }

            if (possibleResourceHandles.Count == 1)
            {
                return((T)possibleResourceHandles[0].Resource);
            }

            ResourceHandle bestResourceHandle = null;
            T bestRedirectedResource          = null;

            foreach (var rHandle in possibleResourceHandles)
            {
                if (bestResourceHandle == null || rHandle.Priority > bestResourceHandle.Priority || (rHandle.Priority == bestResourceHandle.Priority && rHandle.HandleID > bestResourceHandle.HandleID))
                {
                    if (rHandle is IRedirectHandle irh)
                    {
                        object redirectedResource = irh.GetRedirectedResource(false, depth, root ?? this);
                        if (redirectedResource.Ref() == null)
                        {
                            continue; //abort
                        }
                        else
                        {
                            bestRedirectedResource = (T)redirectedResource;
                            bestResourceHandle     = rHandle;
                            continue;
                        }
                    }
                    bestRedirectedResource = null;
                    bestResourceHandle     = rHandle;
                }
            }

            if (bestResourceHandle != null)
            {
                if (!(bestResourceHandle is IRedirectHandle))
                {
                    TypeBestResourceCache.Add(typeof(T), bestResourceHandle);
                }

                if (bestRedirectedResource != null)
                {
                    return(bestRedirectedResource);
                }

                return((T)bestResourceHandle.Resource);
            }

            return(null);
        }