下面放出NGUI 制作动态图集的详细源码
- List<Texture2D> textures=new List<Texture2D>();
- List<UIAtlas.Sprite> sprits = new List<UIAtlas.Sprite> ();
-
- for (int i=batch*batchQuantity; i<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(2048, 2048);
- 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=0; i<rects.Length ; ++i)
- {
- rects[i]=NGUIMath.ConvertToPixels(rects[i], atlas.width, atlas.height, true);
- sprits[i].inner=rects[i];
- sprits[i].outer=rects[i];
- //Logger.log(rects[i]);
- }
-
-
- Material mat = new Material("atlasMat"+batch.ToString());
- mat.shader= Shader.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 (batch, newAtlas);
- batchCompleted.Add (batch);
- batchProcessing.Remove (batch);
- }
- else{
- batchProcessing.Remove (batch);
- Logger.log("picture download failed " +batch.ToString());
- }