示例#1
0
        private List <SpriteEntry> CreateSprites(List <Texture> textures)
        {
            var list = new List <SpriteEntry>();

            foreach (var item in textures)
            {
                var texture = item as Texture2D;

                if (texture == null)
                {
                    continue;
                }

                var sprite = new SpriteEntry();

                sprite.SetRect(0, 0, texture.width, texture.height);
                sprite.texture          = texture;
                sprite.name             = texture.name;
                sprite.guid             = UnityEditorUtility.GetAssetGUID(texture);
                sprite.temporaryTexture = false;

                list.Add(sprite);
            }

            return(list);
        }
示例#2
0
    /// <summary>
    /// Extract the specified sprite from the atlas texture.
    /// </summary>

    static SpriteEntry ExtractSprite(UISpriteData es, Color32[] oldPixels, Color32[] oldAlphaPixels, int oldWidth, int oldHeight)
    {
        int xmin      = Mathf.Clamp(es.x, 0, oldWidth);
        int ymin      = Mathf.Clamp(es.y, 0, oldHeight);
        int xmax      = Mathf.Min(xmin + es.width, oldWidth - 1);
        int ymax      = Mathf.Min(ymin + es.height, oldHeight - 1);
        int newWidth  = Mathf.Clamp(es.width, 0, oldWidth);
        int newHeight = Mathf.Clamp(es.height, 0, oldHeight);

        if (newWidth == 0 || newHeight == 0)
        {
            return(null);
        }

        Color32[] newPixels = new Color32[newWidth * newHeight];

        for (int y = 0; y < newHeight; ++y)
        {
            int cy = ymin + y;
            if (cy > ymax)
            {
                cy = ymax;
            }

            for (int x = 0; x < newWidth; ++x)
            {
                int cx = xmin + x;
                if (cx > xmax)
                {
                    cx = xmax;
                }

                int newIndex = (newHeight - 1 - y) * newWidth + x;
                int oldIndex = (oldHeight - 1 - cy) * oldWidth + cx;

                if (oldAlphaPixels == null)
                {
                    newPixels[newIndex] = oldPixels[oldIndex];
                }
                else
                {
                    Color32 color = oldPixels[oldIndex];
                    color.a             = oldAlphaPixels[oldIndex].r;
                    newPixels[newIndex] = color;
                }
            }
        }

        // Create a new sprite
        SpriteEntry sprite = new SpriteEntry();

        sprite.CopyFrom(es);
        sprite.SetRect(0, 0, newWidth, newHeight);
        sprite.SetTexture(newPixels, newWidth, newHeight);
        return(sprite);
    }
示例#3
0
        private SpriteEntry ExtractSprite(SpriteData es, Color32[] oldPixels, int oldWidth, int oldHeight)
        {
            var xmin      = Mathf.Clamp(es.x, 0, oldWidth);
            var ymin      = Mathf.Clamp(es.y, 0, oldHeight);
            var xmax      = Mathf.Min(xmin + es.width, oldWidth - 1);
            var ymax      = Mathf.Min(ymin + es.height, oldHeight - 1);
            var newWidth  = Mathf.Clamp(es.width, 0, oldWidth);
            var newHeight = Mathf.Clamp(es.height, 0, oldHeight);

            if (newWidth == 0 || newHeight == 0)
            {
                return(null);
            }

            var newPixels = new Color32[newWidth * newHeight];

            for (int y = 0; y < newHeight; ++y)
            {
                int cy = ymin + y;
                if (cy > ymax)
                {
                    cy = ymax;
                }

                for (int x = 0; x < newWidth; ++x)
                {
                    int cx = xmin + x;
                    if (cx > xmax)
                    {
                        cx = xmax;
                    }

                    int newIndex = (newHeight - 1 - y) * newWidth + x;
                    int oldIndex = (oldHeight - 1 - cy) * oldWidth + cx;

                    newPixels[newIndex] = oldPixels[oldIndex];
                }
            }

            var sprite = new SpriteEntry();

            sprite.CopyFrom(es);
            sprite.SetRect(0, 0, newWidth, newHeight);
            sprite.SetTexture(newPixels, newWidth, newHeight);

            return(sprite);
        }
