Author Topic: Getting current position in Slider?  (Read 3366 times)

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Getting current position in Slider?
« on: April 27, 2014, 04:55:12 AM »
Hi!

I have a level progress bar with UISlider setup. As time progresses (level) slider moves to the end, I want to be able to mark certain points on the slider, for example sections of the level that are reached. In order to do this I need position of the right edge of the slider bar, then I should be able to instantiate a sprite over slider to mark that position.

Any ideas how to do it? Thanks!

Sahkan

  • Jr. Member
  • **
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 74
    • View Profile
Re: Getting current position in Slider?
« Reply #1 on: April 27, 2014, 10:23:09 AM »
Make a new script, put this code into it ( In this case the file named : MyTestScript_1 ) :

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MyTestScript_1 : MonoBehaviour {
  5.  
  6.  
  7.         public UISlider slider_1 ;
  8.  
  9.         void Update ()
  10.         {
  11.                 print (slider_1.sliderValue);
  12.         }
  13. }
  14.  

Drag that script into the Slider object .

Drag the slider object from the hierarchy tab to the slider_1 value which shows in the Inspector  (Because it declared as public ) .

Play the scene and it will print the value of it while you play in it every frame .

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Re: Getting current position in Slider?
« Reply #2 on: April 27, 2014, 10:30:17 AM »
Thanks for looking into this although i dont need a value of slider but the position of of a point that is on the right side of the slider (uisprite that is moving) that i can use to instantiate another sprite to mark position on the slider.

Sahkan

  • Jr. Member
  • **
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 74
    • View Profile
Re: Getting current position in Slider?
« Reply #3 on: April 27, 2014, 11:02:47 AM »
I just wanna make sure we are on the same page .

Do you mean this sprite ?


Sahkan

  • Jr. Member
  • **
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 74
    • View Profile
Re: Getting current position in Slider?
« Reply #4 on: April 27, 2014, 11:13:46 AM »
Because if so, all you need to do is :

1.Make a new script .

2.Attach it to the "Thumb" object ( Which is holding that sprite ) .

3.Open this script and put into the Update() function this line :
      print (transform.position);
This line however will print the values with only one digit after the decimal point, If you want it to be more precise you should use these three lines :
      print ("X position : "+transform.position.x);
      print ("Y position : "+transform.position.y);
      print ("Z position : "+transform.position.z);


pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Re: Getting current position in Slider?
« Reply #5 on: April 27, 2014, 11:54:53 AM »
I dont have thum since its progress bar, it is the sprite which is moving

Sahkan

  • Jr. Member
  • **
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 74
    • View Profile
Re: Getting current position in Slider?
« Reply #6 on: April 27, 2014, 12:22:35 PM »
But some object has to have the sprite on it, it's not just sitting there...

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Re: Getting current position in Slider?
« Reply #7 on: April 27, 2014, 11:46:46 PM »
If you click on any sprite in edit view you will notice green handles, i need that in run time

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Getting current position in Slider?
« Reply #8 on: April 28, 2014, 12:15:41 AM »
The thumb doesn't need to be visible. You can simply create a plain game object and set that to be the thumb. You can then retrieve its position like you would with any game object.

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Re: Getting current position in Slider?
« Reply #9 on: April 28, 2014, 12:19:54 AM »
Ok i will try that, but anyhow i would like to know how you draw handles on edges of the sprite, how do you get that position?

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Re: Getting current position in Slider?
« Reply #10 on: April 28, 2014, 02:14:11 AM »
ok thanks to both of you, i thought it might involve something else but this was simple and elegant