Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
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( ... )" –  Benjamin Danger Johnson Aug 1 '13 at 23:03
    
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. –  jhocking Aug 4 '13 at 22:30

2 Answers 2

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.

share|improve this answer
    
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? –  user1306322 Aug 1 '13 at 23:10
    
Just create a script and copy in the the source found in the code –  Byte56 Aug 1 '13 at 23:11
1  
Well, it's community vetted is what I was getting at. :) –  Vaughan Hilts Aug 1 '13 at 23:11
    
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. –  user1306322 Aug 2 '13 at 0:56

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)

share|improve this answer
    
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. –  user1306322 Aug 4 '13 at 22:36
    
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/… –  jhocking Aug 4 '13 at 22:39
    
I think that's what I need then. I'll be testing that framework if nothing else comes up. –  user1306322 Aug 4 '13 at 22:40
    
I just successfully displayed images loaded from the web, so I'm adding that to my answer. –  jhocking Aug 20 '13 at 22:16
    
Unity 4.3 now has a SpriteManager built in, and it can do automatic sprite-atlasing as well –  BlueRaja - Danny Pflughoeft Feb 4 at 19:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.