Hey,
I followed this youtube tutorial on Bézier curves. But when I add the OnSceneGui method, for some reason the FreeMoveHandles won’t show up, and also all the handles, for moving objects in the scene disappear as well. I have gizmos enabled, all the methods are being called correctly, my camera is behind the object so I don’t know what might be the problem.
These are the scripts for the tutorial. I checked and they are the same as mine until a point, where I had to stop because I couldn’t follow along anymore, thanks to the problem.
Regardless, this is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Path
{
[SerializeField, HideInInspector]
List<Vector2> points;
public Path(Vector2 centre)
{
points = new List<Vector2>
{
centre + Vector2.left,
centre + (Vector2.left + Vector2.up) * 0.5f,
centre + (Vector2.right + Vector2.down) * 0.5f,
centre + Vector2.right
};
}
public void AddSegment(Vector2 anchorPos)
{
points.Add(points[points.Count - 1] * 2 - points[points.Count - 2]);
points.Add((points[points.Count - 1] + anchorPos) / 2);
points.Add(anchorPos);
}
public Vector2[] GetPointsInSegment(int i)
{
return new Vector2[]
{
points[i * 3],
points[i * 3 + 1],
points[i * 3 + 2],
points[i * 3 + 3],
};
}
public Vector2 this[int i]
{
get
{
return points[i];
}
}
public int NumSegments
{
get
{
return (points.Count - 1) / 3;
}
}
public int NumPoints
{
get
{
return points.Count;
}
}
public void MovePoint(int i, Vector2 pos)
{
points[i] = pos;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathCreator : MonoBehaviour
{
[HideInInspector]
public Path path;
public void CreatePath()
{
path = new Path(transform.position);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(PathCreator))]
public class PathEditor : Editor
{
PathCreator creator;
Path path;
private void OnSceneGUI()
{
Draw();
}
void Draw()
{
Handles.color = Color.red;
for (int i = 0; i < path.NumPoints; i++)
{
Vector2 v2 = creator.transform.position;
Vector2 newPos = Handles.FreeMoveHandle(path[i] + v2, Quaternion.identity, 0.5f, Vector2.zero, Handles.SphereHandleCap);
if (path[i] != newPos)
{
Undo.RecordObject(creator, "Move point");
path.MovePoint(i, newPos);
}
}
}
private void OnEnable()
{
creator = (PathCreator)target;
if (creator.path == null)
{
creator.CreatePath();
}
path = creator.path;
}
}