Forum rules - please read before posting.

Help with Unity UI Slider that displays images as Menu

I have a Unity UI slider with a simple script attached. The script takes a Target, which is an empty gameobject in the scene that has a SpriteRenderer component. When you slide the slider, it flips through an array of images (in Assets, not in the scene) and you can set the size of the array. Works well.

public Sprite[] sliderSprites;
public GameObject target;
Slider mySlider;

void Start()
{
    mySlider = GetComponent<Slider>();
    mySlider.maxValue = sliderSprites.Length - 1;
}

void Update()
{
    target.GetComponent<SpriteRenderer>().sprite = sliderSprites[Mathf.FloorToInt(mySlider.value)];
}

However, I want this to be an AC Menu that I can turn off and off manually. It's part of an in-game computer interface, so it should only be on when the player uses the laptop.

What's the best way to convert this set-up to an AC menu? Or is there a simple way to turn the canvas on and off without it being a Menu?

I've tried a few combinations of AC menu elements that aren't working because the slider as AC Menu can't access the scene-based Target and bank of sprite images (unassigned variable console error).

I hope that makes sense. Any ideas or advice would be appreciated! Thanks!

Comments

  • Or is there a simple way to turn the canvas on and off without it being a Menu?

    You can enable/disable the Canvas component at runtime - or the Canvas's GameObject - through script:

    mySlider.GetComponentInParent <Canvas>().gameObject.SetActive (false);
    

    What's the best way to convert this set-up to an AC menu?

    An AC menu can still rely on Unity UI for its rendering, in which case you can still keep the script attached as it doesn't affect the contents/behaviour of the Slider - only reads it.

    See the Manual's "Unity UI menus" chapter for a rundown on how Unity UI prefabs are linked to AC's Menu system.

    Just for completeness: if you did want to rely on an "AC" menu in the sense that its Source option is set to Adventure Creator, you could have the Slider element be mapped to a Global Float Variable, and then modify your script to read from the Variable's value, rather than the Slider's value:

    target.GetComponent().sprite = Mathf.FloorToInt (AC.GlobalVariables.GetFloatValue (id));

    (Where "id" is the ID number of the variable you're linking to)

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.