Hi unity boffins,
I have an editor script that provides both a OnInspectorGUI and OnSceneGUI function that are both trying to support undo via Undo.SetSnapshotTarget. I my case the OnSceneGUI provides Handles.PositionHandle to edit spline control points, while the OnInspectorGUI provides int, float and Vector3 fields to edit various parameters.
My problem is I get a constant error message (as per title) every time I edit one of the PositionHandle gizmos?
It doesn't seem to cause any issues (undo works fine) except cluttering my log console.
If I double click on the error in the log it takes me to the: Vector3 newCp = Handles.PositionHandle( cp, handleRotation ); line of my editor script.
The functions that the error refers (SaveSnapshot and MakeSnapshot) are emanating from Handles.PositionHandle but these seem to be deprecated? or at least they are now internal API and not part of the public API documentation.
Because I have a lot of interdependent code I've quickly made a small example of how I call the undo related functions (this should produce the error). Excuse the code compactness.
There are some questions in the comments of the code too.
Thanks
file: ExampleSpline.cs
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
[ AddComponentMenu("Splines/Example Spline") ]
[ System.Serializable ]
public class ExampleSpline : MonoBehaviour
{
public List<Vector3> knots = new List<Vector3>();
public int numControlPoints { get { return knots.Count-2; } }
public ExampleSpline() {
knots.Add( new Vector3(0f,0f,0f) );
knots.Add( new Vector3(1f,0f,0f) );
knots.Add( new Vector3(2f,0f,1f) );
knots.Add( new Vector3(2f,0f,2f) );
knots.Add( new Vector3(2f,0f,4f) );
}
public void AddControlPoint() {
if ( knots.Count > 1 ) {
knots.Add( knots[knots.Count-1]
+ (knots[knots.Count-1]-knots[knots.Count-2]) );
} else { knots.Add( Vector3.zero ); }
}
public Vector3 GetControlPoint( int idx ) {
return knots[idx+1];
}
public void MoveControlPoint( int idx, Vector3 v ) {
knots[idx+1] = v;
}
public void RemoveControlPoint( int idx ) {
knots.RemoveAt( idx+1 );
}
}
file: Editor/ExampleSplineEditor.cs
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(ExampleSpline))]
public class ExampleSplineEditor : Editor {
public ExampleSpline m_target;
void OnEnable() { m_target = (ExampleSpline)target; }
public override void OnInspectorGUI() {
Undo.SetSnapshotTarget(m_target,"Modify Spline");
bool splineModified = false;
for (int i=0; i < m_target.numControlPoints; i++) {
EditorGUILayout.BeginHorizontal();
Vector3 CP = m_target.GetControlPoint( i );
Vector3 newCP = EditorGUILayout.Vector3Field( "CP: "+(i+1).ToString(), CP );
if ( CP != newCP ) {
m_target.MoveControlPoint( i, newCP );
}
if ( GUILayout.Button("X") ) {
m_target.RemoveControlPoint( i );
splineModified = true;
}
EditorGUILayout.EndHorizontal();
}
if( GUI.changed || splineModified ){
EditorUtility.SetDirty(m_target);
// Register the undos when something changes
Undo.CreateSnapshot();
Undo.RegisterSnapshot();
}
// Can anyone explain if or why I need to call this here?
Undo.ClearSnapshotTarget();
}
void OnSceneGUI() {
if ( m_target.numControlPoints > 1 ) {
// Is it correct to call this here as well as OnInspectorGUI()?
Undo.SetSnapshotTarget(m_target,"Adjust Spline Handles");
bool splineModified = false;
for (int i=0; i < m_target.numControlPoints; ++i) {
Vector3 cp = m_target.GetControlPoint( i );
Vector3 newCp = Handles.PositionHandle( cp, Quaternion.identity );
if ( cp != newCp ) {
m_target.MoveControlPoint( i, newCp );
splineModified = true;
}
}
// Is GUI.changed relevant inside OnSceneGUI()?
if ( GUI.changed || splineModified ) {
EditorUtility.SetDirty( m_target );
Undo.CreateSnapshot();
Undo.RegisterSnapshot();
}
// Again, do I acutally need to call this?
Undo.ClearSnapshotTarget();
}
}
}
changing it to -1 works as well; depth above 1 does not make a difference in 2D
– officialmparts