Waltari
Waltari

Reputation: 1249

Unity set default image sprite programmatically

I am trying to create inputfields in Unity 3D programmatically. I succeeded in this when running in the editor emulator, but trying to built into an android device just provides an error. "BCE0005: Unknown identifier: 'AssetDatabase'." Apparently this AssetDataBase is only available in editor.

    inputFieldGO.AddComponent.<Image>();
    var image : Image = inputFieldGO.GetComponent.<Image>();
    image.sprite = AssetDatabase.GetBuiltinExtraResource.<Sprite>("UI/Skin/InputFieldBackground.psd");
    image.type = Image.Type.Sliced;

How do I get around this? How do I set the sprite of this image to the default InputFieldBackground entirely programmatically, without using the AssetDataBase? I'd move the InputFieldBackground into the project resources, but I don't know where the file is or if it's even accessible.

Upvotes: 0

Views: 3378

Answers (1)

mayo
mayo

Reputation: 4075

AssetDatabase is an Editor class, that means that can be used in Editor but not in devices.

Unity Scripting Reference For AssetDatabase

Image


Solution:

Do you have your files on Resources folder? Try this:

Sprite newSprite =  Resources.Load<Sprite>(spritePath);

From: Unity Scripting Reference for Resources.Load

Upvotes: 2

Related Questions