示例#4
0
    /// <summary>
    /// Create a list of sprites using the specified list of textures.
    /// </summary>

    static public List <SpriteEntry> CreateSprites(List <Texture> textures)
    {
        List <SpriteEntry> list = new List <SpriteEntry>();

        foreach (Texture tex in textures)
        {
            // 太慢,加速
            //Texture2D oldTex = NGUIEditorTools.ImportTexture(tex, true, false, true);
            Texture2D oldTex = tex as Texture2D;

            if (oldTex == null)
            {
                oldTex = tex as Texture2D;
            }
            if (oldTex == null)
            {
                continue;
            }

            // If we aren't doing trimming, just use the texture as-is
            if (!NGUISettings.atlasTrimming && !NGUISettings.atlasPMA)
            {
                SpriteEntry sprite = new SpriteEntry();
                sprite.SetRect(0, 0, oldTex.width, oldTex.height);
                sprite.tex              = oldTex;
                sprite.name             = oldTex.name;
                sprite.temporaryTexture = false;
                list.Add(sprite);
                continue;
            }

            // If we want to trim transparent pixels, there is more work to be done
            Color32[] pixels = oldTex.GetPixels32();

            int xmin      = oldTex.width;
            int xmax      = 0;
            int ymin      = oldTex.height;
            int ymax      = 0;
            int oldWidth  = oldTex.width;
            int oldHeight = oldTex.height;

            // Find solid pixels
            if (NGUISettings.atlasTrimming)
            {
                for (int y = 0, yw = oldHeight; y < yw; ++y)
                {
                    for (int x = 0, xw = oldWidth; x < xw; ++x)
                    {
                        Color32 c = pixels[y * xw + x];

                        if (c.a != 0)
                        {
                            if (y < ymin)
                            {
                                ymin = y;
                            }
                            if (y > ymax)
                            {
                                ymax = y;
                            }
                            if (x < xmin)
                            {
                                xmin = x;
                            }
                            if (x > xmax)
                            {
                                xmax = x;
                            }
                        }
                    }
                }
            }
            else
            {
                xmin = 0;
                xmax = oldWidth - 1;
                ymin = 0;
                ymax = oldHeight - 1;
            }

            int newWidth  = (xmax - xmin) + 1;
            int newHeight = (ymax - ymin) + 1;

            if (newWidth > 0 && newHeight > 0)
            {
                SpriteEntry sprite = new SpriteEntry();
                sprite.x      = 0;
                sprite.y      = 0;
                sprite.width  = oldTex.width;
                sprite.height = oldTex.height;

                // If the dimensions match, then nothing was actually trimmed
                if (!NGUISettings.atlasPMA && (newWidth == oldWidth && newHeight == oldHeight))
                {
                    sprite.tex              = oldTex;
                    sprite.name             = oldTex.name;
                    sprite.temporaryTexture = false;
                }
                else
                {
                    // Copy the non-trimmed texture data into a temporary buffer
                    Color32[] newPixels = new Color32[newWidth * newHeight];

                    for (int y = 0; y < newHeight; ++y)
                    {
                        for (int x = 0; x < newWidth; ++x)
                        {
                            int newIndex = y * newWidth + x;
                            int oldIndex = (ymin + y) * oldWidth + (xmin + x);
                            if (NGUISettings.atlasPMA)
                            {
                                newPixels[newIndex] = NGUITools.ApplyPMA(pixels[oldIndex]);
                            }
                            else
                            {
                                newPixels[newIndex] = pixels[oldIndex];
                            }
                        }
                    }

                    // Create a new texture
                    sprite.name = oldTex.name;
                    sprite.SetTexture(newPixels, newWidth, newHeight);

                    // Remember the padding offset
                    sprite.SetPadding(xmin, ymin, oldWidth - newWidth - xmin, oldHeight - newHeight - ymin);
                }
                list.Add(sprite);
            }
        }
        return(list);
    }
