I am a total newby to unity and cannot figure out how to achieve the following:
I want to show a picture composed of different sprites. E.g., a pen with four binary features: the pencolor (black or silver), the penshape (ballpen or normalpen), an ornament (blue or orange), and the tintcolor (blue or black).
My goal:
A function taking a numerical vector as input to specify which of the features/attributes of the pen to show.
- 0,0,0,0 = a black ballpen with blue ornaments and blue tint.
- 1,1,1,1 = a a silver normal pen with orange ornaments and black tint.
- ... and all combinations in between.
One constraint: I need to be able to change how many features there are, and how many values they take.
The sprites are stored in a sprite with mode "multiple" and I use unity to create the layers.
What I did so far:
1) Define an object holding the feature sprites
To enable changing how many features and feature values there are I created a structure. It has flexible length specifying the number of features. Each feature is sprite array specifying the number and source of the featuer values.
// New object class holding all visual feature sprites in hierarchical vectors
[Serializable]
public struct Feature
{
public string Name;
public Sprite[] FeatureValues;
}
public Feature[] Features;
This allows me to specify how many features, how many different sprites per feature, and to drag and drop the respective sprites onto the associated elements of the Features structure.
2) Defined a Prefab for the Pen
With the help of this post on displaying multiple sprites I created a prefab with as many layers as there are features of the pen
3. And now ... ?
Based on the first comment I created Sprite Renderers for all sub-elements of the Pen object.
The Pen object is a prefab.
Can anyone help me how to change the values of the sprite?
I am trying this: Based on the link mentioned above I create a new class for Pen objects with some sprite renderers
public class Pen : MonoBehaviour {
public SpriteRenderer Tintcolor;
public SpriteRenderer Casecolor;
public SpriteRenderer Ringcolor;
public SpriteRenderer Shape;
public void setTintcolor (Sprite featureImage)
{
Tintcolor.sprite = featureImage;
}
public void setCasecolor (Sprite featureImage)
{
Casecolor.sprite = featureImage;
}
public void setRingcolor (Sprite featureImage)
{
Ringcolor.sprite = featureImage;
}
public void setShape (Sprite featureImage)
{
Shape.sprite = featureImage;
}
}
Then I write a function to change the value of the sprite renderer.
// featureCombination is the vector holding the feature values to be displayed
public void ShowFeatureCombination(int[] featureCombination)
{
Pen newPen = Instantiate(penPrefab) as Pen; // new Pen object
newPen.setTintcolor(Features[0].FeatureValues[featureCombination[0]]);
newPen.setCasecolor(Features[1].FeatureValues[featureCombination[1]]);
newPen.setRingcolor(Features[2].FeatureValues[featureCombination[2]]);
newPen.setShape (Features[3].FeatureValues[featureCombination[3]]);
}
This does not work so far.
And it requires to manually specify the spriteRenderers, which i need to have automatically done based on the number of different features that I enter in the Feature structure.

