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