Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SpencerFalwell · May 20, 2018 at 05:03 AM · c#unity 52dgui

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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 ;
  }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SpencerFalwell · May 23, 2018 at 11:56 AM 0
Share

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 :'

avatar image Hellium SpencerFalwell · May 23, 2018 at 12:06 PM 0
Share

Answer fixed (and tested)

avatar image SpencerFalwell Hellium · May 23, 2018 at 01:59 PM 0
Share

Perfect, that works just right. Thank you very much!

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

584 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Button Enabling/Disabling using Collision Triggers? 1 Answer

1 Button Alternating Between 2 onClick Functions 1 Answer

Using c# to Set a Button's Color onto a GameObject in Another Scene? 1 Answer

Does anyone know of a way to assign an Axis or KeyCode to a GUI Button or Texture? I'm trying to make a 2D platformer on a mobile device. As you can tell, I have no Idea what I'm doing... 1 Answer

My rigidbody2d is passing through the side of a collider2d but it's working on the top 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges