Ejemplo n.º 1
0
    /// <summary>
    /// Load the specified imageurl, textIndex and callbackDelegate.
    /// load for nocache
    /// </summary>
    /// <param name="imageurl">Imageurl.</param>
    /// <param name="textIndex">Text index.</param>
    /// <param name="callbackDelegate">Callback delegate.</param>
    public IEnumerator Load(string imageurl, uint textIndex, DelegateSpriteCallMethod callbackDelegate)
    {
        double startTime = (double)Time.time;
        //请求WWW
        WWW www = new WWW(imageurl);

        yield return(www);

        if (www != null && string.IsNullOrEmpty(www.error))
        {
            //最多等5s...
            int maxTimeout = 5;
            while (!www.isDone)
            {
                yield return(new WaitForSeconds(1));

                maxTimeout--;
                if (maxTimeout <= 0)
                {
                    yield break;
                }
            }

            //获取Texture
            Texture2D texture = www.texture;

            //创建Sprite
            Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));

            //image.sprite = sprite;,for cache...
            if (false == dicSpriteList.ContainsKey(imageurl))
            {
                dicSpriteList.Add(imageurl, sprite);
            }

            //?...为啥不能正常回调啊啊啊...
            if (callbackDelegate != null)
            {
                callbackDelegate(sprite, textIndex);
            }
            else
            {
                ImageLoadEvent messageEventArgs = new ImageLoadEvent(imageurl, sprite);
                enqueueToMessageEventQueue(messageEventArgs);
            }

            startTime = (double)Time.time - startTime;
            Debug.Log("WWW加载用时:" + startTime);
        }
    }
Ejemplo n.º 2
0
    /// <summary>
    /// 以WWW方式进行加载,add cache..
    /// </summary>
    public void StartSpriteLoad(string imageurl, uint textIndex, DelegateSpriteCallMethod callbackDelegate)
    {
        if (dicSpriteList.ContainsKey(imageurl))
        {
            if (callbackDelegate != null)
            {
                callbackDelegate(dicSpriteList[imageurl], textIndex);
            }
            else
            {
                ImageLoadEvent messageEventArgs = new ImageLoadEvent(imageurl, dicSpriteList[imageurl]);
                enqueueToMessageEventQueue(messageEventArgs);
            }
            return;
        }

        StartCoroutine(Load(imageurl, textIndex, callbackDelegate));
    }