- Home /
Best way of use conditional sprite related to variable (in editor + runtime) ?
Hi guys, I am new to unity (but have some experience with other framework). I make 2d game where I have MyTeam (Green Image) and Enemy (Red Image). Image is slightly different so I cannot use Tint.
I create 3 public variable in my Scripts which I attach to prefab.
public team; (0 = Green, 1 = Red)
public Sprite teamImage;
public Sprite enemyImage;
in Start method I get SpriteRenderer
void Start () {
spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
}
then inside Update method I add condition
spriteRenderer.sprite = team == 0 ? teamImage : enemyImage;
in my prefab by default I use teamImage for spriteRender and team = 0 and if I wanna change some unit to enemy I change team = 1. My problem it is still have teamImage in editor mode.
I see 2 solution here, but I have problem with each.
Create two different prefab. One for Green team, and one for Red. But when I wanna change something (For example size I will need do it twice for green and red, it is look as bad because break DRY principle, I try googling , can I inherit one prefab from another - but I don't find anything.
Try using something from here https://docs.unity3d.com/Manual/ExtendingTheEditor.html
but I don't understand is it possible at all.
I think this problem should be already solver several times, but I don't know where to find info. Thanks!
Your answer