Question by
zozazu · Mar 18 at 02:14 PM ·
custom editor
Having a hard time getting custom editor script changes to save
Hello, I have been trying to get a custom editor script to persist changes that I make in the editor but nothing that I have tried has worked so far. I have the following list in a monobehaviour:
public class SnapPoint : MonoBehaviour
{
public List<SnapPointWhiteList> SnapPointWhiteList = new List<SnapPointWhiteList>();
}
The snappoint class is defined as:
[Serializable]
public class SnapPointWhiteList
{
public BuildingPieceBehaviour TargetPiece { get; }
public Dictionary<SnapPoint, bool> AllowedSnapPoints { get; } = new Dictionary<SnapPoint, bool>();
public SnapPointWhiteList(BuildingPieceBehaviour targetPiece)
{
TargetPiece = targetPiece;
foreach (var snapPoint in targetPiece.GetComponentsInChildren<SnapPoint>())
{
AllowedSnapPoints.Add(snapPoint, true);
}
}
}
My custom editor script that I'm using:
public class SnapPointInspector : Editor
{
private SnapPoint Target => (SnapPoint) target;
private readonly Dictionary<int, bool> _foldouts = new Dictionary<int, bool>();
private List<SnapPoint> _snapPointNames = new List<SnapPoint>();
public override void OnInspectorGUI()
{
serializedObject.Update();
GUILayout.Label("Allowed Snap Points");
for (var i = 0; i < Target.SnapPointWhiteList.Count; i++)
{
_snapPointNames = Target.SnapPointWhiteList[i].AllowedSnapPoints.Keys.ToList();
SetFoldout(i, EditorGUILayout.Foldout(GetFoldout(i), Target.SnapPointWhiteList[i].TargetPiece.GetBuildingPieceName(), true));
if (GetFoldout(i))
{
foreach (var snapPoint in _snapPointNames)
{
// GUILayout.Label(snapPoint.Key.name, EditorStyles.label, GUILayout.ExpandWidth(true));
Target.SnapPointWhiteList[i].AllowedSnapPoints[snapPoint] =
GUILayout.Toggle(Target.SnapPointWhiteList[i].AllowedSnapPoints[snapPoint], snapPoint.name);
}
}
}
HandleDragAndDrop();
EditorUtility.SetDirty(target);
serializedObject.ApplyModifiedProperties();
}
And where it's getting saved in the HandleDragAndDrop() method:
Target.SnapPointWhiteList.Add(new SnapPointWhiteList(draggedPiece));
EditorUtility.SetDirty(target);
I've also tried the serializedProperty route and had no luck there. When I edit the values in the editor nothing is persisting.
Comment
Your answer
