Skip to content

Instantly share code, notes, and snippets.

@desmondfernando
Last active August 29, 2015 14:14
Show Gist options
  • Save desmondfernando/170398da1a986594b720 to your computer and use it in GitHub Desktop.
Save desmondfernando/170398da1a986594b720 to your computer and use it in GitHub Desktop.
Snippet to create basic Unity UI button from script, using built in resources
// create new button...
var o = new GameObject( id, typeof( Image ), typeof( Button ) );
o.transform.SetParent( parent );
var image = o.GetComponent<Image>();
image.sprite =
AssetDatabase.LoadAllAssetsAtPath( "Resources/unity_builtin_extra" ).OfType<Sprite>().SingleOrDefault(
sprite => sprite.name.Equals( "UISprite", StringComparison.OrdinalIgnoreCase ) );
image.type = Image.Type.Sliced;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment