예제 #1
0
        public static void AddTexturesToAtlas(UITextureAtlas atlas, Texture2D[] newTextures)
        {
            Texture2D[] textures = new Texture2D[atlas.count + newTextures.Length];

            for (int i = 0; i < atlas.count; i++)
            {
                Texture2D texture2D = atlas.sprites[i].texture;
                texture2D        = texture2D.TryMakeReadable();
                textures[i]      = texture2D;
                textures[i].name = atlas.sprites[i].name;
            }

            for (int i = 0; i < newTextures.Length; i++)
            {
                textures[atlas.count + i] = newTextures[i];
            }

            Rect[] regions = atlas.texture.PackTextures(textures, atlas.padding, 4096, false);

            atlas.sprites.Clear();

            for (int i = 0; i < textures.Length; i++)
            {
                UITextureAtlas.SpriteInfo spriteInfo = atlas[textures[i].name];
                atlas.sprites.Add(new UITextureAtlas.SpriteInfo {
                    texture = textures[i],
                    name    = textures[i].name,
                    border  = spriteInfo?.border ?? new RectOffset(),
                    region  = regions[i],
                });
            }

            atlas.RebuildIndexes();
        }
예제 #2
0
        private void AddTexturesInAtlas(UITextureAtlas atlas, Texture2D[] newTextures)
        {
            Texture2D[] textures = new Texture2D[atlas.count + newTextures.Length];

            for (int i = 0; i < atlas.count; i++)
            {
                // Locked textures workaround
                Texture2D texture2D = atlas.sprites[i].texture;

                if (texture2D != null)
                {
                    RenderTexture renderTexture = RenderTexture.GetTemporary(texture2D.width, texture2D.height, 0);
                    Graphics.Blit(texture2D, renderTexture);

                    RenderTexture active = RenderTexture.active;
                    texture2D            = new Texture2D(renderTexture.width, renderTexture.height);
                    RenderTexture.active = renderTexture;
                    texture2D.ReadPixels(new Rect(0f, 0f, (float)renderTexture.width, (float)renderTexture.height), 0, 0);
                    texture2D.Apply();
                    RenderTexture.active = active;

                    RenderTexture.ReleaseTemporary(renderTexture);

                    textures[i]      = texture2D;
                    textures[i].name = atlas.sprites[i].name;
                }
            }

            for (int i = 0; i < newTextures.Length; i++)
            {
                textures[atlas.count + i] = newTextures[i];
            }

            Rect[] regions = atlas.texture.PackTextures(textures, atlas.padding, 4096, false);

            atlas.sprites.Clear();

            for (int i = 0; i < textures.Length; i++)
            {
                if (textures[i] != null)
                {
                    UITextureAtlas.SpriteInfo spriteInfo = atlas[textures[i].name];
                    atlas.sprites.Add(new UITextureAtlas.SpriteInfo
                    {
                        texture = textures[i],
                        name    = textures[i].name,
                        border  = (spriteInfo != null) ? spriteInfo.border : new RectOffset(),
                        region  = regions[i]
                    });
                }
            }

            atlas.RebuildIndexes();
        }
예제 #3
0
        public static void AddTextures(Texture2D[] newTextures, bool locked = false)
        {
            createStaticAtlas();

            Texture2D[] textures = new Texture2D[m_atlas.count + newTextures.Length];

            for (int i = 0; i < m_atlas.count; i++)
            {
                Texture2D texture2D = m_atlas.sprites[i].texture;

                if (locked)
                {
                    // Locked textures workaround
                    RenderTexture renderTexture = RenderTexture.GetTemporary(texture2D.width, texture2D.height, 0);
                    Graphics.Blit(texture2D, renderTexture);

                    RenderTexture active = RenderTexture.active;
                    texture2D            = new Texture2D(renderTexture.width, renderTexture.height);
                    RenderTexture.active = renderTexture;
                    texture2D.ReadPixels(new Rect(0f, 0f, (float)renderTexture.width, (float)renderTexture.height), 0, 0);
                    texture2D.Apply();
                    RenderTexture.active = active;

                    RenderTexture.ReleaseTemporary(renderTexture);
                }

                textures[i]      = texture2D;
                textures[i].name = m_atlas.sprites[i].name;
            }

            for (int i = 0; i < newTextures.Length; i++)
            {
                textures[m_atlas.count + i] = newTextures[i];
            }

            Rect[] regions = m_atlas.texture.PackTextures(textures, m_atlas.padding, 4096, false);

            m_atlas.sprites.Clear();

            for (int i = 0; i < textures.Length; i++)
            {
                UITextureAtlas.SpriteInfo spriteInfo = m_atlas[textures[i].name];
                m_atlas.sprites.Add(new UITextureAtlas.SpriteInfo
                {
                    texture = textures[i],
                    name    = textures[i].name,
                    border  = (spriteInfo != null) ? spriteInfo.border : new RectOffset(),
                    region  = regions[i]
                });
            }

            m_atlas.RebuildIndexes();
        }
