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 /
avatar image
0
Question by bekiryanik · Oct 09, 2014 at 11:53 AM · guiscreenscoreguilabel

How to center the score in screen?

Hello,

I put the score at upper center of the screen. Score is increased by players performance. What i want to do is, to center the score as far as it's number. For example, if score is greater than 9 or 99 or 999, it should stay in the upper center of the screen. I centered it by 0. It goes right as far as it's number. If it's greater then 9, it goes right.

I could do that just like that;

     if(Score >= 10)
     {
         GUI.Label(new Rect(100, 10, 100, 20),"",skin.GetStyle("Score"));
     }

     if(Score >= 100)
     {
         GUI.Label(new Rect(90, 10, 100, 20), "",skin.GetStyle("Score"));
     }

     if(Score >= 1000)
     {
         GUI.Label(new Rect(80, 10, 100, 20), "",skin.GetStyle("Score"));
     }

but i didn't want to do it like that. I was wondering that, if there is another way to do it briefly and to the point. It should stay in the top center all the time but how?

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

1 Reply

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

Answer by HarshadK · Oct 09, 2014 at 11:55 AM

Use GUIStyle.CalcSize to calculate the size of content from your label and then re-draw it according to its size.

Comment
Add comment · Show 4 · 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 bekiryanik · Oct 09, 2014 at 12:40 PM 0
Share

Damn.. it's Javascript. I guess i need to find it's C# version. Is this the only way?

avatar image Landern · Oct 09, 2014 at 12:54 PM 0
Share

The example is incomplete, but here is the c# version. be aware of the fields being modified on the target Property(damage and armor).

 using System;
 using System.Collections;
 using UnityEditor;

 [CustomEditor(SimpleExampleScript)]
 public class CustomEditorExample : Editor
 {
     public void OnInspectorGUI()
     {
         target.damage = EditorGUILayout.IntSlider("Damage:", target.damage, 1, 100);
         ProgressBar(target.damage / 100.0f, "Damage");
         
         target.armor = EditorGUILayout.IntSlider("Armor:", target.armor, 1, 100);
         ProgressBar(target.armor / 100.0f, "Armor");
     }
     
     public void ProgressBar(float value, string label)
     {
         Vector2 size = GUI.skin.GetStyle("ProgressBarText").CalcSize(GUIContent(label));
         Rect rect = GUILayoutUtility.GetRect (size.x, $$anonymous$$athf.$$anonymous$$ax(size.y));
         rect = new Rect(rect.x + 4, rect.y, rect.width - 8, rect.height);
         EditorGUI.ProgressBar (rect, value, label);
         EditorGUILayout.Space();
     }
 }
avatar image bekiryanik · Oct 11, 2014 at 10:54 AM 0
Share

I tried to modify this but i couldn't. How can i modify this?

avatar image bekiryanik · Oct 11, 2014 at 12:10 PM 0
Share

I have made the shortest way to center the score. Here is what i did;

 skin.GetStyle("Score").alignment = TextAnchor.UpperCenter;
                 Vector2 contentOffset = skin.GetStyle("Score").contentOffset;
                 contentOffset.x = -151;
                 skin.GetStyle("Score").contentOffset = contentOffset;

These codes centers the score according to its number.

Thanks for interesting guys, appreciated!

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

2 People are following this question.

avatar image avatar image

Related Questions

adjust GUI.label to the full width of the screen 0 Answers

Displaying score on screen in specific place 1 Answer

getting gui error you can only call functions from inside OnGui. 1 Answer

GUI Label Score Overlapping - Javascript 1 Answer

Make GUI elements disappear 3 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