Using Texture Assets in Code

I have some sprites in my Assets/Images/ folder which I want to use in dynamically created Orthello 2D Sprites.

Here’s the code:

OTSprite os = OT.CreateObject(OTObjectType.Sprite).GetComponent<OTSprite>();
os.position = mPos;
os.additive = true;
os.image=Resources.LoadAssetAtPath("Assets/Images/circle.png", typeof(Texture)) as Texture

Now this works fine when I play in Unity. But when I build & run, the proper sprite is not loaded & I just see the default square sprite.

What’s wrong with this?

Is there any other way to use imported sprites at runtime?

1 Answer

1

Try putting your assets in the Assets/Resources folder. My guess is that since the the images in Assets/Images are not referenced directly in code, they are not being included in the build. Everything in the Resources folder is included in the build.