Search Unity

New UI Images

Discussion in 'Scripting' started by GamesOnAcid, Feb 11, 2016.

  1. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    I am trying to draw an image to the screen with the new UI, and I've reached a blockade. I can't seem to find any decent tutorials, so I'm stuck. Basically, I have a canvas with an image attached to it. I need to know how to script it so that the canvas only appears on keypress 'E', and draws the image. How do I do this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Essentially you would position the image where you want in the editor, then make a reference to that image in your script.

    At Start() time in your script you would disable that image (set its .enabled property to false).

    At Update() you would check for the E key and when it is pressed, you would enable the image.

    If you need it to only appear when the E key is BEING held, then you can do away with anything in Start() and simply copy through the current state of the E key being up/down into the image's .enabled property.
     
  3. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    How do I reference the image? That is what my problem is.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Ah, gotcha. Put the image in the scene when you lay out your scene.

    Then in your script you would have a public variable:

    Code (csharp):
    1. public Image myImage;
    Once that script has been placed on a game object in the scene, you can select that game object in the editor, and drag the game object containing the Image onto the public field, which should now be visible in the inspector window.
     
  5. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Code (JavaScript):
    1. public var TableNote : Image;
    Almost positive this is the JS equivalent, however, my images I am trying to display won't drag into the inspector. It says that they aren't compatible. Should they be sprites, textures? What should I set the images as themselves so they work in the inspector?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    You should not be dragging the texture from in the project folder. That is the actual asset and should be imported as a sprite in order to work with the UI system.

    You need to put that texture on a Unity UI Image component in your scene and make it look how you want.

    What you drag into that field on your script is the specific GameObject in your scene, the one with the Unity UI Image that contains a reference to your texture/sprite.
     
  7. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    First off you use C# for the UI as far as I know and to get it working... maybe try


    Code (CSharp):
    1. using UnityEngine.UI;
    2.  
    3. public Image imageShown;
    4.  
     
    LeftyRighty likes this.
  8. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Ah, ok. That was what I was misunderstanding. Putting an image on a canvas and disabling the image worked flawlessly. Thanks guys!
     
    Josenifftodd likes this.