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 /
This question was closed Dec 25, 2014 at 04:09 AM by Prasanna for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Prasanna · Dec 23, 2014 at 06:23 AM · assetbundlefloatdownloadurlloading screen

Float to Integer

Hi all, i created a script for download my object from server. So i used calculating the download using following scripts.

 using UnityEngine;
 using System.Collections;
 public class BundleLoad : MonoBehaviour
 {
     private string progress = " "; 
     private string logtxt;
     public string url = " ";
 
     GUIStyle Font_Size;
     
     void Awake()
     {
         Font_Size = new GUIStyle ();
         Font_Size.fontSize = 40;
         Font_Size.normal.textColor = Color.black;
     }
 
     void log( string t )
     {
 
         logtxt += t + "/n";
     }
     void OnGUI()
     {
         Vector3 scale;
         
         float ResolutionX   = 1024;
         float ResolutionY   = 768;
         
         scale.x             = (float)Screen.width/ResolutionX;
         scale.y             = (float)Screen.height/ResolutionY;
         scale.z             = 1;
         
         Matrix4x4 svMat     = GUI.matrix;
         GUI.matrix          = Matrix4x4.TRS(new Vector3(0,0,0),Quaternion.identity,scale);
 
         GUI.Label (new Rect (560, 684, Screen.width - 5, Screen.height - 5), "Downloading: ", Font_Size);
         GUI.Label(new Rect(795, 684, Screen.width - 5, Screen.height - 5), logtxt + " " + progress, Font_Size);
     }
     
     IEnumerator Start()
     {
         using(WWW www = WWW.LoadFromCacheOrDownload (url, 1))
         { 
             while( !www.isDone )
             {
                 progress = " " + www.progress * 100 + " %";
                 yield return null;
             }
             yield return www;
             AssetBundle bundle = www.assetBundle;
             bundle.LoadAll();
             Application.LoadLevel("Bundle");
         }
     }
 }

Here my downloading parts are working fine. But my download value display in decimal values, i want to show that in integer. i tried a way to convert that, but there is no use.

 progress = " " + www.progress * 100 + " %";
Comment
Add comment · Show 1
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 Fonserbc · Dec 23, 2014 at 06:48 AM 0
Share

You should clean your question to the only important part which is the conversion and display of the floating number www.progress to string without decimals.

The question of formating floats to string has been answered many times before:

  • http://answers.unity3d.com/questions/183646/show-1-decimal-only-when-printing-a-float.html

  • http://answers.unity3d.com/questions/50391/how-to-round-a-float-to-2-dp.html

  • http://answers.unity3d.com/questions/192977/print-to-only-two-decimal-places.html

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Fonserbc · Dec 23, 2014 at 07:12 AM

There are several ways to get what you want, either casting or converting the float to int or use the built-in ToString() function of floats.

 String withoutDecimals;
 withoutDecimals = (www.progress * 100.0f).ToString("#");
 // Or
 withoutDecimals = ( (int)(www.progress * 100.0f).ToString();
 // Or
 withoutDecimals = ( Mathf.FloorToInt(www.progress * 100.0f) ).ToString();
 // Or use either Mathf.CeilToInt or Mathf.RoundToInt depending on what you want

The # inside the ToString function, tells the function to just print the integer value of the float. Alternatively you can pass the function #.# to ask for a decimal, #.## for 2 decimals and so on.

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 Prasanna · Dec 23, 2014 at 07:33 AM 0
Share

if i use this: progress = " " + www.progress.ToString("#") * 100 + " %"; got an error.

or if i use this:

progress = " " + ((int)www.progress).ToString(); my values are not increasing.

avatar image Fonserbc · Dec 23, 2014 at 07:55 AM 0
Share

Oh! progress goes from 0 to 1, I edited the answer

avatar image Prasanna · Dec 25, 2014 at 04:09 AM 0
Share

@Fonserbc, Thanks for your answer.

Follow this Question

Answers Answers and Comments

28 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

Related Questions

Error when downloading AssetBundle from external server during runtime 0 Answers

Unity 5 Download AssetBundles from server via URL. 2 Answers

Loading scene from assetbundle (url) 1 Answer

Unauthorized use of Caching API in webplayer 0 Answers

Asset Bundles and dynamic loading 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