- Home /
Editor Targets are updated but SerializedPropertys arent
Hey everyone!
Im building an application where I need to have Multiple Objects Edition while having a custom interface, so Im creating some Editor classes. Right now Im trying to update each selected object with a foreach target and then use it somewhere else with a SerializedProperty var, but this is just not working how I thought it should be.
_thrustersProp is defined as follows:
SerializedProperty _thrustersProp;
And inited inside OnEnable:
_thrustersProp = serializedObject.FindProperty ("_thrusters");
This is the code to update all selected targets, found within the ThrustersEditor class:
if( InterfaceEditor.CreateField( _thrustersCountProp, "SetThrusters" ) )
{
Debug.Log ( "updating" );
foreach ( SimVrRobotics.ROV.Thrusters pm in targets )
{
Debug.Log ( "Before - pm._thrusters.Length: " + pm._thrusters.Length );
pm.updateThrusters( _thrustersCountProp.intValue );
Debug.Log ( "After - pm._thrusters.Length: " + pm._thrusters.Length );
}
serializedObject.ApplyModifiedProperties ();
serializedObject.Update ();
Debug.Log("A - _thrustersProp.size: " + _thrustersProp.arraySize);
}
When Debugging, I can see that the "After" Debug shows me the behaviour Id expect, but this doesnt happen on the "A - " Debug, which shows me size 0.
This is the updateThrusters method, found within the Thrusters class, and here both Debugs works as expected.
public void updateThrusters( int newThrustersCount )
{
Thruster[] newThrusters = new Thruster[newThrustersCount];
int previousThrusters = 0;
Debug.Log( "newThrustersCount: " + newThrustersCount + " _thrusters.Length: " + _thrusters.Length );
for( ; previousThrusters < _thrusters.Length; previousThrusters++ )
{
newThrusters[previousThrusters].SetRotation ( _thrusters[previousThrusters].GetRotation() );
newThrusters[previousThrusters].SetRelativePos ( _thrusters[previousThrusters].GetRelativePos() );
newThrusters[previousThrusters].SetMaxEffort ( _thrusters[previousThrusters].GetMaxEffort() );
newThrusters[previousThrusters].SetSetThruster ( _thrusters[previousThrusters].GetSetThruster() );
newThrusters[previousThrusters].SetFoldout ( _thrusters[previousThrusters].GetFoldout() );
}
if ( _thrusters.Length < newThrustersCount )
{
for ( ; previousThrusters < newThrustersCount; previousThrusters++ )
{
newThrusters[previousThrusters] = new Thruster();
}
}
_thrustersCount = newThrustersCount;
System.Array.Clear( _thrusters, 0, _thrusters.Length );
_thrusters = new Thruster[newThrustersCount];
newThrusters.CopyTo( _thrusters, 0 );
Debug.Log( "2newThrustersCount: " + newThrustersCount + " _thrusters.Length: " + _thrusters.Length );
}
}
This is the nested class Thruster, defined inside the Thrusters class:
[System.Serializable]
public class Thruster
{
public bool _setThruster;
public Vector3 _relativePos;
public Vector3 _rotation;
public Vector3 _direction;
public float _maxEffort;
public bool _foldout;
public Thruster ( )
{
_setThruster = false;;
_relativePos = new Vector3(0f, 0f, 0f);
_rotation = new Vector3(0f, 0f, 0f);
_direction = new Vector3(0f, 0f, 0f);
_maxEffort = 0.0f;
_foldout = false;
}
}
And finally, the following snip method, defined within the ThrustersEditor class, is how I access the nested class Thruster and where I get the error "Retrieving array element that was out of bounds", because it has an arraySize of 0 according to Debug.Log.
private void createAttributes( )
{
for( int i = 0; i < _thrustersCountProp.intValue; i++ )
{
Debug.Log ( "createAttributes - i: " + i + " _thrustersProp.size: " + _thrustersProp.arraySize);
SerializedProperty thrusterElementProp = _thrustersProp.GetArrayElementAtIndex( i );
SerializedProperty setThrusterProp = thrusterElementProp.FindPropertyRelative( "_setThruster" );
SerializedProperty relativePosProp = thrusterElementProp.FindPropertyRelative( "_relativePos" );
SerializedProperty rotationProp = thrusterElementProp.FindPropertyRelative( "_rotation" );
SerializedProperty directionProp = thrusterElementProp.FindPropertyRelative( "_direction" );
SerializedProperty maxEffortProp = thrusterElementProp.FindPropertyRelative( "_maxEffort" );
SerializedProperty foldoutProp = thrusterElementProp.FindPropertyRelative( "_foldout" );
EditorGUILayout.Space();
InterfaceEditor.CreateBoldFoldout( "Thruster", foldoutProp, " " + i, true );
EditorGUI.indentLevel++;
}
}
What am I missing here?
Thank you for reading till the end and sorry for the long post, I just wanted to give as much information needed.
Answer by thrmotta · Mar 26, 2015 at 06:37 PM
I dont understand why, but apparently I cant update an array the way I was. The way that worked for me was simply substituting the foreach where the update occured with _thrustersProp.arraySize = _thrustersCountProp.intValue;