예제 #1
0
        public static bool ExtractAlpha(Texture2D texture2D, int x, int y, int width, int height)
        {
            if (texture2D != null && x >= 0 && y >= 0 && (x + width) <= texture2D.width && (y + height) <= texture2D.height)
            {
#if UNITY_EDITOR
                D2dHelper.MakeTextureReadable(texture2D);
#endif
                var pixels32       = texture2D.GetPixels32();
                var total          = width * height;
                var texture2DWidth = texture2D.width;

                // Replace alpha data array?
                if (AlphaData == null || AlphaData.Length != total)
                {
                    AlphaData = new byte[total];
                }

                // Copy alpha from texture
                for (var v = height - 1; v >= 0; v--)
                {
                    var aOffset = v * width;
                    var pOffset = (y + v) * texture2DWidth + x;

                    for (var h = width - 1; h >= 0; h--)
                    {
                        AlphaData[aOffset + h] = pixels32[pOffset + h].a;
                    }
                }

                return(true);
            }

            return(false);
        }