Custom Inspector erasing data on compile
Asked Answered
P

1

0

Hello!

So, I’ve written a custom inspector for a custom object called “ProcedureHelper”. ProcedureHelper is a custom class (implements MonoBehavior) that contains a list of "AbstractProcedure"s, which is an interface I’ve implemented in several classes. I can add several AbstractProcedures from the custom inspector, and the inspector will list them all one by one.

However, as soon as a recompile, the inspector data disappears. A Debug.Log reveals that the list contained goes from full to null.

Is there any known cause to why this happens? What can I do to keep this data? Thank you!

Here is the Custom Editor

using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
 

[CustomEditor(typeof(ProcedureHelper))]
public class ProcedureHelperEditor : Editor
{
	ProcedureType typeToAdd = ProcedureType.Default;
	
	public override void OnInspectorGUI()
	{
		// Variables
		ProcedureHelper controller = target as ProcedureHelper; 
		
		// Set our controls like normal
		EditorGUIUtility.LookLikeControls();
		DrawDefaultInspector ();

		// Add Button 
		EditorGUILayout.BeginHorizontal();
			typeToAdd = (ProcedureType)EditorGUILayout.EnumPopup("Loop Type:", typeToAdd);
			if (GUILayout.Button("Add Procedure"))
			{
				controller.AddProcedure(typeToAdd);
			}
		EditorGUILayout.EndHorizontal();
		
		// Begin Showing Procedures
		if (controller.procedures != null)
		for (int i = 0; i < controller.procedures.Count; i++)
		{
			EditorGUILayout.BeginHorizontal();
				EditorGUILayout.LabelField(controller.procedures*.type.ToString());*
  •  	EditorGUILayout.EndHorizontal();*
    
  •  }*
    
  •  // If we've changed the gui, set it to dirty*
    
  •  if (GUI.changed == true)*
    
  •  {*
    
  •  	EditorUtility.SetDirty (target);*
    
  •  }*
    
  • }*

}
Here is the class you’re editing
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class ProcedureHelper : MonoBehaviour
{

  • [HideInInspector]*

  • private List m_procedures;*

  • public List procedures {*

  •  get { return this.m_procedures; }	*
    
  • }*

  • public void AddProcedure(ProcedureType type)*

  • {*

  •  if (m_procedures == null)*
    
  •  	m_procedures = new List<AbstractProcedure>();*
    
  •  switch (type)*
    
  •  {*
    
  •  case ProcedureType.Default:*
    
  •  	break;*
    
  •  case ProcedureType.GoToLevel:*
    
  •  	m_procedures.Add(new GoToLevelProcedure());*
    
  •  	break;*
    
  •  }*
    
  • }*
    }
    Here is the interface
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

public interface AbstractProcedure
{

  • ProcedureType type*
  • {*
  •  get;*
    
  • }*
  • void Run(GameObject obj);*
    }

public enum ProcedureType { Default, GoToLevel, ShowObjects, HideObjects };
Here is an Implementation of the Interface
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class GoToLevelProcedure : System.Object, AbstractProcedure
{

  • public ProcedureType type { get { return ProcedureType.GoToLevel; } }*
  • public Levels_t LevelToLoad = Levels_t.NONE;*
  • public string GoToChapter = “”;*

}

Practice answered 31/8, 2023 at 11:21 Comment(0)
A
0

mistake 1

[System.Serializable]
public class ProcedureHelper : MonoBehaviour
{
    [HideInInspector]
    private List m_procedures;

this List will not be serialized cause it’s private. so every time you reload unity or compile or something that reinstantiate the ProcedureHelper, all data will be erased

mistake 2


source code is the same. Unity will not check object AbstractProcedure for any fields that type AbstractProcedure not contains in itself. so, for any field ingerited from AbstractProcedure all fields that are implemented in child but not in parent will be ignored.

Alhambra answered 27/8, 2012 at 19:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.