public virtual GameObject InstantiateImageObject(TextureExporter.ImageFile image, int i, Transform parent) { // Instantiage prefab GameObject instance = GameObject.Instantiate(m_ImageItemPrefab); // Set the go parent instance.transform.SetParent(parent); instance.transform.localScale = Vector3.one; // Set texture RawImage img = instance.GetComponentInChildren <RawImage> (); if (img == null) { Debug.LogError("Can not find the RawImage component in the gallery image. Be sure the canvas game object is enabled before calling this method."); } img.texture = image.m_Texture; // Add button callback Button btn = instance.GetComponentInChildren <Button> (); int index = i; btn.onClick.AddListener(() => OnSelectImageCallback(index)); // Add image to list m_ImageInstances.Add(instance); return(instance); }
public virtual void DoUpdate() { TextureExporter.ImageFile imageFile = m_Gallery.m_ImageFiles [m_CurrentImageId]; // Update the texture image RawImage img = m_ImageContainer.GetComponentInChildren <RawImage> (); img.texture = imageFile.m_Texture; // Scale the texture to fit its parent size float parentRatio = m_ImageContainer.rect.width / m_ImageContainer.rect.height; float ratio = (float)imageFile.m_Texture.width / (float)imageFile.m_Texture.height; float scaleCoeff = ratio / parentRatio; if (scaleCoeff >= 1f) { img.GetComponentInChildren <RawImage> ().transform.localScale = new Vector3(1f, 1f / scaleCoeff, 1f); } else { img.GetComponentInChildren <RawImage> ().transform.localScale = new Vector3(scaleCoeff, 1f, 1f); } // Buttons if (m_CurrentImageId == 0) { m_PreviousButton.gameObject.SetActive(false); } else { m_PreviousButton.gameObject.SetActive(true); } if (m_CurrentImageId >= m_Gallery.m_ImageFiles.Count - 1) { m_NextButton.gameObject.SetActive(false); } else { m_NextButton.gameObject.SetActive(true); } m_FileName.text = imageFile.m_Name; m_PageText.text = (m_CurrentImageId + 1).ToString() + "/" + m_Gallery.m_ImageFiles.Count; }