Custom editor for prefabs

Hi!

I have a customEditor script for component called “Upgradeable” that displays array of data differently in inspector. Now while this approach is working fine on scene objects (I can modify data via my customEditor) I can not get it to work on non-instantiated prefabs. It seems that the data just does not get saved.

Here is my code:

`#pragma strict
@CustomEditor(Upgradeable)
class UpgradeableEditor extends Editor{

private var m_Object : SerializedObject;
private var m_upgradesCount : SerializedProperty;

private static var upgradesSize : String = "upgrades.Array.size";
private static var upgradesData : String = "upgrades.Array.data[{0}]";

function GetUpgradesArray(){
	this.m_upgradesCount = this.m_Object.FindProperty(upgradesSize);
	var upgradesArray : Upgrade[] = new Upgrade[this.m_upgradesCount.intValue];
	
	for(var i = 0; i < this.m_upgradesCount.intValue; i++){
		upgradesArray *= m_Object.FindProperty(String.Format(upgradesData, i)).objectReferenceValue as Upgrade;*
  •   }*
    
  •   return upgradesArray;*
    
  • }*

  • function SetUpgrade(index : int, upgrade : Upgrade){*

  •   m_Object.FindProperty(String.Format(upgradesData, index)).objectReferenceValue = upgrade as UnityEngine.Object;*
    
  • }*

  • function AddUpgrade(){*

  •   this.m_upgradesCount.intValue++;*
    
  •   var upgrade : Upgrade = Upgrade.CreateInstance(typeof(Upgrade));*
    
  •   upgrade.name = "Upgrade" + this.m_upgradesCount.intValue;*
    
  •   this.SetUpgrade(this.m_upgradesCount.intValue -1, upgrade);*
    
  • }*

  • function GetUpgradeAt(index : int){*

  •   return m_Object.FindProperty(String.Format(upgradesData, index)).objectReferenceValue as Upgrade;*
    
  • }*

  • function RemoveUpgradeAt(index : int){*

  •    for (var i : int = index; i < this.m_upgradesCount.intValue - 1; i++)*
    
  • SetUpgrade(i, GetUpgradeAt(i+1));*

  • this.m_upgradesCount.intValue–;*

  • }*

  • function OnEnable(){*

  •   m_Object = new SerializedObject(target);*
    
  • }*

  • //TODO: check that this function gets executed only in debug mode. Don’t want this going on in production*

  • function OnInspectorGUI () {*

  •   m_Object.Update();		*
    
  •   GUILayout.Label("Upgrades", EditorStyles.boldLabel);*
    
  •   var upgrades : Upgrade[] = GetUpgradesArray();*
    
  •   for(var i = 0; i < upgrades.Length; i++){*
    
  •   	Debug.Log(upgrades[0]);*
    

_ var upgrade : Upgrade = upgrades*;_
_
GUILayout.BeginHorizontal();_
_
EditorGUILayout.LabelField(“Price”);_
_
upgrade.price = EditorGUILayout.IntField(upgrade.price);_
_
EditorGUILayout.LabelField(“Object”);_
_
upgrade.upgradObject = EditorGUILayout.ObjectField(upgrade.upgradObject, GameObject);*_

* if(GUILayout.Button(“Remove”, GUILayout.Width(70)))*
* RemoveUpgradeAt(i);*

* GUILayout.EndHorizontal();*

* EditorGUILayout.Space();*

* if(GUI.changed)*
* SetUpgrade(i, upgrade); *
* }*

* if(GUILayout.Button(“Add Upgrade”)){*
* AddUpgrade();*
* }*

* m_Object.ApplyModifiedProperties();
_
}_
_
}_
_
`*_
Looks like that “AddUpgrade” function is not saving data so the GetUpgradesArray function can not fetch it. Again I repeat this is working fine if that prefab is instantiated in the scene view. Any ideas how to save data in prefab objects?

Because you’re working with a prefab, it means it’s stored on the disk. Any changes you make should also be stored to the disk.

You most likely need to check if the current target is a prefab stored on the disk or an instantiated object with EditorUtility.IsPersistent; if it’s a prefab then call EditorUtility.SetDirty so Unity knows the object has changed. Then either let the user save manually or do AssetDatabase.SaveAssets in OnDisable… or somewhere.