Search Unity

Creating a slider to control variables

Discussion in 'Scripting' started by housewarmer, Aug 26, 2008.

  1. housewarmer

    housewarmer

    Joined:
    Apr 18, 2008
    Posts:
    74
    I'd like to be able to create a slider for controlling variables in the editor - like the specular value on glossy shaders. Using the method that's in the shader source doesn't seem to work in js. How would I go about this?

    Thanks!
    Mark
     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    This should do what you want:

    Code (csharp):
    1. //keep our own copy of shininess so we aren't always reading from the material
    2. private var shininess : float;
    3.  
    4. function Awake() {
    5.     //initialize shininess from material
    6.     shininess = renderer.material.GetFloat("_Shininess");
    7. }
    8.  
    9. function OnGUI() {
    10.     //get a new shininess out of a slider
    11.     shininess = GUI.HorizontalSlider(new Rect(10, 10, 100, 20), shininess, 0.01, 1.0);
    12.     //assign the shininess to the material
    13.     renderer.material.SetFloat( "_Shininess", shininess );
    14. }
     
  3. housewarmer

    housewarmer

    Joined:
    Apr 18, 2008
    Posts:
    74
    Ah, looking at my post I could have been more clear - what I'd like to achieve is a slider in the inspector (supplanting type fields), rather than a GUI solution.
     
  4. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    You'd nee to create your own custom inspector for that (that is, extend the editor rather than setting up parameters on your game object).

    This is possible in 2.1, however since custom inspectors are not officially supported, your code might very well break after the next release of unity.
     
  5. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    o_O custom inspectors
    ...(starts digggin)