Created
September 5, 2020 19:27
-
-
Save sabresaurus/3321755946a07236340c27e8f63f0809 to your computer and use it in GitHub Desktop.
Getting Unity Sprite Outline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEditor; | |
using UnityEngine; | |
public class DisplaySpriteOutline : MonoBehaviour | |
{ | |
[SerializeField] private Sprite sprite; | |
private void OnDrawGizmos() | |
{ | |
TextureImporter textureImporter = (TextureImporter) AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(sprite.texture)); | |
var spriteOutlineProperty = new SerializedObject(textureImporter).FindProperty("m_SpriteSheet.m_Outline").GetArrayElementAtIndex(0); | |
Vector2[] spriteOutline = new Vector2[spriteOutlineProperty.arraySize]; | |
for (int i = 0; i < spriteOutlineProperty.arraySize; i++) | |
{ | |
spriteOutline[i] = spriteOutlineProperty.GetArrayElementAtIndex(i).vector2Value; | |
} | |
for (var index = 0; index < spriteOutline.Length; index++) | |
{ | |
Vector2 vertex1 = spriteOutline[index]; | |
Vector2 vertex2 = spriteOutline[(index + 1) % spriteOutline.Length]; | |
Gizmos.DrawLine(vertex1, vertex2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment