Search Unity

Scaling object with GUI

Discussion in 'Scripting' started by whitedrow, Mar 31, 2008.

  1. whitedrow

    whitedrow

    Joined:
    Mar 31, 2008
    Posts:
    15
    Hi,

    I'm new to Unity and trying to scale a cube with the GUI.
    I've found this script:


    Code (csharp):
    1.  
    2. /* Using nested Horizontal and Vertical Groups */
    3.  
    4. var sliderValue = 1.0;
    5. var maxSliderValue = 10.0;
    6.  
    7. function OnGUI()
    8. {
    9.     // Wrap everything in the designated GUI Area
    10.     GUILayout.BeginArea (Rect (0,0,200,60));
    11.  
    12.     // Begin the singular Horizontal Group
    13.     GUILayout.BeginHorizontal();
    14.  
    15.     // Place a Button normally
    16.     if (GUILayout.RepeatButton ("Increase max\nSlider Value"))
    17.     {
    18.         maxSliderValue += 3.0 * Time.deltaTime;
    19.     }
    20.  
    21.     // Arrange two more Controls vertically beside the Button
    22.     GUILayout.BeginVertical();
    23.     GUILayout.Box("Slider Value: " + Mathf.Round(sliderValue));
    24.     sliderValue = GUILayout.HorizontalSlider (sliderValue, 0.0, maxSliderValue);
    25.  
    26.     // End the Groups and Area
    27.     GUILayout.EndVertical();
    28.     GUILayout.EndHorizontal();
    29.     GUILayout.EndArea();
    30. }
    31.  

    How do I use the "sliderValue" for scaling e.g. the x-axis of a cube?

    I guess it's the update function but I have no idea how to tell the slide to sclae the cube.

    Greetings
     
  2. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    There are several ways to do it. You can put a script on the cube, for example, that queries sliderValue.

    Or you could add:

    Code (csharp):
    1. public var object : GameObject;
    In front and then do something like:

    Code (csharp):
    1.  
    2. object.transform.scale.x = sliderValue;
    3. object.transform.scale.y = sliderValue;
    4. object.transform.scale.z = sliderValue;
    5.  
    Not sure about the exact syntax. You will also want to performance-optimize this by calling it only when sliderValue actually changed. But you get the idea?