- Home /
Question by
eidnog · Sep 08, 2018 at 04:00 PM ·
serializationcustomeditor
Custom Editor doesn't keep changes made on Inspector
I was trying to create a Custom Editor to help designing a simple battle system, but I stumbled across a problem. The editor works when I'm on Play Mode, but any changes made on Editor Mode doesn't persist on Play Mode. It seems like everything is being serialized correctly, so I really don't have any idea about what could be causing this problem. Here is the code at the moment: Here's my CustomEditor Script:
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Round))]
public class RoundEditor : Editor
{
Round round;
public void Awake()
{
round = target as Round;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
for (int i = 0; i < round.Enemies.Length; i++)
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Enemy " + (i + 1), GUILayout.Width(70));
round.Enemies[i] = (Enemy)EditorGUILayout.ObjectField(round.Enemies[i], typeof(Enemy), true);
if (round.Enemies[i] != null)
round.Enemies[i].currentPattern = ToPattern(round.Enemies[i], EditorGUILayout.Popup(ToIndex(round.Enemies[i], round.Enemies[i].currentPattern), round.Enemies[i].PatternOptions));
EditorGUILayout.EndHorizontal();
}
}
public int ToIndex(Enemy enemy, Pattern pattern)
{
int index = 0;
for (int i = 0; i < enemy.Patterns.Length; i++)
{
if (enemy.Patterns[i] == pattern)
{
index = i;
break;
}
}
return index;
}
public Pattern ToPattern(Enemy enemy, int index)
{
Debug.Log("Converting to... " + index);
Pattern pattern = enemy.Patterns[index];
return pattern;
}
}
And the other classes that I use:
[System.Serializable]
public class Round : MonoBehaviour
{
[SerializeField]
private Enemy[] enemies;
public Enemy[] Enemies
{
get { return enemies; }
set { enemies = value; }
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Enemy : MonoBehaviour
{
#region ATTRIBUTES
[SerializeField]
private Pattern[] patterns;
public Pattern currentPattern;
private Animator animator;
#endregion
#region PROPERTIES
public Pattern[] Patterns
{ get { return patterns; } }
public string[] PatternOptions
{
get
{
string[] options = new string[patterns.Length];
for (int i = 0; i < patterns.Length; i++)
options[i] = patterns[i].Title;
return options;
}
}
#endregion
#region METHODS
public void Update()
{
currentPattern.Shoot(transform.position, transform.rotation);
}
public void Attack(Pattern pattern)
{
pattern.Shoot(transform.position, transform.rotation);
}
#endregion
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Pattern
{
#region ATTRIBUTES
[SerializeField]
private string title = "Pattern";
[SerializeField]
private ProjectileData[] projectiles;
private float index = 0;
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}
#endregion
#region METHODS
public void Shoot(Vector3 position, Quaternion rotation)
{
bool newIndex = false;
foreach (ProjectileData projectile in projectiles)
{
if (Time.time > projectile.TimeToFire)
{
projectile.TimeToFire = Time.time + 1 / projectile.FireRate;
Projectile clone = MonoBehaviour.Instantiate(projectile.Projectile, position, rotation);
clone.SetAttributes(projectile.Title, projectile.Speed, projectile.Angle, index);
newIndex = true;
}
else
continue;
}
if (newIndex)
index++;
}
#endregion
}
Comment
Answer by dan_wipf · Sep 08, 2018 at 06:05 PM
add this line at the end of the editor:
serializedObject.ApplyModifiedProperties();
use propertyfield ins$$anonymous$$d of object field
https://docs.unity3d.com/ScriptReference/EditorGUILayout.PropertyField.html