예제 #1
0
        private static void OnLoad(UnityEngine.Object asset, System.Object obj)
        {
            TempLoad tl = (TempLoad)obj;

            try
            {
                Texture2D tex = (Texture2D)asset;

                TexInfo texInfo = new TexInfo();
                texInfo.tex          = tex;
                texInfo.w            = (short)tex.width;
                texInfo.h            = (short)tex.height;
                tl.texInfo[tl.index] = texInfo;

                bool    over = true;
                int     i, count;
                TexInfo ti;
                for (i = 0, count = tl.texInfo.Length; i < count; i++)
                {
                    ti = tl.texInfo[i];
                    if (ti == null)
                    {
                        over = false;
                        break;
                    }
                }
                tl.over = over;

                if (over)
                {
                    if (tl.autoTemp)
                    {
                        PackCombineTexture(tl);
                    }
                    else
                    {
                        Texture2D texture = new Texture2D(1, 1);

                        Rect[] rect = CreateTex(texture, tl.texInfo);

                        ProcessCombine(tl, texture, rect);
                    }
                }
            }
            catch (System.Exception e)
            {
                for (int i = 0; i < tl.meshList.Count; i++)
                {
                    Object.Destroy(tl.meshList[i]);
                }
                if (tl.endCombine != null)
                {
                    tl.endCombine(null, tl.plus, tl.sub, tl.endParam);
                }

                Debug.LogError("combine load e->" + asset.name + "^" + e.ToString());
            }
        }
예제 #2
0
        private static void ProcessCombine(TempLoad tl, Texture2D texture, Rect[] rect)
        {
            lock (lockCombine)
            {
                int       i, count, j = 0, k = 0, count1;
                Rect      rt;
                TexInfo   ti;
                Mesh      mesh;
                Vector2[] uvs;

                for (i = 0, count = tl.meshList.Count; i < count; i++)
                {
                    rt   = rect[i];
                    ti   = tl.texInfo[i];
                    mesh = tl.meshList[i];

                    uvs    = mesh.uv;
                    count1 = uvs.Length;
                }

                try
                {
                    texture.Compress(false);
                    if (rect == null)
                    {
                        Debug.LogError("rect is null");
                        return;
                    }

                    Vector2 v2;
                    Vector2 nuv;

                    for (i = 0, count = tl.meshList.Count; i < count; i++)
                    {
                        rt   = rect[i];
                        ti   = tl.texInfo[i];
                        mesh = tl.meshList[i];

                        uvs = mesh.uv;

                        count1 = uvs.Length;
                        for (j = 0; j < count1; j++)
                        {
                            v2     = uvs[j];
                            nuv    = new Vector2(rt.xMin + v2.x * ti.w / texture.width, rt.yMin + v2.y * ti.h / texture.height);
                            uvs[j] = nuv;
                        }
                        mesh.uv = uvs;
                    }

                    SkinnedMeshRenderer r = tl.root.GetComponent <SkinnedMeshRenderer>();
                    r.sharedMesh = new Mesh();
                    r.sharedMesh.CombineMeshes(tl.combineInstances.ToArray(), true, false);
                    r.bones = tl.bones.ToArray();

                    Material material = new Material("xx");
                    material.mainTexture = texture;

                    r.materials = new Material[] { material };

                    CombineObj co = new CombineObj();
                    co.obj     = tl.root;
                    co.combine = true;
                    if (!combinedDic2.ContainsKey(tl.plus))
                    {
                        Dictionary <int, List <CombineObj> > dic = new Dictionary <int, List <CombineObj> >();
                        List <CombineObj> list = new List <CombineObj>();
                        list.Add(co);
                        dic.Add(tl.sub, list);
                        combinedDic2.Add(tl.plus, dic);
                    }
                    else
                    {
                        Dictionary <int, List <CombineObj> > dic = combinedDic2[tl.plus];
                        if (dic.ContainsKey(tl.sub))
                        {
                            dic[tl.sub].Add(co);
                        }
                        else
                        {
                            List <CombineObj> list = new List <CombineObj>();
                            list.Add(co);
                            dic.Add(tl.sub, list);
                        }
                    }

                    //add first, then avoid to del
                    if (tl.endCombine != null)
                    {
                        tl.endCombine(tl.root, tl.plus, tl.sub, tl.endParam);
                    }

                    //Mesh mesh;
                    for (i = 0, count = tl.meshList.Count; i < count; i++)
                    {
                        Object.Destroy(tl.meshList[i]);
                    }

                    for (i = 0, count = tl.texName.Length; i < count; i++)
                    {
                        ResLoader.RemoveAssetCacheByName(tl.texName[i]);
                    }
                }
                catch (System.Exception e)
                {
                    for (i = 0, count = tl.meshList.Count; i < count; i++)
                    {
                        Object.Destroy(tl.meshList[i]);
                    }

                    for (i = 0, count = tl.texName.Length; i < count; i++)
                    {
                        ResLoader.RemoveAssetCacheByName(tl.texName[i]);
                    }

                    if (tl.endCombine != null)
                    {
                        tl.endCombine(null, tl.plus, tl.sub, tl.endParam);
                    }

                    Debug.LogError("process combine error->" + e.ToString());
                }
            }
        }