Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using UnityEngine;
namespace Giacomelli.Framework
{
/// <summary>
/// #unitytips: Sprites Collection - http://diegogiacomelli.com.br/unitytips-sprites-collection/
/// </summary>
public static class SceneViewInput
{
public static bool GetKeyDown(KeyCode key)
{
return Event.current.type == EventType.KeyDown && Event.current.keyCode == key;
}
}
}
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// #unitytips: Sprites Collection - http://diegogiacomelli.com.br/unitytips-sprites-collection/
/// </summary>
public static class SpriteExtensions
{
public static void UpdateCollider(this Sprite sprite, PolygonCollider2D _collider)
{
var _path = new List<Vector2>();
_path.Clear();
for (int i = 0; i < _collider.pathCount; i++)
_collider.SetPath(i, _path);
_collider.pathCount = sprite.GetPhysicsShapeCount();
for (int i = 0; i < _collider.pathCount; i++)
{
_path.Clear();
sprite.GetPhysicsShape(i, _path);
_collider.SetPath(i, _path);
}
}
}
using UnityEngine;
namespace Giacomelli.Framework
{
/// <summary>
/// #unitytips: Sprites Collection - http://diegogiacomelli.com.br/unitytips-sprites-collection/
/// </summary>
[RequireComponent(typeof(SpriteRenderer))]
public class SpritesCollection : MonoBehaviour
{
[SerializeField]
Sprite[] _sprites;
[SerializeField]
[HideInInspector]
int _selectedSpriteIndex;
public bool HasNextSprite => _selectedSpriteIndex < _sprites.Length - 1;
public bool HasPreviousSprite => _selectedSpriteIndex > 0;
public int SelectedSpriteIndex => _selectedSpriteIndex;
private void Awake()
{
Refresh();
}
private void Refresh()
{
var sr = GetComponent<SpriteRenderer>();
var collider = GetComponent<PolygonCollider2D>();
sr.sprite = _sprites[_selectedSpriteIndex];
if (collider != null)
sr.sprite.UpdateCollider(collider);
}
public bool NextSprite()
{
if (HasNextSprite)
{
_selectedSpriteIndex++;
Refresh();
return true;
}
return false;
}
public bool PreviousSprite()
{
if (HasPreviousSprite)
{
_selectedSpriteIndex--;
Refresh();
return true;
}
return false;
}
}
}
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
namespace Giacomelli.Framework
{
/// <summary>
/// #unitytips: Sprites Collection - http://diegogiacomelli.com.br/unitytips-sprites-collection/
/// </summary>
[CustomEditor(typeof(SpritesCollection))]
public class SpritesCollectionEditor : Editor
{
readonly GUIContent PreviousContent = new GUIContent(" < ", "Type A to previous sprite");
readonly GUIContent NextContent = new GUIContent(" > ", "Type D to previous sprite");
readonly Vector3 ButtonMargin = new Vector3(10f, 10f, 0);
SpritesCollection _target;
SpriteRenderer _sr;
SerializedProperty _selectedSpriteIndex;
GUIStyle _style;
bool _changed;
private void OnEnable()
{
_target = (SpritesCollection)target;
_sr = _target.GetComponent<SpriteRenderer>();
_selectedSpriteIndex = serializedObject.FindProperty("_selectedSpriteIndex");
_style = new GUIStyle
{
alignment = TextAnchor.MiddleCenter
};
}
void OnSceneGUI()
{
var p = _target.transform.position;
var ext = _sr.bounds.extents;
var r1 = HandleUtility.WorldPointToSizedRect(p - new Vector3(ext.x, ext.y), PreviousContent, _style);
r1 = new Rect(r1.position.x + ButtonMargin.x, r1.position.y + ButtonMargin.y, r1.width, r1.height);
var r2 = new Rect(r1.position.x + r1.width, r1.position.y, r1.width, r1.height);
Handles.BeginGUI();
_changed = false;
GUI.enabled = _target.HasPreviousSprite;
if (GUI.Button(r1, PreviousContent) || SceneViewInput.GetKeyDown(KeyCode.A))
{
_target.PreviousSprite();
_changed = true;
}
GUI.enabled = _target.HasNextSprite;
if (GUI.Button(r2, NextContent) || SceneViewInput.GetKeyDown(KeyCode.D))
{
_target.NextSprite();
_changed = true;
}
Handles.EndGUI();
if (_changed)
{
_selectedSpriteIndex.intValue = _target.SelectedSpriteIndex;
serializedObject.ApplyModifiedProperties();
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment