Reputation: 55
How can I store gameobjects/images in an array so that I can just use the "for loop" in changing the color of the image instead of individual changing its color.. I'm using c# in unity3d..
my code:
public Image clay, sand, loam, silt, siltyClay, siltyLoam, loamySand, sandyLoam, siltyClayLoam;
public void imageColor(){
sand.color = Color.green;
clay.color = Color.white;
loam.color = Color.white;
silt.color = Color.white;
siltyClay.color = Color.white;
siltyLoam.color = Color.white;
loamySand.color = Color.white;
sandyLoam.color = Color.white;
siltyClayLoam.color = Color.white;
mountTex = (Texture)Resources.Load("sand", typeof(Texture));
}
Upvotes: 1
Views: 741
Reputation: 5353
You seem to want different colors for different images. You can e.g. group the Images and colors together.
public Image clay, sand, loam, silt, siltyClay, siltyLoam, loamySand, sandyLoam, siltyClayLoam;
[System.Serializable]
public class ImageColorPairs
{
public Image[] images {get;set; } /* associate Images */
public Color targetColor { get; set; } /* ..with a target color */
}
private ImageColorPairs[] pairs; //Declare here but initialize later. Can't initialize these objects (error: referencing non-static field..)
public void InitializePairsOnce()
{
/* Make up array of Pairs here */
pairs = new ImageColorPairs[] {
new ImageColorPairs() { images = new Image[] { sand }, targetColor = Color.green } ,
new ImageColorPairs() { images = new Image[] { clay, loam, silt, siltyClay, siltyLoam, loamySand, sandyLoam, siltyClayLoam }, targetColor = Color.white }
};
}
public void ImageColor()
{
/* Now setting the colors is easy! :) */
foreach (var pair in pairs)
{
foreach (var image in pair.images)
{
image.color = pair.targetColor;
}
}
}
You can also make it use more basic data strucutres like a your own ImageTuple
, which would then associate each image individually a color. But up there you seem to only have two basic groups, so it's less work to just associate a color for a whole array of images.
With such a class you would do it like
[System.Serializable]
public class ImageTuple
{
public Image image;
public Color targetColor;
public ImageTuple(Image img, Color col)
{
image = img;
targetColor = col;
}
}
private ImageTuple[] imageTuples;
public void InitializeTuplesOnce()
{
imageTuples = new ImageTuple[]
{
new ImageTuple(clay, Color.green),
new ImageTuple(sand, Color.white),
//.. the rest
};
}
public void ImageColor()
{
foreach (var pair in imageTuples)
{
pair.image.color = pair.targetColor;
}
}
Upvotes: 1
Reputation: 12598
Funnily enough, the best way to do it, is how you already have it.
There's only one thing you're not getting:
when you want to "highlight one thing", the programming patter is turn them all off, and then turn just the one on.
It's a basic programming trick:
public Image clay, sand, loam, silt, siltyClay,
siltyLoam, loamySand, sandyLoam, siltyClayLoam;
private void AllOff()
{
sand.color = Color.white;
clay.color = Color.white;
loam.color = Color.white;
silt.color = Color.white;
siltyClay.color = Color.white;
siltyLoam.color = Color.white;
loamySand.color = Color.white;
sandyLoam.color = Color.white;
siltyClayLoam.color = Color.white;
}
public Highlight( Image x )
{
AllOff();
x.color = Color.green;
}
to use ..
Highlight( silt );
The nine items in question are not, really, a list - so don't use them that way.
Note that in other cases you should do this:
public Image[] images;
try it and see, in the Inspector you can just drag them in.
To work on them all at once, it's just
foreach (Image i in images)
Debug.Log(i);
Upvotes: 5