Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

New UI canvas and drawing images

Discussion in 'Scripting' started by Grimshad, Sep 21, 2014.

  1. Grimshad

    Grimshad

    Joined:
    Dec 28, 2011
    Posts:
    9
    Hi all,

    I'm switching my WIP over from the old UI system to the new UI system and I'm trying to find the best way to do something.

    So lets just say for simplicity sake, depending on which arrow key i'm pressing I want to draw an image on the screen.

    In the old UI system I would just check which arrow key is down and draw the corresponding image in the OnGUI() function.

    With the new system the only way I've found to do this same thing is messy. I have to put each image in its own canvas and then reference each canvas in the script and then check which arrow key is down and enable the corresponding canvas and disable it when finished.

    So i'm trying to figure out, is there any way to have all of the images on 1 canvas and have the images draw or not draw depending on certain variables?
     
  2. aoe_labs

    aoe_labs

    Joined:
    Nov 4, 2013
    Posts:
    42
    One way to do this is with UI buttons. They have a component in their Button Script that allows for events to be called from OnClick(). You can attach a script to that button - and within that script have a custom method for changing the images - and when the button is clicked, your custom method will be called. This video explains it better.

    If you don't want to use buttons and would prefer keys, you could write a script that
    • has an array of images
    • gets the image component similar to this: images = gameObject.GetComponentsInChildren<Image>();
    • and then sets the image accordingly with key presses