Exemplo n.º 1
0
        /// <summary>
        /// Creates and returns a new Texture2D using the given raw image data.
        /// </summary>
        public static Texture2D TextureFormatter(ImageUtils.ImageInfo imageInfo)
        {
#if UNITY_UWP
            TextureFormat imageFormat;
            switch (imageInfo.Format)
            {
            case BitmapPixelFormat.Bgra8:
                imageFormat = TextureFormat.BGRA32;
                break;

            case BitmapPixelFormat.Rgba8:
                imageFormat = TextureFormat.RGBA32;
                break;

            default:
                // Reject image on unknown format.
                // We could also return a placeholder image.
                return(null);
            }
#else
            TextureFormat imageFormat = TextureFormat.BGRA32;
#endif

            Texture2D image = new Texture2D(imageInfo.Width, imageInfo.Height, imageFormat, false);
            image.LoadRawTextureData(imageInfo.DecompressedBytes);
            image.Apply();

            return(image);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Fetches the resource at the given index from OneDrive, formats it into the
        /// requested type using the given ContentFormatter and returns it to the callback function.
        /// </summary>
        /// <typeparam name="T">Type of the resource to create with the fetched data.</typeparam>
        /// <param name="formatter">Function to use to convert the raw data into a resource of type T.</param>
        /// <param name="callback">Function to which the created resource should be sent.</param>
        protected override async Task GetResourceAt <T>(int resourceIndex, ContentFormatter <T> formatter, Action <T> callback)
        {
            // We fetch the children for the first time
            // here since we can't do it in the constructor.
            if (ResourceIds == null)
            {
                await FetchResourceIds();
            }

            string resourceId = GetResourceIdAt(resourceIndex);

            if (string.IsNullOrWhiteSpace(resourceId))
            {
                Debug.LogError("The requested resource has a null ID.");
                return;
            }

            byte[] resourceData = await oneDriveController.GetItemContentAsync(resourceId);

            // Decompress bytes on a background thread.
            ImageUtils.ImageInfo imageInfo = await ImageUtils.DecompressImageBytes(resourceData);

            if (imageInfo.DecompressedBytes == null)
            {
                Debug.LogError("Failed to fetch the requested resource from OneDrive.");
                return;
            }

            // EventQueueManager executes the given Action on the main thread.
            // This is required for resource formatting (i.e. Texture2D.LoadImage()).
            EventQueueManager.Instance?.Execute(() =>
            {
                T resource = formatter(imageInfo);
                callback(resource);
            });
        }