예제 #1
0
        //This function is used if you've made your own texture atlas and the configs just specify where the textures are
        private void UseCustomTextureAtlas()
        {
            atlas = Resources.Load <Texture2D>(config.customTextureAtlasFile);

            configs = new ConfigLoader <TextureConfig>(new[] { config.textureFolder }).AllConfigs();

            for (int i = 0; i < configs.Length; i++)
            {
                for (int j = 0; j < configs[i].textures.Length; j++)
                {
                    Rect texture = new Rect(configs[i].textures[j].xPos / (float)atlas.width, configs[i].textures[j].yPos / (float)atlas.height,
                                            configs[i].textures[j].width / (float)atlas.width, configs[i].textures[j].height / (float)atlas.height);

                    TextureCollection collection;
                    if (!textures.TryGetValue(configs[i].name, out collection))
                    {
                        collection = new TextureCollection(configs[i].name);
                        textures.Add(configs[i].name, collection);
                    }

                    int connectedTextureType = -1;
                    if (configs[i].connectedTextures)
                    {
                        connectedTextureType = configs[i].textures[j].connectedType;
                    }

                    collection.AddTexture(texture, connectedTextureType, configs[i].textures[j].weight);
                }
            }
        }
예제 #2
0
        private void LoadTextureIndex()
        {
            // If you're using a pre defined texture atlas return now, don't try to generate a new one
            if (config.useCustomTextureAtlas)
            {
                UseCustomTextureAtlas();
                return;
            }

            if (configs == null)
            {
                configs = LoadAllTextures();
            }

            List <Texture2D> individualTextures = new List <Texture2D>();

            for (int i = 0; i < configs.Length; i++)
            {
                for (int j = 0; j < configs[i].textures.Length; j++)
                {
                    //create an array of all these textures
                    individualTextures.Add(configs[i].textures[j].texture2d);
                }
            }

            // Generate atlas
            Texture2D packedTextures = new Texture2D(8192, 8192);

            Rect[] rects = packedTextures.PackTextures(individualTextures.ToArray(), config.textureAtlasPadding, 8192, false);

            // Transfer over the pixels to another texture2d because PackTextures resets the texture format and useMipMaps settings
            atlas = new Texture2D(packedTextures.width, packedTextures.height, config.textureFormat, config.useMipMaps);
            atlas.SetPixels(packedTextures.GetPixels(0, 0, packedTextures.width, packedTextures.height));
            atlas.filterMode = config.textureAtlasFiltering;

            List <Rect> repeatingTextures    = new List <Rect>();
            List <Rect> nonrepeatingTextures = new List <Rect>();

            int index = 0;

            for (int i = 0; i < configs.Length; i++)
            {
                for (int j = 0; j < configs[i].textures.Length; j++)
                {
                    Rect texture = rects[index];

                    TextureCollection collection;
                    if (!textures.TryGetValue(configs[i].name, out collection))
                    {
                        collection = new TextureCollection(configs[i].name);
                        textures.Add(configs[i].name, collection);
                    }

                    int connectedTextureType = -1;
                    if (configs[i].connectedTextures)
                    {
                        connectedTextureType = configs[i].textures[j].connectedType;
                    }


                    collection.AddTexture(texture, connectedTextureType, configs[i].textures[j].weight);

                    if (configs[i].textures[j].repeatingTexture)
                    {
                        repeatingTextures.Add(rects[index]);
                    }
                    else
                    {
                        nonrepeatingTextures.Add(rects[index]);
                    }

                    index++;
                }
            }

            uPaddingBleed.BleedEdges(atlas, config.textureAtlasPadding, repeatingTextures.ToArray(), true);
            uPaddingBleed.BleedEdges(atlas, config.textureAtlasPadding, nonrepeatingTextures.ToArray(), false);
        }
예제 #3
0
        private void LoadTextureIndex()
        {
            List <Texture2D> individualTextures = new List <Texture2D>();

            for (int i = 0; i < configs.Length; i++)
            {
                for (int j = 0; j < configs[i].textures.Length; j++)
                {
                    //create an array of all these textures
                    individualTextures.Add(configs[i].textures[j].texture2d);
                }
            }

            // Generate atlas
            Texture2D packedTextures = new Texture2D(8192, 8192);

            Rect[] rects = packedTextures.PackTextures(individualTextures.ToArray(), config.textureAtlasPadding, 8192, false);

            // Transfer over the pixels to another texture2d because PackTextures resets the texture format and useMipMaps settings
            atlas = new Texture2D(packedTextures.width, packedTextures.height, config.textureFormat, config.useMipMaps);
            atlas.SetPixels(packedTextures.GetPixels(0, 0, packedTextures.width, packedTextures.height));
            atlas.filterMode = config.textureAtlasFiltering;

            List <Rect> repeatingTextures    = new List <Rect>();
            List <Rect> nonrepeatingTextures = new List <Rect>();

            int index = 0;

            textures.Clear();
            for (int i = 0; i < configs.Length; i++)
            {
                for (int j = 0; j < configs[i].textures.Length; j++)
                {
                    var  t   = configs[i].textures[j];
                    Rect uvs = rects[index];

                    TextureCollection collection;
                    if (!textures.TryGetValue(configs[i].name, out collection))
                    {
                        collection = new TextureCollection(configs[i].name, configs[i].type);
                        textures.Add(configs[i].name, collection);
                    }

                    collection.AddTexture(uvs, t);

                    if (configs[i].textures[j].repeating)
                    {
                        repeatingTextures.Add(rects[index]);
                    }
                    else
                    {
                        nonrepeatingTextures.Add(rects[index]);
                    }

                    index++;
                }
            }

            uPaddingBleed.BleedEdges(atlas, config.textureAtlasPadding, repeatingTextures.ToArray(), true);
            uPaddingBleed.BleedEdges(atlas, config.textureAtlasPadding, nonrepeatingTextures.ToArray(), false);
        }