Search Unity

create UI button via script

Discussion in 'UGUI & TextMesh Pro' started by PersianKiller, Jul 17, 2018.

  1. PersianKiller

    PersianKiller

    Joined:
    Jul 16, 2017
    Posts:
    132
    i know how to create an UI button .but i want to do it with script.how can i do it?
     

    Attached Files:

  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Make it into a prefab and instantiate it as a child of the canvas.
     
    PersianKiller likes this.
  3. PersianKiller

    PersianKiller

    Joined:
    Jul 16, 2017
    Posts:
    132
    hmmmmm i quess creating a prefab for something like a button is a better idea ,thanks
     
  4. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    Because this is still a top hit on Google, and no-one had answered correctly (I knew there was a correct answer, but the name of the API is so badly named that it's imposible to find unless you already know the name):

    Unity provides an API call that creates each UnityUI thing directly from script, with all the required pieces, exactly as if you'd made it in Editor, sparing you from a *lot* of easy-to-make mistakes that lead to difficult-to-tract bugs in your code:
    https://docs.unity3d.com/Packages/com.unity.ugui@1.0/api/UnityEngine.UI.DefaultControls.html

    For a button:

    If you want a button exactly like in UnityEditor, use the slightly longer code in my post below: https://forum.unity.com/threads/create-ui-button-via-script.540947/#post-9149026
    Code (CSharp):
    1.  
    2. This version won't assign a background image (assumes you'll add a custom one yourself):
    3.  
    4. GameObject var newButton = DefaultControls.CreateButton(
    5. new DefaultControls.Resources()
    6. );
    7. newButton.transform.SetParent(canvas.transform, false);
    8.  
    NB: this works great for simple examples. For complex examples ... Unity broke some of the more complex UI controls in some versions of the Editor (I logged bugs against it, with 1-line-of-code reproductions that showed someone at Unity had broken the API, but Unity's QA team don't know how to read code (!) and kept rejecting it, eventually I gave up reporting it. I think it's working again in latest versions)
     
    Last edited: Jul 15, 2023
    eses likes this.
  5. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    I don't think there is one "correct" answer for how to create a button or any other UI element via script.

    IMO it depends on what you need. I haven't used Default Controls much, but at least some UI elements you create from menus have such settings I'll have to anyway configure those afterwards. I don't know/remember if you can do this better with Default Controls.

    But anyway, creating new UI elements using script can be done in several ways:

    Like you mentioned, it can be done with Default controls.

    Or piece by piece, by adding gameobjects and components.

    Or UI elements can be created from prefabs, like @LiterallyJeff mentioned.

    Another thing is, this is not the UGUI forum, this is 2D forum. And there exists some good answers on Stack Overflow about this same topic, like this one (see the answer by "Programmer") so people may have gone there for the information.
     
  6. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    Only two of those are by script, and one of them is VERY easy to get wrong and for some components VERY hard to get right (requires looking up the other hard-to-remember APIs for things like 'how do I get a default-font reference?').

    The other one (the one I posted) can be done in 1 line of code, and then only customize the bits you need to change.

    So ... yeah, I'd say there's a Right way for this :).
     
  7. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    Returning to add some missing code that most people probably want/need...

    If you want a fully default standard button there's a little bit more you have to do. My original example was fine if you want to customize the background image, but if you want a default BG image from Unity, you need this instead:

    Code (CSharp):
    1.  
    2. DefaultControls.Resources uiResources = new DefaultControls.Resources();
    3. foreach(Sprite sprite in Resources.FindObjectsOfTypeAll<Sprite>())
    4. {
    5.   if( sprite.name == "UISprite" )
    6.   {
    7.     uiResources.standard = sprite;
    8.     break;
    9.   }
    10. }
    11. GameObject var NewButton = DefaultControls.CreateButton(uiResources);
    12. newButton.transform.SetParent(canvas.transform, false);
    13.  
    Note: if you're going to do this thousands of times in a single frame then you should cache the reference to uiResources and not re-create it for each one - you only need it to refresh it when Editor reloads assemblies etc. But for most cases you'll only call this once every few minutes or similar and won't notice the cost/won't care.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    I'll move this UI post to the UI forum. It doesn't belong here.
     
    eses likes this.