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 Ben Blaut · Jul 31, 2012 at 04:32 PM · javascriptguieditor

How can I make a custom Time Range Selector (with sliders)?

I'm trying to use GUI.DrawTexture to create a custom progress bar type object within OnInspectorGUI. Despite the fact that I haven't been able to get it to work outside of OnGUI, I also haven't had luck creating what I want. Basically, I have two sliders that control the range you select. The first would select the opening value of the range, and the second selects the end value. There would then be a texture drawn in between the two that updates with the sliders as you move them. Outside of the range, the texture would be different on each end. Currently, I only have a custom progress bar that fills in from one side working, based on answers from other questions. Is a selector like this possible to create with Unity's API?

Here's my code so far:

  var startSliderValue : float = 0.0;
  var endSliderValue : float = 1.0;
  var pos : Vector2 = new Vector2(20,40);
  var size : Vector2 = new Vector2(200,20);
  var progressBarEmpty : Texture2D;
  var progressBarFull : Texture2D;

  function Start() {
    progressBarEmpty = Resources.Load("white-texture-large");
    progressBarFull = Resources.Load("Black");
  }

  function OnGUI() {  //Ideally, this is OnInspectorGUI in an Editor class
    GUI.DrawTexture(Rect(pos.x, pos.y, size.x, size.y), progressBarEmpty);
    GUI.BeginGroup(new Rect (pos.x, pos.y, size.x * Mathf.Clamp01(startSliderValue), size.y));
    GUI.DrawTexture(new Rect(0, 0, size.x, size.y), progressBarFull);
    GUI.EndGroup();  
  }

The sliders are in a different script within the Inspector.

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 Ben Blaut · Feb 27, 2013 at 06:46 PM

To do this, I ended up using EditorGUI.DrawPreviewTexture instead of GUI.DrawTexture. I also used a MinMaxSlider from EditorGUILayout to specify a time range. DrawPreviewTexture draws a dynamic color bar underneath to show where previous time ranges were saved (reading from XML). Since DrawPreviewTexture requires a texture to show, I just initialize my own color textures based on how many time ranges I have with the following function:

 /// <summary>
 /// Create a texture with a unique color based off of the current
 /// _numTimeRanges.
 /// </summary>
 function createNewTexture(_numTimeRanges : int)
 {
   var texture : Texture2D = new Texture2D(128, 128);
 
   for (var y : int = 0; y < texture.height; y++) {
     for (var x : int = 0; x < texture.width; x++) {
       var color : Color;
 
       if (_numTimeRanges == 0) {
           color = Color.white;
       }
       else if (_numTimeRanges % 4 == 1) {
           color = Color.red;
       }
       else if (_numTimeRanges % 4 == 2) {
           color = Color.blue;
       }
       else if (_numTimeRanges % 4 == 3) {
           color = Color.yellow;
       }
       else if (_numTimeRanges % 4 == 4) {
         color = Color.black;
       }
 
       texture.SetPixel(x, y, color);
     }
   }
 
   texture.Apply();
 }
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

6 People are following this question.

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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Limit on GUI Components? 0 Answers

How To Make Ammo & Realod for Gun & Spark for Gun ? 0 Answers

How to make an GUILayout.Window draggable ? 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