- Home /
Unlocking & "Equipping" Colors onto Sprites with c#
Hey, everyone... A multi-tiered question, but help in any way would be greatly appreciated!
I have 2 desaturated/grey sprites: ( RingButtonImage + SpinnerImage) as the main focus of my game, in which you're allowed to change the color of them. I have a "store" with multiple buttons, which are images of those 2 sprites. The store's buttons are colored in their SpriteRenderers, showing the player exactly what their sprite will look like if they tap each button. I have already referenced all the button colors, and created voids for each of the buttons using the colors I referenced. This is what that looks like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonVoids : MonoBehaviour{
public PlayerPrefs highScore;
public GameObject RingButton;
public GameObject SpinnerImage;
Color yellow = new Color(1, 1, 0);
Color blue = new Color(0, 1, 1);
public void yellowSpin()
{
SpinnerImage.GetComponent<Renderer>().material.color = yellow;
}
public void yellowButt()
{
RingButton.GetComponent<Renderer>().material.color = yellow;
}
public void blueSpin()
{
SpinnerImage.GetComponent<Renderer>().material.color = blue;
}
public void blueButt()
{
RingButton.GetComponent<Renderer>().material.color = blue;
}
}
This script changes the color of the sprites just fine, it just doesn't save it. Is there any way to save the color of your sprites on click, so if you exit the app, their colors will stay the last colors you picked?
I also have text boxes overlaying each of the store buttons that display a required high score in order to unlock each color. Is there any way I can use a saved PlayerPrefs high score, which I already have working fine, to disable these buttons until their required high scores are reached (i.e. 10,15,20...), then enable them? Also, can I have the text displaying the required scores disappear when they're unlocked?
Again, thank you very much for reading and for any help you might offer!
Answer by Hellium · May 23, 2018 at 07:42 AM
Quick and dirty solution:
private void Awake()
{
SpinnerImage.GetComponent<Renderer>().material.color = LoadColor( "spin" );
RingButton.GetComponent<Renderer>().material.color = LoadColor( "butt" );
}
public void yellowSpin()
{
SpinnerImage.GetComponent<Renderer>().material.color = yellow;
SaveColor( "spin", yellow );
}
public void yellowButt()
{
RingButton.GetComponent<Renderer>().material.color = yellow;
SaveColor( "butt", yellow );
}
public void blueSpin()
{
SpinnerImage.GetComponent<Renderer>().material.color = blue;
SaveColor( "spin", blue);
}
public void blueButt()
{
RingButton.GetComponent<Renderer>().material.color = blue;
SaveColor( "butt", blue);
}
private void SaveColor( string imageName, Color color )
{
PlayerPrefs.SetString( imageName, "#" + ColorUtility.ToHtmlStringRGBA( color ) );
PlayerPrefs.Save();
}
private Color LoadColor( string imageName )
{
Color color = new Color(1, 1, 1, 1);
ColorUtility.TryParseHtmlString( PlayerPrefs.GetString( imageName, "#FFFFFFFF" ), out color ) ) ;
return color ;
}
Thank you, @hellium . Every time I die then restart it loads back the default color of white. Is there any reason why it would be doing that? I've tried everything and this is literally the final finishing touch to add to my game :'
Perfect, that works just right. Thank you very much!
Answer by SpencerFalwell · May 21, 2018 at 02:00 PM
I figured out how to use the high score to unlock the colors, here's the code now:
public void yellowSpin()
{
if (9 < PlayerPrefs.GetInt("highScore", 0))
{
SpinnerImage.GetComponent<Renderer>().material.color = yellow;
}
else
{
}
}
This is for yellow, which requires a high score of 10 to unlock.