Skip to content

Instantly share code, notes, and snippets.

@synchrok
Created September 13, 2022 09:33
Show Gist options
  • Save synchrok/491efbccf2c311d5b5f1cfd579a61dff to your computer and use it in GitHub Desktop.
Save synchrok/491efbccf2c311d5b5f1cfd579a61dff to your computer and use it in GitHub Desktop.
[Unity] X-Axis flip to SpriteShapeController spline
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