예제 #4
0
        public static void AddTexturesInAtlas(UITextureAtlas atlas, Texture2D[] newTextures, bool locked = false)
        {
            var textures = new Texture2D[atlas.count + newTextures.Length];

            for (var i = 0; i < atlas.count; i++)
            {
                var texture2D = atlas.sprites[i].texture;

                if (locked)
                {
                    // Locked textures workaround
                    var renderTexture = RenderTexture.GetTemporary(texture2D.width, texture2D.height, 0);
                    Graphics.Blit(texture2D, renderTexture);

                    var active = RenderTexture.active;
                    texture2D            = new Texture2D(renderTexture.width, renderTexture.height);
                    RenderTexture.active = renderTexture;
                    texture2D.ReadPixels(new Rect(0f, 0f, renderTexture.width, renderTexture.height), 0, 0);
                    texture2D.Apply();
                    RenderTexture.active = active;

                    RenderTexture.ReleaseTemporary(renderTexture);
                }

                textures[i]      = texture2D;
                textures[i].name = atlas.sprites[i].name;
            }

            for (var i = 0; i < newTextures.Length; i++)
            {
                textures[atlas.count + i] = newTextures[i];
            }

            var regions = atlas.texture.PackTextures(textures, atlas.padding, 4096, false);

            atlas.sprites.Clear();

            for (var i = 0; i < textures.Length; i++)
            {
                var spriteInfo = atlas[textures[i].name];
                atlas.sprites.Add(new UITextureAtlas.SpriteInfo
                {
                    texture = textures[i],
                    name    = textures[i].name,
                    border  = spriteInfo != null ? spriteInfo.border : new RectOffset(),
                    region  = regions[i]
                });
            }

            atlas.RebuildIndexes();
        }
        /// <summary>
        /// Destroys contents of UITextureAtlas and instance itself
        /// </summary>
        /// <param name="atlas">instance to destroy</param>
        public static void DestroyTextureAtlasAndContents(UITextureAtlas atlas)
        {
            List <UITextureAtlas.SpriteInfo> atlasSprites = atlas.sprites;

            // destroy sprites
            for (int i = 0; i < atlasSprites.Count; i++)
            {
                Object.DestroyImmediate(atlasSprites[0].texture);
            }
            atlasSprites.Clear();
            // reset map
            atlas.RebuildIndexes();
            // destroy material texture
            Object.DestroyImmediate(atlas.texture);
            // destroy material
            Object.DestroyImmediate(atlas.material);
            // destroy atlas scriptable object
            Object.DestroyImmediate(atlas);
        }
