NGUI 制作动态图集

List<Texture2Dtextures=new List<Texture2D>();
        List<UIAtlas.Spritesprits = new List<UIAtlas.Sprite> ();

        for (int i=batch*batchQuantityi<batchQuantity*(batch+1); ++i) {

            if(i==itemGos.Count)
            {
                Logger.log("i = "+i+" itemGos = "+itemGos.Count);

                break;
            }

            UIFriendItem fi=itemGos[i].GetComponent<UIFriendItem>();
            string tempUrl=facebookGraphURL+"/"+fi.firendId+"/picture?access_token="+FacebookCenter.instance.GetAccessToken();
            if(WebTextureCache.InstantiateGlobal().ImageCache.ContainsKey(tempUrl))
            {
                if(WebTextureCache.InstantiateGlobal().ImageCache[tempUrl].width==50)
                {
                    textures.Add(WebTextureCache.InstantiateGlobal().ImageCache[tempUrl]);
                    UIAtlas.Sprite sp = new UIAtlas.Sprite ();
                    sp.name=fi.firendId;
                    sprits.Add(sp);
                }
            }
            
        }

        //
        if(textures.Count>0)
        {
            textures.Add((Texture2D)Resources.Load("Others/photoFrame"));
            UIAtlas.Sprite sp = new UIAtlas.Sprite ();
            sp.name="photoFrame";
            sprits.Add(sp);
            ///------------
            Texture2D atlas = new Texture2D(20482048);
            Rect[] rects;
            rects = atlas.PackTextures ((Texture2D[])textures.ToArray (),0,2048);
            Logger.log("the length is + "+rects.Length);

            if (rects.Length != sprits.Count) {
                Logger.log("error!! length not match ");
                    }

            for(int i=0i<rects.Length ; ++i)
            {
                rects[i]=NGUIMath.ConvertToPixels(rects[i], atlas.widthatlas.heighttrue);
                sprits[i].inner=rects[i];
                sprits[i].outer=rects[i];
                //Logger.log(rects[i]);
            }


            Material mat = new Material("atlasMat"+batch.ToString());
            mat.shaderShader.Find("Unlit/Transparent Colored");
            if (mat.shader == null) {
                Logger.logError("shader is null");
                }
            mat.mainTexture=atlas;

            var gameobject = new GameObject ("atlasBatch"+batch.ToString());
            UIAtlas newAtlas=gameobject.AddComponent<UIAtlas> ();
            newAtlas.spriteList = sprits;
            newAtlas.spriteMaterial = mat;
            //newAtlas.coordinates = UIAtlas.Coordinates.TexCoords;

            Logger.log("successfully build atlas for batch " +batch.ToString());
            //Logger.log(newAtlas.spriteMaterial.ToString());
        

            createdAtlas.Add (batchnewAtlas);
            batchCompleted.Add (batch);
            batchProcessing.Remove (batch);
        }
        else{
            batchProcessing.Remove (batch);
            Logger.log("picture download failed " +batch.ToString());
        }

posted @ 2014-02-14 14:31  火之梦魇  阅读(2959)  评论(0编辑  收藏  举报