コード例 #1
0
        public static Color[] GetSafePixels(this Texture2D texture, Rect?crop = null)
        {
            Color[]   pixels     = null;
            Texture2D source     = texture;
            bool      isReadable = false;
            BabylonTextureImporter importTool = null;
            string srcTexturePath             = AssetDatabase.GetAssetPath(texture);

            if (srcTexturePath.IndexOf("unity_builtin_extra", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                source = texture.RenderToTexture();
            }
            else
            {
                importTool = new BabylonTextureImporter(srcTexturePath);
                isReadable = importTool.IsReadable();
                if (!isReadable)
                {
                    importTool.SetReadable();
                }
            }
            try
            {
                if (crop != null && crop.HasValue)
                {
                    pixels = source.GetPixels((int)crop.Value.x, (int)crop.Value.y, (int)crop.Value.width, (int)crop.Value.height);
                }
                else
                {
                    pixels = source.GetPixels();
                }
            }
            catch (Exception ex)
            {
                UnityEngine.Debug.LogException(ex);
            }
            finally
            {
                if (importTool != null && isReadable == false)
                {
                    importTool.ForceUpdate();
                }
            }
            return(pixels);
        }