Created
September 13, 2022 09:33
-
-
Save synchrok/491efbccf2c311d5b5f1cfd579a61dff to your computer and use it in GitHub Desktop.
[Unity] X-Axis flip to SpriteShapeController spline
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 System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.U2D; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
public class SpriteShapeFlip { | |
#if UNITY_EDITOR | |
[MenuItem("CONTEXT/SpriteShapeController/FlipX")] | |
public static void FlipX(MenuCommand command) { | |
var target = (SpriteShapeController) command.context; | |
if (target == null) | |
return; | |
var lst = new List<Vector3>(); | |
var min = float.MaxValue; | |
var max = float.MinValue; | |
for (var i = 0; i < target.spline.GetPointCount(); i++) { | |
var p = target.spline.GetPosition(i); | |
lst.Add(p); | |
min = Mathf.Min(min, p.x); | |
max = Mathf.Max(max, p.x); | |
} | |
Undo.RecordObject(target, "SpriteShapeController FlipX"); | |
for (var i = 0; i < lst.Count; i++) { | |
var p = lst[i]; | |
p.x = max - (p.x - min); | |
target.spline.SetPosition(i, p); | |
var lt = target.spline.GetLeftTangent(i); | |
lt.x = -lt.x; | |
target.spline.SetLeftTangent(i, lt); | |
var rt = target.spline.GetRightTangent(i); | |
rt.x = -rt.x; | |
target.spline.SetRightTangent(i, rt); | |
} | |
target.RefreshSpriteShape(); | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment