Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by skillitronic · Mar 08, 2021 at 10:04 PM · uiupdatesynchronization

How to update ui in unity properly

I can't find best way on how to update the UI in Unity like a good developer would.

For example: I have a card which contains: - card name - manacost - card desc - damage

I created scriptable object script to store data about name,desc,damage.

 [CreateAssetMenu(menuName = "Create card", fileName = "new Card")]
 public class CardScriptableObject : ScriptableObject
 {
     public string CardName
     {
         get => _cardName;
     }
 
     public string Description
     {
         get => _description;
     }
 
     public int ManaCost
     {
         get => _manacost;
     }
 
     public int Damage
     {
         get => _damage;
     }
 
     public int Health
     {
         get => _health;
     }
 
     [SerializeField] private string _cardName;
     [SerializeField] private string _description;
 
     [Min(1)] [SerializeField] private int _manacost;
     [Min(0)] [SerializeField] private int _damage;
     [Min(0)] [SerializeField] private int _health;
 
 }

Also I created card script which will use local variable of this name,desc,damage to use in project.

 [DisallowMultipleComponent]
 public class Card : MonoBehaviour
 {
     [SerializeField] private CardScriptableObject _cardData;
     [SerializeField] private CardDisplay _cardDisplay;
     public int Health
     {
         get => _health;
         set 
         {
             _health = value;
             
             if (_health <= 0)
                 Die();
 
         }
     }
 
     public int Damage
     {
         get => _damage;
         private set
         {
             if (value < 0)
                 value = 0;
 
             _damage = value;
 
         }
     }
 
     public int ManaCost
     {
         get => _manaCost;
         private set
         {
             if (value < 0)
                 value = 0;
 
             _manaCost = value;
 
         }
     }
 
     private int _damage;
     private int _manaCost;
     private int _health;
 
     private void Awake()
     {
         _health = _cardData.Health;
     }
 
     private void Die()
     {
         Debug.Log("Card destroyed");
     }
 
 }

And I created card display script which will link UI objects with variables.

 [DisallowMultipleComponent]
 public class CardDisplay : MonoBehaviour
 {
     [SerializeField] private CardScriptableObject cardData;
 
     [SerializeField] private Image cardArtImage;
 
     public TextMeshProUGUI HealthText
     {
         get => _healthText;
     }
 
     public TextMeshProUGUI DamageText
     {
         get => _damageText;
     }
 
     public TextMeshProUGUI ManaCostText
     {
         get => _manaCostText;
     }
 
     [SerializeField] private TextMeshProUGUI _healthText;
     [SerializeField] private TextMeshProUGUI _damageText;
     [SerializeField] private TextMeshProUGUI _manaCostText;
 }

Now I want to know how to Update UI in unity when card will Instantiated,Changed health value.

Maybe use events?
Implement in setter (i think its not a good thing)?
Create something like UIManager?

It would be nice if someone gave me a links on how to do it in a good way. Or give me an example of how you would do it.

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

0 Replies

· Add your reply
  • Sort: 

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

243 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

Related Questions

Message Centre Does Not Display 0 Answers

How to use UI button in Update()? 1 Answer

Update UI from inside a loop 2 Answers

UI Text is not being rebuilt/updated in edit mode, when changing text from OnInspectorGUI 1 Answer

Layer mask has a weird interaction 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