Unity2D – Sprites 3

13 01 2014

Actually I kind of forgotten where I left off. I really need to start attaching code files. Might create a shared repository for all this stuff over at Bitbucket. Anyway; yes, sprites in Unity. Have I mentioned that this has nothing to do with the new Unity2D update which the lovely boys and girls over at the Unity team just released? Did I also mention that the Unity2D update is mostly not really that useful?

Trolling aside, let’s see where I was.
Hmmm…. Sprites… Meshes… GameObjects… Ah yeah, making sprite-like objects in the editor.

Alright, with the first approach done I’d like to go for the third approach since it’s only slightly more complex. In fact, it’s not even slightly more complex so we’ll get it out of the way right now. Remember how in our first attempt we’ve created the sprite node. All we need to do now is simply create another node and parent the sprite node to it.

var selected_texture = (Texture2D) Selection.activeObject;
var sprite_node = new GameObject("Hero");
var graphic_node = NewSpriteNode.Make(selected_texture);
graphic_node.transform.parent = sprite_node.transform;

And voila, we have a proper transform hierarchy where the graphic_node itself acts as the pivot point of the sprite since it’s position is localized to the sprite_node’s space. And if we really want to be sexy here we can always set the graphic_node’s hide state and not even see the resulting hierarchy ugliness.

The second approach is more interesting and more complex since it involves custom inspectors (which Unity for some strange occultish reason decides to call Editor).

So the first thing we need to do is create a ‘Sprite’ behaviour which will help us control the underlying mesh via its associated inspector/editor. What goes into this behaviour you ask? Let’s see what we want from our sprite and let’s keep it simple for now; pivot and scale.

public class Sprite
  : MonoBehviour
{
  public Vector2 Pivot;
  public Vector2 Scale;
}

With that in tow we shall continue onward with our spriting adventure next time.

“Some day you’ll return to me; Your valleys and your farms; And you’ll no longer burn To be brothers in arms”

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: