- Home /
The question is answered, right answer was accepted
Set picture to 4 UI Image grid
Hi, I have got simple grid on canvas made by UI Images. I would like to set sprites in UI Images so they fit right their content. What I want is on the right, what I have already is on left. Is there way to set uv for them?
Answer by TBruce · Apr 07, 2016 at 09:43 PM
The easy solution would be to slice up the image in import settings. Then you could easily set each individual image.
To do this dynamically its a bit more complicated, especially since the UI image has a RectTransform but it can be done with some coding. You would need to create a script that does something like this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Gem : MonoBehaviour
{
public List<Image> uiImages = new List<Image>();
public Texture2D image;
public int rows = 2;
public int columns = 2;
// Use this for initialization
void Start ()
{
if ((image != null) && (rows * columns) == uiImages.Count)
{
// first verify all GameObjects have been assigned
for (int i = 0; i < uiImages.Count; i++)
if (uiImages[i] == null)
return;
}
}
public void AssignImages()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
// you will need to create a sprite based on the portion of the image that you wan to copy
// and then assign it to the appropriate image in your list
}
}
}
}
@$$anonymous$$amil1064 Would you be so kind as to click the tick to accept the answer if your question was answered. Thank you!
Follow this Question
Related Questions
UI Sprite location changes its appearance. 0 Answers
Crop UI image and convert it as Sprite 1 Answer
Sprite import problem? 1 Answer
Change UI GUI Image Sprite at runtime .. Unity 5 .. js 1 Answer