- Home /
Question by
Hacky · Mar 22, 2013 at 05:05 PM ·
custom editorplaymodecustom inspector
changing value of child objects in root object not persistent?
I have a root object as gamefield with many child objects (cells). The root objects script has a custom editor script. With these script I instantiate the cells and can change a value of each cell (isWalkable). When I do this with my custom editor and go into playmode I lost all my changes. When I do this manually in each cell and go into playmode, I lost nothing. Do you have any suggestions?
Here are my scripts:
public class Cell : MonoBehaviour
{
public bool isWalkable;
}
public class GameField : MonoBehaviour
{
public GameObject cellPrefab;
public Vector2 gamefieldSize = new Vector2(10.0f, 10.0f);
public Cell[,] cells;
public void BuildGrid()
{
if (cellPrefab == null) return;
cells = new Cell[(int)gamefieldSize.x, (int)gamefieldSize.y];
for (int y = 0; y < gamefieldSize.y; y++)
{
for (int x = 0; x < gamefieldSize.x; x++)
{
GameObject cell = PrefabUtility.InstantiatePrefab(cellPrefab) as GameObject;
cell.name = "Cell-" + y + "-" + x;
cell.transform.parent = transform;
cell.transform.position = transform.position + x * transform.right + y * transform.forward;
cell.transform.localRotation = Quaternion.identity;
cells[x, y] = cell.GetComponent<Cell>();
if (cells[x, y] == null)
cells[x, y] = cell.AddComponent<Cell>();
cells[x, y].isWalkable = true;
}
}
}
}
[CustomEditor(typeof(GameField))]
public class GameFieldEditor : Editor {
private bool m_isInEditMode = false;
private GameField m_gamefield;
private SerializedProperty m_cellPrefab;
private SerializedProperty m_gamefieldSize;
private Vector2 m_tmpGamefieldSize;
private bool m_gamefieldSizeFolded;
private void OnEnable()
{
m_gamefield = target as GameField;
m_cellPrefab = serializedObject.FindProperty("cellPrefab");
m_gamefieldSize = serializedObject.FindProperty("gamefieldSize");
m_tmpGamefieldSize = m_gamefieldSize.vector2Value;
SceneView.onSceneGUIDelegate += GameFieldUpdate;
}
private void OnDisable()
{
SceneView.onSceneGUIDelegate -= GameFieldUpdate;
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(m_cellPrefab, new GUIContent("Cell Prefab"));
m_gamefieldSizeFolded = EditorGUILayout.Foldout(m_gamefieldSizeFolded, "Resolution of Gamefield");
if (m_gamefieldSizeFolded)
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.Space();
EditorGUILayout.BeginVertical();
m_tmpGamefieldSize.x = Mathf.Clamp(EditorGUILayout.IntField("Columns", (int)m_tmpGamefieldSize.x), 1, int.MaxValue);
m_tmpGamefieldSize.y = Mathf.Clamp(EditorGUILayout.IntField("Rows", (int)m_tmpGamefieldSize.y), 1, int.MaxValue);
if (GUILayout.Button("Set Resolution"))
{
m_gamefieldSize.vector2Value = m_tmpGamefieldSize;
m_gamefield.BuildGrid();
}
EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.Separator();
if (!m_isInEditMode)
{
m_isInEditMode = GUILayout.Button("Edit GameField");
}
else
{
m_isInEditMode = !(GUILayout.Button("Leave EditMode"));
}
if (GUI.changed)
{
EditorUtility.SetDirty(target);
}
serializedObject.ApplyModifiedProperties();
}
private void GameFieldUpdate(SceneView sv)
{
Event current = Event.current;
int controlID = GUIUtility.GetControlID(FocusType.Passive);
if (m_isInEditMode)
{
foreach (Cell cell in m_gamefield.cells)
{
Handles.color = cell.isWalkable ? Color.green : Color.red;
Handles.SphereCap(controlID, cell.transform.position, cell.transform.rotation, 1.0f);
}
if (current.type == EventType.mouseUp && current.button == 0 && !current.alt)
{
GameObject go = HandleUtility.PickGameObject(current.mousePosition, false);
Cell cell = null;
if (go != null)
{
cell = go.GetComponent<Cell>();
}
if (cell == null)
{
foreach (Cell c in m_gamefield.cells)
{
if (HandleUtility.DistanceToCircle(c.transform.position, 1.0f) < 1.0f)
{
cell = c;
break;
}
}
}
if (cell != null)
cell.isWalkable = !cell.isWalkable;
current.Use();
}
}
if (current.type == EventType.layout)
HandleUtility.AddDefaultControl(controlID);
}
}
Comment
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
EditorGUILayout.PropertyField cannot draw custom class? 2 Answers
Inspector Overlapping Text Label at a Position 1 Answer
Custom editor window does not show prefab overrides 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers