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 PuneetK · Oct 06, 2013 at 07:18 PM · progress bargui style

Progress bar moving texture?

I have a progress bar in my scene which basically increases its length, every time a certain input is given.

  //This is in onGUI
     
     GUI.BeginGroup(group2);        
     GUI.Box(box2, full, guifull);
 
 //this is in update
 
 group2 = new Rect(0, 0, Screen.width * 0.6f * barDisplay,Screen.height * 0.04f);
     box2 = new Rect(1.5f, 2.5f, Screen.width * 0.6f, Screen.height * 0.04f);
     barDisplay = mov*(1/(float)themaxmovespossible);

   

Now, my question is, how do I get this bar, to have a moving/changing constant texture. Something (or almost similar to) Gmail's loading bar, or the bar below a google image when you open it.

Example: http://www.thinkcreative.com/extension/site/design/site/images/animated_progress_bar.gif

I wish to get this effect somehow. My main issue is, this bar is being created dynamically using onGUI. I don't know how in the world will I be able to get the GUIstyle of this elemet to be animated. I could use a sprite sheet or something, but that would be if it was a plane texture made in the heirarchy, not a guistyle, that too one created dynamically by script.

Thanks!

I have read up a few questions on this before, but none solved my doubt particularly which is why I am creating a question!

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

3 Replies

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

Answer by brycedaawg · Oct 08, 2013 at 05:41 PM

A simple way to approach this problem is by using Unity's GUI.DrawTextureWithTexCoords; a link to this method's reference can be found here: http://docs.unity3d.com/Documentation/ScriptReference/GUI.DrawTextureWithTexCoords.html

Like GUI.DrawTexture, you can use this method to draw a texture, within a rectangle, on the screen. Unlike GUI.DrawTexture, however, you get to pick another rectangle with which Unity will 'slice' a section of the texture out and place onto the screen. Using this, you can effectively 'slide' the rectangle across the texture over time, giving you the same effect as the animated gif file you linked, but with the added benefit of using only a single frame image and not needing to render it in 3d in your scene.

Here's a very simple script I wrote that demonstrates using GUI.DrawTextureWithTexCoords. You'll need to provide your own texture, and it will most definitely stretch accross the screen, but it should provide you with enough information to create your own progress bar in your project.

 using UnityEngine;
 using System.Collections;
 
 public class GUILoadBar : MonoBehaviour
 {
     private float        loadBarProgress    = 0.0f;
     private const float    loadBarSpeed    = 0.25f;
     public Texture        loadBarTexture    = null;
     void OnGUI()
     {
         //Draw loading bar with offset texture coordinates
         GUI.DrawTextureWithTexCoords(new Rect(Screen.width * 0.4f, Screen.height * 0.4f, Screen.width * 0.2f, Screen.height * 0.1f), loadBarTexture, new Rect(loadBarProgress, 0.0f, 1.0f, 1.0f), false);
     }
     // Use this for initialization
     void Start ()
     {
         
     }
     
     // Update is called once per frame
     void Update ()
     {
         //Move the bar along; keep it's position between zero and one for best float point precision
         loadBarProgress += Time.deltaTime * loadBarSpeed;
         if (loadBarProgress >= 1.0f) loadBarProgress -= 1.0f;
     }
 }
 
Comment
Add comment · Show 5 · 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 robertbu · Oct 08, 2013 at 07:47 PM 0
Share

Nice solution.

avatar image brycedaawg · Oct 09, 2013 at 03:34 AM 0
Share

Thanks man

avatar image PuneetK · Oct 09, 2013 at 09:40 AM 0
Share

Really nice dude! Would this also work for say a button, in which I just want the button to switch textures over a period of time?

avatar image brycedaawg · Oct 09, 2013 at 01:07 PM 0
Share

You could iterate over a few sub images in a sprite sheet using this method, yes. However, if you just want to switch between a few different textures over time, I'd recommend doing just that; save overcomplicating things for yourself.

avatar image PuneetK · Oct 09, 2013 at 01:09 PM 0
Share

