/// <summary> /// a function Generating automatically a Grid of Images Child of the Canvas where this script is put /// it will also change the layer to 8 so that we'll detect the fact that this image is what we want to interact with /// </summary> private void GenerateGrid() { GameObject imageSpawn = Instantiate(ImageTemplate); GetInformationFromCanvas getInfoFromCanvas = null; int row = 0; int column = 0; foreach (VideoInformation videoInfo in SceneLoader.OutputScript.ClipGroups) { if (count < rows * columns) { GameObject tile = Instantiate(imageSpawn, transform); getInfoFromCanvas = tile.transform.GetComponent <GetInformationFromCanvas>(); float posX = -rectTransformCanvas.rect.width / 2 + RectTemplate.rect.width / 2 + column * (RectTemplate.rect.width + tileSize); float posY = -rectTransformCanvas.rect.height / 2 + RectTemplate.rect.height / 2 + row * (RectTemplate.rect.height + tileSize); tile.transform.localPosition = new Vector3(posX, posY, 0); tile.layer = 8; column++; if (column == columns) { column -= columns; row++; } StartCoroutine(DownloadImage(videoInfo.ThumbnailUrl, getInfoFromCanvas)); //tile.transform.GetChild(0).GetComponent<Text>().text = videoInfo.Title; getInfoFromCanvas.SetText(videoInfo.Title); } count++; } Destroy(imageSpawn); }
/// <summary> /// This coroutine will download the image from the url provided and replace the sprite of the image /// using the GetRectTransform() method to get the rectTransform of the image and resizing it /// and changing it's sprite, using the SetSprite() method, /// by the downloaded image (adapted into a sprite) /// </summary> /// <param name="url"></param> /// <param name="image"></param> /// <returns></returns> IEnumerator DownloadImage(string url, GetInformationFromCanvas image) { UnityWebRequest request = UnityWebRequestTexture.GetTexture(url); yield return(request.SendWebRequest()); if (request.isNetworkError || request.isHttpError) { Debug.Log(request.error); } else { reziseCanvas(image.GetRectTransform(), DownloadHandlerTexture.GetContent(request).width, DownloadHandlerTexture.GetContent(request).height); image.SetSprite(Sprite.Create( DownloadHandlerTexture.GetContent(request), new Rect(0.0f, 0.0f, DownloadHandlerTexture.GetContent(request).width, DownloadHandlerTexture.GetContent(request).height), new Vector2(0.5f, 0.5f))); } }