Exemple #1
0
 public AssetPool(string name, int prefer, int max, PrefabAssetInstance assetInstance)
 {
     PreferSize      = prefer;
     MaxSize         = max;
     m_assetInstance = assetInstance;
     m_cached        = new List <GameObject>(MaxSize);
     PoolNode        = new GameObject(name).transform;
     PoolNode.SetParent(AssetService.Get().PoolRootNode);
 }
 public AssetInstance(string assetPath)
 {
     if (string.IsNullOrEmpty(assetPath))
     {
         Debug.LogError("Asset path is empty");
         return;
     }
     AssetPath = string.Intern(assetPath);
     AssetService.Get().Container.Put(this);
 }
        public SpriteLoadingHandle RequestSprite(SpriteAssetAssignment assignment, string path, bool sync = false)
        {
            SpriteLoadingHandle ret = null;

            if (m_sprites.TryGetValue(path, out var spriteAsset))
            {
                if (spriteAsset.Status == AssetRefObject.AssetStatus.DESTROYED)
                {
                    m_sprites.Remove(path);
                }
                else
                {
                    if (spriteAsset.IsFinished)
                    {
                        TryApplySprite(assignment, spriteAsset);
                    }
                    else
                    {
                        ret = SpriteLoadingHandle.Create(assignment, spriteAsset, path);
                    }
                    return(ret);
                }
            }

            if (sync)
            {
                var reference = AssetService.Get().Load(path, typeof(Sprite));
                spriteAsset = reference.Asset;
                TryApplySprite(assignment, spriteAsset);
            }
            else
            {
                var loadHandle = AssetService.Get().LoadAsync(path, typeof(Sprite));
                spriteAsset = loadHandle.Asset;
                ret         = SpriteLoadingHandle.Create(assignment, spriteAsset, path);
            }

            m_sprites.Add(path, spriteAsset);
            return(ret);
        }
        public SpriteElement Request(string path)
        {
            if (m_inUsed.TryGetValue(path, out var element))
            {
                element.Trigger();
                return(element);
            }

            if (m_freeList.TryGetValue(path, out element))
            {
                m_freeList.Remove(path);
                m_inUsed.Add(path, element);
                element.Trigger();
                return(element);
            }

            var packed = true;

            if (m_notInUsed.Count > 0)
            {
                element = m_notInUsed[m_notInUsed.Count - 1];
                m_notInUsed.RemoveAt(m_notInUsed.Count - 1);
            }
            else if (m_freeList.Count > 0)
            {
                element = m_freeList.Dequeue();
            }
            else
            {
                packed  = false;
                element = new SpriteElement(null);
            }
            element.Path = path;

            if (packed)
            {
                m_inUsed[path] = element;
            }
            var loadHandle = AssetService.Get().LoadAsync(path, typeof(TextAsset));

            loadHandle.OnComplete += handle => {
                if (handle.Result.AssetStatus != AssetRefObject.AssetStatus.DONE)
                {
                    return;
                }

                var assetRef  = handle.Result;
                var textAsset = assetRef.GetTextAsset();
                var texture   = new Texture2D(m_spriteSize, m_spriteSize);
                texture.LoadImage(textAsset.bytes, false);
                assetRef.Dispose();

                if (m_spriteSize != texture.width || m_spriteSize != texture.height)
                {
                    Object.Destroy(texture);
                    Debug.LogError($"Texture {path} size : {texture.width} x {texture.height} not match request : {m_spriteSize}");
                }
                else
                {
                    if (packed)
                    {
                        var iconColors = texture.GetPixels32();
                        var rect       = element.Sprite.rect;
                        m_packTexture.SetPixels32((int)rect.x, (int)rect.y, m_spriteSize, m_spriteSize, iconColors);
                        m_packTexture.Apply();
                        Object.Destroy(texture);
                    }
                    else
                    {
                        Debug.LogWarning($"Not empty space for {path}. Create an independent sprite.");
                        var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
                        element.Sprite = sprite;
                    }
                    element.Trigger();
                }
            };
            return(element);
        }
 public AssetBundleInstance(string abPath)
 {
     ABPath = string.Intern(abPath);
     AssetService.Get().Container.PutAB(this);
     SetupDependencies(AssetBundleLoadProvider.Manifest.GetDirectDependencies(abPath));
 }