create UI button via script

i know how to create an UI button .but i want to do it with script.how can i do it?

Make it into a prefab and instantiate it as a child of the canvas.

1 Like

hmmmmm i quess creating a prefab for something like a button is a better idea ,thanks

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: create UI button via script - Unity Engine - Unity Discussions **

This version won't assign a background image (assumes you'll add a custom one yourself):

GameObject var newButton = DefaultControls.CreateButton(
new DefaultControls.Resources()
);
newButton.transform.SetParent(canvas.transform, false);

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)

1 Like

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.

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 :).

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:

DefaultControls.Resources uiResources = new DefaultControls.Resources();
foreach(Sprite sprite in Resources.FindObjectsOfTypeAll<Sprite>())
{
  if( sprite.name == "UISprite" )
  {
    uiResources.standard = sprite;
    break;
  }
}
GameObject var NewButton = DefaultControls.CreateButton(uiResources);
newButton.transform.SetParent(canvas.transform, false);

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.

I’ll move this UI post to the UI forum. It doesn’t belong here.

1 Like