Esempio n. 1
0
        public I76Sprite GetSprite(string mapName, string spriteName, int xOffset = 0, int yOffset = 0, bool addToCache = true)
        {
            if (addToCache && _sprites.TryGetValue(spriteName, out I76Sprite sprite))
            {
                return(sprite);
            }

            if (!TryGetMapTexture(mapName, out Texture2D mapTexture, out ETbl etbl))
            {
                return(null);
            }

            if (etbl.IsReferenceImage)
            {
                Debug.LogErrorFormat("Map '{0}' is a reference image, not a sprite sheet.", mapName);
                return(null);
            }

            if (!etbl.Items.TryGetValue(spriteName, out ETbl.ETblItem item))
            {
                Debug.LogErrorFormat("Sprite '{0}' does not exist in map '{1}'.", spriteName, mapName);
                return(null);
            }

            int textureWidth = mapTexture.width;
            int itemWidth    = item.Width;
            int mapYOffset   = Mathf.Clamp(mapTexture.height - item.Height - item.YOffset - item.Height * yOffset + 1, 0, mapTexture.height - item.Height);

            Color32[] texturePixels = mapTexture.GetPixels32(0);
            Color32[] pixels        = new Color32[item.Width * item.Height];
            int       xStart        = item.XOffset + item.Width * xOffset;
            int       yStart        = mapYOffset;
            int       xEnd          = xStart + item.Width;
            int       yEnd          = yStart + item.Height;

            for (int x = xStart; x < xEnd; ++x)
            {
                for (int y = yStart; y < yEnd; ++y)
                {
                    pixels[(y - yStart) * itemWidth + x - xStart] = texturePixels[y * textureWidth + x];
                }
            }

            sprite = new I76Sprite
            {
                Name   = spriteName,
                Pixels = pixels,
                Width  = item.Width,
                Height = item.Height
            };

            if (addToCache)
            {
                _sprites.Add(spriteName, sprite);
            }

            return(sprite);
        }
Esempio n. 2
0
        public void ApplySprite(string referenceId, I76Sprite sprite, bool uploadToGpu, bool alphaBlend = false)
        {
            if (sprite == null)
            {
                return;
            }

            Vector2Int referencePos = Vector2Int.zero;

            if (referenceId != null)
            {
                if (!_referencePositions.TryGetValue(referenceId, out referencePos))
                {
                    Debug.LogErrorFormat("Reference ID '{0}' does not exist for reference image '{1}'.", referenceId, Name);
                    return;
                }
            }

            int xOffset = referencePos.x;
            int yOffset = Mathf.Max(0, MainTexture.height - referencePos.y - sprite.Height - 1);

            Color32[] pixels = sprite.Pixels;
            if (alphaBlend)
            {
                Color[] existingPixels = MainTexture.GetPixels(xOffset, yOffset, sprite.Width, sprite.Height);
                int     pixelCount     = existingPixels.Length;
                for (int i = 0; i < pixelCount; ++i)
                {
                    pixels[i] = Color32.Lerp(existingPixels[i], pixels[i], pixels[i].a);
                }
            }

            MainTexture.SetPixels32(xOffset, yOffset, sprite.Width, sprite.Height, pixels, 0);

            if (uploadToGpu)
            {
                MainTexture.Apply();
            }
        }