예제 #6
0
        /// <summary>
        /// Adds a collection of textures to an atlas.
        /// </summary>
        /// <param name="atlas">Atlas to add to</param>
        /// <param name="newTextures">Textures to add</param>
        private void AddTexturesToAtlas(UITextureAtlas atlas, Texture2D[] newTextures)
        {
            Texture2D[] textures = new Texture2D[atlas.count + newTextures.Length];


            // Populate textures with sprites from the atlas.
            for (int i = 0; i < atlas.count; i++)
            {
                textures[i]      = atlas.sprites[i].texture;
                textures[i].name = atlas.sprites[i].name;
            }

            // Append new textures to our list.
            for (int i = 0; i < newTextures.Length; i++)
            {
                textures[atlas.count + i] = newTextures[i];
            }

            // Repack atlas with our new additions (regions are individual texture areas within the atlas).
            Rect[] regions = atlas.texture.PackTextures(textures, atlas.padding, 4096, false);

            // Clear original atlas sprites.
            atlas.sprites.Clear();

            // Iterate through our list, adding each sprite into the atlas.
            for (int i = 0; i < textures.Length; i++)
            {
                UITextureAtlas.SpriteInfo spriteInfo = atlas[textures[i].name];
                atlas.sprites.Add(new UITextureAtlas.SpriteInfo
                {
                    texture = textures[i],
                    name    = textures[i].name,
                    border  = (spriteInfo != null) ? spriteInfo.border : new RectOffset(),
                    region  = regions[i]
                });
            }

            // Rebuild atlas indexes.
            atlas.RebuildIndexes();
        }
        public static void RegenerateTextureAtlas(UITextureAtlas textureAtlas, List <SpriteInfo> newFiles)
        {
            IEnumerable <string> newSpritesNames = newFiles.Select(x => x.name);

            newFiles.AddRange(textureAtlas.sprites.Where(x => !newSpritesNames.Contains(x.name)));
            textureAtlas.sprites.Clear();
            textureAtlas.AddSprites(newFiles.ToArray());
            if (textureAtlas.texture == null)
            {
                textureAtlas.material.mainTexture = new Texture2D(1, 1);
                (textureAtlas.material.mainTexture as Texture2D).SetPixel(0, 0, default);
            }
            Rect[] array = textureAtlas.texture.PackTextures(textureAtlas.sprites.Select(x => x.texture).ToArray(), textureAtlas.padding, 4096 * 4);

            for (int i = 0; i < textureAtlas.count; i++)
            {
                textureAtlas.sprites[i].region = array[i];
            }
            textureAtlas.sprites.Sort();
            textureAtlas.RebuildIndexes();
            UIView.RefreshAll(false);
        }
예제 #8
0
        /**
         * Method sourced from SamsamTSs Airport Roads mod for Cities Skylines
         * Adds array of Texture2D objects to a UITextureAtlas
         * https://github.com/SamsamTS/CS-AirportRoads/blob/master/AirportRoads/AirportRoads.cs
         **/
        private static void AddTexturesInAtlas(UITextureAtlas atlas, Texture2D[] newTextures)
        {
            Texture2D[] textures = new Texture2D[atlas.count + newTextures.Length];

            for (int i = 0; i < atlas.count; i++) {
                // Locked textures workaround
                Texture2D texture2D = atlas.sprites[i].texture;

                RenderTexture renderTexture = RenderTexture.GetTemporary(texture2D.width, texture2D.height, 0);
                Graphics.Blit(texture2D, renderTexture);

                RenderTexture active = RenderTexture.active;
                texture2D = new Texture2D(renderTexture.width, renderTexture.height);
                RenderTexture.active = renderTexture;
                texture2D.ReadPixels(new Rect(0f, 0f, (float)renderTexture.width, (float)renderTexture.height), 0, 0);
                texture2D.Apply();
                RenderTexture.active = active;

                RenderTexture.ReleaseTemporary(renderTexture);

                textures[i] = texture2D;
                textures[i].name = atlas.sprites[i].name;
            }

            for (int i = 0; i < newTextures.Length; i++)
                textures[atlas.count + i] = newTextures[i];

            Rect[] regions = atlas.texture.PackTextures(textures, atlas.padding, 4096, false);

            atlas.sprites.Clear();

            for (int i = 0; i < textures.Length; i++) {
                UITextureAtlas.SpriteInfo spriteInfo = atlas[textures[i].name];
                atlas.sprites.Add(new UITextureAtlas.SpriteInfo {
                    texture = textures[i],
                    name = textures[i].name,
                    border = (spriteInfo != null) ? spriteInfo.border : new RectOffset(),
                    region = regions[i]
                });
            }

            atlas.RebuildIndexes();
        }