示例#5
0
	/// <summary>
	/// Extract the specified sprite from the atlas texture.
	/// </summary>

	static SpriteEntry ExtractSprite (UISpriteData es, Color32[] oldPixels, int oldWidth, int oldHeight)
	{
		int xmin = Mathf.Clamp(es.x, 0, oldWidth);
		int ymin = Mathf.Clamp(es.y, 0, oldHeight);
		int xmax = Mathf.Min(xmin + es.width, oldWidth - 1);
		int ymax = Mathf.Min(ymin + es.height, oldHeight - 1);
		int newWidth = Mathf.Clamp(es.width, 0, oldWidth);
		int newHeight = Mathf.Clamp(es.height, 0, oldHeight);

		if (newWidth == 0 || newHeight == 0) return null;

		Color32[] newPixels = new Color32[newWidth * newHeight];

		for (int y = 0; y < newHeight; ++y)
		{
			int cy = ymin + y;
			if (cy > ymax) cy = ymax;

			for (int x = 0; x < newWidth; ++x)
			{
				int cx = xmin + x;
				if (cx > xmax) cx = xmax;

				int newIndex = (newHeight - 1 - y) * newWidth + x;
				int oldIndex = (oldHeight - 1 - cy) * oldWidth + cx;

				newPixels[newIndex] = oldPixels[oldIndex];
			}
		}

		// Create a new sprite
		SpriteEntry sprite = new SpriteEntry();
		sprite.CopyFrom(es);
		sprite.SetRect(0, 0, newWidth, newHeight);
		sprite.temporaryTexture = true;
		sprite.tex = new Texture2D(newWidth, newHeight);
		sprite.tex.SetPixels32(newPixels);
		sprite.tex.Apply();
		return sprite;
	}
示例#6
0
	/// <summary>
	/// Create a list of sprites using the specified list of textures.
	/// </summary>

	static public List<SpriteEntry> CreateSprites (List<Texture> textures)
	{
		List<SpriteEntry> list = new List<SpriteEntry>();

		foreach (Texture tex in textures)
		{
			Texture2D oldTex = NGUIEditorTools.ImportTexture(tex, true, false, true);
			if (oldTex == null) oldTex = tex as Texture2D;
			if (oldTex == null) continue;

			// If we aren't doing trimming, just use the texture as-is
			if (!NGUISettings.atlasTrimming && !NGUISettings.atlasPMA)
			{
				SpriteEntry sprite = new SpriteEntry();
				sprite.SetRect(0, 0, oldTex.width, oldTex.height);
				sprite.tex = oldTex;
				sprite.name = oldTex.name;
				sprite.temporaryTexture = false;
				list.Add(sprite);
				continue;
			}

			// If we want to trim transparent pixels, there is more work to be done
			Color32[] pixels = oldTex.GetPixels32();

			int xmin = oldTex.width;
			int xmax = 0;
			int ymin = oldTex.height;
			int ymax = 0;
			int oldWidth = oldTex.width;
			int oldHeight = oldTex.height;

			// Find solid pixels
			if (NGUISettings.atlasTrimming)
			{
				for (int y = 0, yw = oldHeight; y < yw; ++y)
				{
					for (int x = 0, xw = oldWidth; x < xw; ++x)
					{
						Color32 c = pixels[y * xw + x];

						if (c.a != 0)
						{
							if (y < ymin) ymin = y;
							if (y > ymax) ymax = y;
							if (x < xmin) xmin = x;
							if (x > xmax) xmax = x;
						}
					}
				}
			}
			else
			{
				xmin = 0;
				xmax = oldWidth - 1;
				ymin = 0;
				ymax = oldHeight - 1;
			}

			int newWidth  = (xmax - xmin) + 1;
			int newHeight = (ymax - ymin) + 1;

			if (newWidth > 0 && newHeight > 0)
			{
				SpriteEntry sprite = new SpriteEntry();
				sprite.x = 0;
				sprite.y = 0;
				sprite.width = oldTex.width;
				sprite.height = oldTex.height;

				// If the dimensions match, then nothing was actually trimmed
				if (!NGUISettings.atlasPMA && (newWidth == oldWidth && newHeight == oldHeight))
				{
					sprite.tex = oldTex;
					sprite.name = oldTex.name;
					sprite.temporaryTexture = false;
				}
				else
				{
					// Copy the non-trimmed texture data into a temporary buffer
					Color32[] newPixels = new Color32[newWidth * newHeight];

					for (int y = 0; y < newHeight; ++y)
					{
						for (int x = 0; x < newWidth; ++x)
						{
							int newIndex = y * newWidth + x;
							int oldIndex = (ymin + y) * oldWidth + (xmin + x);
							if (NGUISettings.atlasPMA) newPixels[newIndex] = NGUITools.ApplyPMA(pixels[oldIndex]);
							else newPixels[newIndex] = pixels[oldIndex];
						}
					}

					// Create a new texture
					sprite.temporaryTexture = true;
					sprite.name = oldTex.name;
					sprite.tex = new Texture2D(newWidth, newHeight);
					sprite.tex.SetPixels32(newPixels);
					sprite.tex.Apply();

					// Remember the padding offset
					sprite.SetPadding(xmin, ymin, oldWidth - newWidth - xmin, oldHeight - newHeight - ymin);
				}
				list.Add(sprite);
			}
		}
		return list;
	}
示例#7
0
	/// <summary>
	/// Extract sprites from the atlas, adding them to the list.
	/// </summary>

	static public void ExtractSprites (UIAtlas atlas, List<SpriteEntry> finalSprites)
	{
		// Make the atlas texture readable
		Texture2D atlasTex = NGUIEditorTools.ImportTexture(atlas.texture, true, false, !atlas.premultipliedAlpha);

		if (atlasTex != null)
		{
			Color32[] oldPixels = null;
			int oldWidth = atlasTex.width;
			int oldHeight = atlasTex.height;
			List<UISpriteData> existingSprites = atlas.spriteList;

			foreach (UISpriteData es in existingSprites)
			{
				bool found = false;

				foreach (SpriteEntry fs in finalSprites)
				{
					if (es.name == fs.name)
					{
						fs.CopyBorderFrom(es);
						found = true;
						break;
					}
				}

				if (!found)
				{
					// Read the atlas
					if (oldPixels == null) oldPixels = atlasTex.GetPixels32();

					int xmin = Mathf.Clamp(es.x, 0, oldWidth);
					int ymin = Mathf.Clamp(es.y, 0, oldHeight);
					int xmax = Mathf.Min(xmin + es.width, oldWidth - 1);
					int ymax = Mathf.Min(ymin + es.height, oldHeight - 1);
					int newWidth = Mathf.Clamp(es.width, 0, oldWidth);
					int newHeight = Mathf.Clamp(es.height, 0, oldHeight);

					if (newWidth == 0 || newHeight == 0) continue;

					Color32[] newPixels = new Color32[newWidth * newHeight];

					for (int y = 0; y < newHeight; ++y)
					{
						int cy = ymin + y;
						if (cy > ymax) cy = ymax;

						for (int x = 0; x < newWidth; ++x)
						{
							int cx = xmin + x;
							if (cx > xmax) cx = xmax;

							int newIndex = (newHeight - 1 - y) * newWidth + x;
							int oldIndex = (oldHeight - 1 - cy) * oldWidth + cx;

							newPixels[newIndex] = oldPixels[oldIndex];
						}
					}

					// Create a new sprite
					SpriteEntry sprite = new SpriteEntry();
					sprite.CopyFrom(es);
					sprite.SetRect(0, 0, newWidth, newHeight);
					sprite.temporaryTexture = true;
					sprite.tex = new Texture2D(newWidth, newHeight);
					sprite.tex.SetPixels32(newPixels);
					sprite.tex.Apply();
					finalSprites.Add(sprite);
				}
			}
		}

		// The atlas no longer needs to be readable
		NGUIEditorTools.ImportTexture(atlas.texture, false, false, !atlas.premultipliedAlpha);
	}
    /// <summary>
    /// Extract sprites from the atlas, adding them to the list.
    /// </summary>

    static void ExtractSprites(UIAtlas atlas, List <SpriteEntry> finalSprites)
    {
        // Make the atlas texture readable
        Texture2D atlasTex = NGUIEditorTools.ImportTexture(atlas.texture, true, false, !atlas.premultipliedAlpha);

        if (atlasTex != null)
        {
            Color32[]           oldPixels       = null;
            int                 oldWidth        = atlasTex.width;
            int                 oldHeight       = atlasTex.height;
            List <UISpriteData> existingSprites = atlas.spriteList;

            foreach (UISpriteData es in existingSprites)
            {
                bool found = false;

                foreach (SpriteEntry fs in finalSprites)
                {
                    if (es.name == fs.name)
                    {
                        fs.CopyBorderFrom(es);
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    // Read the atlas
                    if (oldPixels == null)
                    {
                        oldPixels = atlasTex.GetPixels32();
                    }

                    int xmin      = Mathf.Clamp(es.x, 0, oldWidth);
                    int ymin      = Mathf.Clamp(es.y, 0, oldHeight);
                    int newWidth  = Mathf.Clamp(es.width, 0, oldWidth);
                    int newHeight = Mathf.Clamp(es.height, 0, oldHeight);
                    if (newWidth == 0 || newHeight == 0)
                    {
                        continue;
                    }

                    Color32[] newPixels = new Color32[newWidth * newHeight];

                    for (int y = 0; y < newHeight; ++y)
                    {
                        for (int x = 0; x < newWidth; ++x)
                        {
                            int newIndex = (newHeight - 1 - y) * newWidth + x;
                            int oldIndex = (oldHeight - 1 - (ymin + y)) * oldWidth + (xmin + x);
                            newPixels[newIndex] = oldPixels[oldIndex];
                        }
                    }

                    // Create a new sprite
                    SpriteEntry sprite = new SpriteEntry();
                    sprite.CopyFrom(es);
                    sprite.SetRect(0, 0, newWidth, newHeight);
                    sprite.temporaryTexture = true;
                    sprite.tex = new Texture2D(newWidth, newHeight);
                    sprite.tex.SetPixels32(newPixels);
                    sprite.tex.Apply();
                    finalSprites.Add(sprite);
                }
            }
        }

        // The atlas no longer needs to be readable
        NGUIEditorTools.ImportTexture(atlas.texture, false, false, !atlas.premultipliedAlpha);
    }
示例#9
0
    /// <summary>
    /// Extract sprites from the atlas, adding them to the list.
    /// </summary>
    static void ExtractSprites(UIAtlas atlas, List<SpriteEntry> sprites)
    {
        // Make the atlas texture readable
        Texture2D atlasTex = NGUIEditorTools.ImportTexture(atlas.texture, true, false, !atlas.premultipliedAlpha);

        if (atlasTex != null)
        {
            Color32[] oldPixels = null;
            int oldWidth = atlasTex.width;
            int oldHeight = atlasTex.height;
            List<UISpriteData> list = atlas.spriteList;

            foreach (UISpriteData asp in list)
            {
                bool found = false;

                foreach (SpriteEntry se in sprites)
                {
                    if (asp.name == se.name)
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    // Read the atlas
                    if (oldPixels == null) oldPixels = atlasTex.GetPixels32();

                    int xmin = Mathf.Clamp(asp.x, 0, oldWidth);
                    int ymin = Mathf.Clamp(asp.y, 0, oldHeight);
                    int newWidth = Mathf.Clamp(asp.width, 0, oldWidth);
                    int newHeight = Mathf.Clamp(asp.height, 0, oldHeight);
                    if (newWidth == 0 || newHeight == 0) continue;

                    Color32[] newPixels = new Color32[newWidth * newHeight];

                    for (int y = 0; y < newHeight; ++y)
                    {
                        for (int x = 0; x < newWidth; ++x)
                        {
                            int newIndex = (newHeight - 1 - y) * newWidth + x;
                            int oldIndex = (oldHeight - 1 - (ymin + y)) * oldWidth + (xmin + x);
                            newPixels[newIndex] = oldPixels[oldIndex];
                        }
                    }

                    // Create a new sprite
                    SpriteEntry sprite = new SpriteEntry();
                    sprite.CopyFrom(asp);
                    sprite.SetRect(0, 0, newWidth, newHeight);
                    sprite.temporaryTexture = true;
                    sprite.tex = new Texture2D(newWidth, newHeight);
                    sprite.tex.SetPixels32(newPixels);
                    sprite.tex.Apply();
                    sprites.Add(sprite);
                }
            }
        }

        // The atlas no longer needs to be readable
        NGUIEditorTools.ImportTexture(atlas.texture, false, false, !atlas.premultipliedAlpha);
    }