patterncsharpMinor
Unity3D DirectionalSprite
Viewed 0 times
unity3ddirectionalspritestackoverflow
Problem
I've created a simple utility class for use in my Unity2D project, which allows a Sprite to have separate sprites for when it is facing East, North, West, or South (that's the order that z-axis rotation places them in). There aren't too many Unity-isms in the code, and the part that I'm most concerned with is the internal logic. It seems like I'm duplicating a lot of code to handle each direction the sprite can face; is there a way to better handle this? [[As this is a relatively simple 2D game in early development, clarity and maintainability are preferred even if it takes a performance hit; e.g. using a dynamic lookup versus hard-coding it.]]
I've done my best to explain the Unity-isms below the code.
```
using UnityEngine;
using System;
namespace towerofbabble.scripts
{
///
/// A controller for a SpriteRenderer with directional sprites
///
[RequireComponent(typeof(SpriteRenderer))]
public class DirectionalSprite : MonoBehaviour
{
[SerializeField]
private SpriteRenderer _spriteRenderer;
#if UNITY_EDITOR
void Awake()
{
SpriteRenderer spriteRenderer = this.GetComponent();
if (_spriteRenderer != spriteRenderer)
{
Debug.LogError("SpriteRenderer is not assigned correctly.", this);
_spriteRenderer = spriteRenderer;
}
Sprite sprite = _spriteRenderer.sprite;
updateSprite();
if (sprite != _spriteRenderer.sprite)
{
Debug.LogError("Sprite is not assigned correctly.", this);
}
}
#endif
[SerializeField]
private Sprite _east;
[SerializeField]
private Sprite _north;
[SerializeField]
private Sprite _west;
[SerializeField]
private Sprite _south;
public enum Direction
{
RIGHT = 0,
UP = 1,
LEFT = 2,
DOWN = 3
}
///
I've done my best to explain the Unity-isms below the code.
```
using UnityEngine;
using System;
namespace towerofbabble.scripts
{
///
/// A controller for a SpriteRenderer with directional sprites
///
[RequireComponent(typeof(SpriteRenderer))]
public class DirectionalSprite : MonoBehaviour
{
[SerializeField]
private SpriteRenderer _spriteRenderer;
#if UNITY_EDITOR
void Awake()
{
SpriteRenderer spriteRenderer = this.GetComponent();
if (_spriteRenderer != spriteRenderer)
{
Debug.LogError("SpriteRenderer is not assigned correctly.", this);
_spriteRenderer = spriteRenderer;
}
Sprite sprite = _spriteRenderer.sprite;
updateSprite();
if (sprite != _spriteRenderer.sprite)
{
Debug.LogError("Sprite is not assigned correctly.", this);
}
}
#endif
[SerializeField]
private Sprite _east;
[SerializeField]
private Sprite _north;
[SerializeField]
private Sprite _west;
[SerializeField]
private Sprite _south;
public enum Direction
{
RIGHT = 0,
UP = 1,
LEFT = 2,
DOWN = 3
}
///
Solution
Vector2Direction and DirectionVector2You should consider to rename this methods to
ToDirection() and ToVector2(). In this way it is more clear what the methods are doing, ideally this methods would live in a Convert class. Initializing for each method a dictionary would be the way to go IMO.Assume you have done this and named this dictionary
vectorsDictionary then your former Vector2Direction() method would look like sopublic static Direction ToDirection(Vector2 vector)
{
Direction direction;
if (vectorsDictionary.TryGetValue(vector.normalized, out direction))
{
return direction;
}
throw new ArgumentException("Argument must be a vector along an axis.", "vector");
}In the same way you can do it for
DirectionVector2 and this would be also possible for the updateSprite() method.Code Snippets
public static Direction ToDirection(Vector2 vector)
{
Direction direction;
if (vectorsDictionary.TryGetValue(vector.normalized, out direction))
{
return direction;
}
throw new ArgumentException("Argument must be a vector along an axis.", "vector");
}Context
StackExchange Code Review Q#127255, answer score: 4
Revisions (0)
No revisions yet.