You can use an imported or built-in image asset to set a background image for your UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary.
To use UI Builder to set a background image for your UI:
VisualElement
in UI Builder.When you set the background image, you must select a supported background image type:
Note: To use an SVG image as a background for a VisualElement
, you must install a package. To do so, in the Package Manager window, install the package com.unity.vectorgraphics
from Git URL.
To use C# to set a background image for your UI, assign the image to the style.backgroundImage
property.
The following code example shows how to set background images for visual elementsA node of a visual tree that instantiates or derives from the C# VisualElement
class. You can style the look, define the behaviour, and display it on screen as part of the UI. More info
See in Glossary:
// Use the AssetDatabase method to load the texture.
myElement1.style.backgroundImage = AssetDatabase.LoadAssetAtPath<Texture2D>("path/to/imageFile.png");
// Use the AssetDatabase method to load the Sprite.
myElement2.style.backgroundImage = new StyleBackground(AssetDatabase.LoadAssetAtPath<Sprite>("path/to/spriteAssetFile.png"));
// Load the texture from project's Resources folder.
myElement3.style.backgroundImage = Resources.Load<Texture2D>("imageFile");
// Load the Sprite from project's Resources folder.
myElement4.style.backgroundImage = new StyleBackground(Resources.Load<Sprite>("spriteAssetFile"));
// Use the Unity Editor's default resources.
myElement5.style.backgroundImage = EditorGUIUtility.FindTexture("CloudConnect");
myElement6.style.backgroundImage = EditorGUIUtility.IconContent("FolderOpened Icon").image;