- Home /
How to prevent prefab of custom editor inspector from overriding array size.
I created a custom editor for the inspector of one of my scripts. The issue is, when I create a prefab of an object with the script attached, the prefab's array will override the array of every instance to be the same.
What causes this and how can I prevent it?
EDIT: I am running unity version 5.6.2f1
Editor Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(PointMovingPlatform))]
public class PointMovingPlatformEditor : Editor {
PointMovingPlatform pmp;
public void OnEnable()
{
pmp = (PointMovingPlatform)target;
}
public override void OnInspectorGUI()
{
///////////////////////////////////////
//Display speed
///////////////////////////////////////
pmp.speed = EditorGUILayout.FloatField("Speed", pmp.speed);
GUILayout.Space(20);
/////////////////////////////////////////
//Display waypoints array
// - Allow adding/removing of points
/////////////////////////////////////////
if (GUILayout.Button("Add Waypoint"))
{
AddWaypoint();
}
for (int i = 0; i < pmp.waypoints.Count; i++)
{
GUILayout.BeginHorizontal();
GUILayout.Label("Waypoint " + i.ToString());
GameObject old = pmp.waypoints[i];
pmp.waypoints[i] = (GameObject)EditorGUILayout.ObjectField(pmp.waypoints[i], typeof(GameObject), true);
if(old != pmp.waypoints[i])
{
if(old.name == "Waypoint")
DestroyImmediate(old);
}
if (GUILayout.Button("[X]"))
{
RemoveWaypoint(i);
}
GUILayout.EndHorizontal();
}
//Display original for reference
GUILayout.Space(40);
GUILayout.Label("ORIGINAL INSPECTOR");
base.OnInspectorGUI();
}
void AddWaypoint()
{
GameObject newWP = new GameObject("Waypoint");
newWP.transform.position = new Vector3(pmp.platform.transform.position.x, pmp.platform.transform.position.y, pmp.platform.transform.position.z);
newWP.transform.parent = pmp.transform;
pmp.waypoints.Add(newWP);
}
void RemoveWaypoint(int index)
{
DestroyImmediate(pmp.waypoints[index].gameObject);
pmp.waypoints.RemoveAt(index);
}
}
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PointMovingPlatform : MonoBehaviour {
public float speed;
public GameObject platform;
public List<GameObject> waypoints;
//public Transform[] waypoints;
private Vector3 direction;
private Transform destination;
private int currentWaypoint = 0;
void Start () {
if(waypoints.Count > 0)
{
destination = waypoints[currentWaypoint].transform;
direction = (destination.position - platform.transform.position).normalized;
}
}
void Update()
{
platform.transform.Translate(direction * speed * Time.deltaTime);
float distance = Vector3.Distance(platform.transform.position, destination.position);
if (distance < speed * Time.deltaTime || distance < speed / 20f)
{
platform.transform.position = destination.transform.position;
NextDestination();
}
}
private void OnDrawGizmos()
{
for(int i = 0; i < waypoints.Count; i++)
{
Gizmos.color = Color.grey;
Gizmos.DrawWireCube(waypoints[i].transform.position, platform.transform.localScale);
Gizmos.color = Color.black;
if(i + 1 >= waypoints.Count)
{
Gizmos.DrawLine(waypoints[waypoints.Count - 1].transform.position, waypoints[0].transform.position);
} else
{
Gizmos.DrawLine(waypoints[i].transform.position, waypoints[i + 1].transform.position);
}
if(waypoints.Count > 0)
{
Gizmos.DrawLine(platform.transform.position, waypoints[currentWaypoint].transform.position);
}
}
}
void NextDestination()
{
if (waypoints.Count > 0)
{
currentWaypoint++;
if(currentWaypoint >= waypoints.Count)
{
currentWaypoint = 0;
}
destination = waypoints[currentWaypoint].transform;
direction = (destination.position - platform.transform.position).normalized;
}
}
}
I apologize for the large amount of code, but I just want to ensure there is enough content to determine why this issue arises. Thank you in advance.
Your code worked fine for me. $$anonymous$$aybe your instance of unity is damaged! Because this is highly irregular behavior. It shouldn't happen.
To be sure, did you reload the scene after adding a few waypoints? Because that is when the instances reset to the default prefab.
To be clear as well: The objects get created, initially get put into the array, and function like normal. As soon as I reload the scene or play the scene, the array size gets reset to the prefab size, the created waypoints still exist, but are no longer in the array.
Answer by uulamock_unity · Oct 09, 2017 at 01:53 AM
After some very deep research I discovered that it is simply because I am not using SerializedProperty
data type for handling things. SerializedProperty
has plenty of self handling for issues such as Undo and Prefab.
Therefore, if I do something like this:
SerializedProperty m_Speed;
SerializedProperty m_Waypoints;
public void OnEnable()
{
m_Speed = this.serializedObject.FindProperty("speed");
m_Waypoints = this.serializedObject.FindProperty("waypoints");
}
public override void OnInspectorGUI()
{
this.serializedObject.Update();
EditorGUILayout.PropertyField(m_Speed);
if(m_Speed.floatValue < 0)
{
m_Speed.floatValue = 0f;
}
GUILayout.Space(40);
if(GUILayout.Button("Add Waypoint"))
{
AddWaypoint();
}
for(int i = 0; i < m_Waypoints.arraySize(); i++) {
EditorGUILayout.PropertyField(m_Waypoints.GetArrayElementAtIndex(i));
}
this.serializedObject.ApplyModifiedProperties();
}
It functions perfectly fine when prefabbed.
////////////////////
// This resource was very useful in understanding how to use SerializedProperty
with arrays
////////////////////
Also, for a question very similar with a valuable answer is here