$$anonymous$$akes sense! Thanks!

avatar image
1

Answer by robertbu · Oct 08, 2013 at 07:36 AM

I see two different approaches to this problem. One approach uses a set of textures to animate the bar. The link you provided is to an animated GIF, which is really composed of 16 separate images. You can do the same thing in Unity. You initialize artex in the following code to the set of images that animate the progress bar. Adjust 'percentage' to cause the bar to grow or shrink.

 #pragma strict
  
 var artex : Texture[]; 
 var percent = 0.5;
 var framesPerSecond = 8.0;
 private var curr = 0;
 
 function Update() {
     curr = (Time.time * framesPerSecond) % artex.Length;
 }
  
 function OnGUI() {
     if(Event.current.type.Equals(EventType.Repaint))
         Graphics.DrawTexture(Rect(10, 10, artex[0].width * percent, artex[0].height), artex[curr], Rect(0,0,percent, 1.0), 0, 0, 0, 0);
 }

Another approach is to use a Quad in world space. With an game object in world space, I can control the material offset and scale/tiling. Here is the setup:

  • Add the following single tile segment to the project. I used Advanced and RGBA32.

  • Create a new material: Unlit/Texture with the tile segment as the texture

  • Create a Quad and size it to (1.0, 0.2, 1.0).

  • Attach the created material to the Quad.

  • Attach the following script.

Tile Segment:

alt text

Script:

 #pragma strict
 
 var offsetSpeed = 1.0;
 var scaleRatio = 4.0;
 
 private var offset = 0.0;
 
 function Update () {
     offset -= Time.deltaTime * offsetSpeed;
     renderer.material.SetTextureOffset("_MainTex", Vector2(offset,0));
     renderer.material.SetTextureScale("_MainTex", Vector2(transform.localScale.x * scaleRatio, 1.0));
 
 }

You adjust the transform.localScale.x to make the bar shrink and grow. Note that I did not get a perfect cut on the segment, but it is close and you get the idea. Also note that neither of the solutions implement the progress bar background.


progress_bar_segment.png (3.7 kB)
Comment
Add comment · Show 2 · 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 PuneetK · Oct 08, 2013 at 09:47 AM 0
Share

Thanks Robert! I believe I'll be going with the artex approach, as I'm doing everything in onGUI and according to screen coordinates.

If possible, could you explain the artex approach in a little more detail and possibly tell me how to do some variations in it as well?

avatar image robertbu · Oct 08, 2013 at 03:57 PM 0
Share

The code works by rotating through a series of frames to animate the action. The easiest way to understand the code is to play with it. One source of frames is animated GIFs (including the one at your link). Pull any animated GIF into a later-version of Photoshop, and you can save out the frames. Or Google "get frames from animated gif," and you will find both online solutions and downloadable tools for extracting frames.

Once you have the frames, import them into your project. Attach the above script to an empty game object. Then in the inspector, first set the size of artex to the number of frames, then drag and drop the frames in the slots of the array. Hit play. You can adjust the Percentage variable in the inspector and watch the progress bar grow and shrink while still animating.

Shortcut: You can use the lock icon in the upper right corner of the inspector to lock the view to your script. Then you can select all the frames and drop them all on artex at once.

I don't know what you mean by variations.

avatar image
0

Answer by ShadoX · Oct 07, 2013 at 08:05 AM

You could try displaying a animated texture and hide it using some layer, I presume. I'm not too sure how the loading bar is supposed to look since the example you linked to isn't animated either.

Comment
Add comment · Show 2 · 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 PuneetK · Oct 07, 2013 at 05:40 PM 0
Share

I really don't know how to do either..! Please explain

avatar image PuneetK · Oct 07, 2013 at 05:44 PM 0
Share

Put up a better (working) example

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

17 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

Related Questions

Load Level 01 Progress Screen 0 Answers

Want to create a moving line over a filling object 1 Answer

Trying to pick one or the other 1 Answer

Timer With GUI 0 Answers

Is it possible to get a smooth WebGL Progress Bar? 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