5
\$\begingroup\$

I'm looking for a way to draw textures to screen in Unity in a similar fashion to XNA's SpriteBatch.Draw method. Ideally, I'd like to write a few helper methods to make all my XNA code work in Unity. This is the first issue I've faced on this seemingly long journey.

I guess I could just use quads, but I'm not so sure it's the least expensive way performance-wise. I could do that stuff in XNA anyway, but they made SpriteBatch not without a reason, I believe.

So I see there is some sort of GUI notation in Unity. Maybe I could efficiently draw everything using it?

\$\endgroup\$
2
  • \$\begingroup\$ if you want to just draw a texture (like a GUI item) you can use the "OnGUI" method that comes with monobehaviour and function "GUI.DrawTexture( ... )" \$\endgroup\$ Commented Aug 1, 2013 at 23:03
  • \$\begingroup\$ I wouldn't be surprised if XNA's SpriteBatch.Draw method is an abstraction on top of 3D quads. I mean, I don't know how Windows does things under the hood, but I know that on OSX Quartz graphics are really an abstraction on top of 3D graphics hardware, and that's why the OS can apply 3D effects to windows and such. \$\endgroup\$
    – jhocking
    Commented Aug 4, 2013 at 22:30

2 Answers 2

5
\$\begingroup\$

Unity has an offical Wiki page on just this - a full blown SpriteManager. It does a lot more than just "draw textures" but it'll do what you want with batching. And it'll give quite a bit of flexibility. Give it a try.

\$\endgroup\$
4
  • \$\begingroup\$ I guess this isn't official enough to be included with the usual Unity installation, and I have to copy its code and import it into my project manually? \$\endgroup\$ Commented Aug 1, 2013 at 23:10
  • \$\begingroup\$ Just create a script and copy in the the source found in the code \$\endgroup\$
    – House
    Commented Aug 1, 2013 at 23:11
  • 1
    \$\begingroup\$ Well, it's community vetted is what I was getting at. :) \$\endgroup\$ Commented Aug 1, 2013 at 23:11
  • \$\begingroup\$ Is there a way to create a sprite with a texture different than the one used in material? It looks like this SpriteManager is supposed to only work with a single texture atlas. \$\endgroup\$ Commented Aug 2, 2013 at 0:56
2
+50
\$\begingroup\$

As already mentioned by Vaughan the most popular free 2D framework for Unity is SpriteManager. However another widely used 2D graphics framework is 2D Toolkit; that's the one my company is using. It costs money, but it's not much and the framework has more features than SpriteManager, like tools to generate sprite atlases.

ADDITION: I just successfully displayed images loaded from the web, here's the code

  StartCoroutine(DownloadImage());
  ...

private IEnumerator DownloadImage() {
  WWW www = new WWW("http://www.server.com/image.png");
  yield return www;

  GameObject sprite = tk2dSprite.CreateFromTexture(www.texture, tk2dRuntime.SpriteCollectionSize.ForTk2dCamera(), new Rect(0, 0, 300, 150), Vector2.zero);
  sprite.transform.position = new Vector3(50, 500, -1000);
}

(because I had an older version of 2D Toolkit installed I had to do this in order for this to work; I believe the latest versions don't have this issue)

\$\endgroup\$
7
  • \$\begingroup\$ Is it possible to use it without sprite atlases? I'm going to need to load textures during runtime, so if there's now way around that, it's no good. I mean I could go with simply drawing separate textures, I don't really need atlases. \$\endgroup\$ Commented Aug 4, 2013 at 22:36
  • \$\begingroup\$ I think so, but I'm honestly not entirely certain because I've never done that. Refer to this in the documentation unikronsoftware.com/2dtoolkit/docs/2.10/html/… \$\endgroup\$
    – jhocking
    Commented Aug 4, 2013 at 22:39
  • \$\begingroup\$ I think that's what I need then. I'll be testing that framework if nothing else comes up. \$\endgroup\$ Commented Aug 4, 2013 at 22:40
  • \$\begingroup\$ I just successfully displayed images loaded from the web, so I'm adding that to my answer. \$\endgroup\$
    – jhocking
    Commented Aug 20, 2013 at 22:16
  • \$\begingroup\$ Unity 4.3 now has a SpriteManager built in, and it can do automatic sprite-atlasing as well \$\endgroup\$ Commented Feb 4, 2014 at 19:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .