Search Unity

Editor scripts for multiple objects but with different results

Discussion in 'Immediate Mode GUI (IMGUI)' started by pbelling78, Oct 13, 2016.

  1. pbelling78

    pbelling78

    Joined:
    Oct 12, 2016
    Posts:
    2
    I am trying to write an editor script for a simple Attach to Vert function. This script allows me to select an object and run the "Find Nearest Vert" function, which then sets its own "Nearest Vertex ID" variable to an int value. Now the current script works with objects that are not a prefab, but if it is, the function will run and assign the variable but when the scene is then played all Vertex ID's revert back whatever the prefab is set to.

    I was able to use serializedObject.FindProperty("nearestVertexID"), which would assign the value and it would stay when the scene was played, but then that set all the selected objects to the same ID (the last object selected in the inspector).

    Code (CSharp):
    1. #if UNITY_EDITOR
    2. [CustomEditor( typeof( AttachToVert ) )]
    3. [CanEditMultipleObjects]
    4. public class ObjectBuilderEditor : Editor
    5. {
    6.    private AttachToVert _script = null;
    7.    SerializedProperty vertexIndex;
    8.  
    9.    void OnEnable()
    10.    {
    11.       vertexIndex = serializedObject.FindProperty("nearestVertexID");
    12.    }
    13.  
    14.    public override void OnInspectorGUI()
    15.    {
    16.       serializedObject.Update ();
    17.  
    18.       DrawDefaultInspector();
    19.       _script = (AttachToVert)target;
    20.       if (_script.manager == null || _script.manager.meshRenderer == null)
    21.       {
    22.          EditorGUILayout.HelpBox( "Attach To Vert Manager is not assigned or configured", MessageType.Info );
    23.          return;
    24.       }
    25.  
    26.  
    27.       if (GUILayout.Button( "Find Nearest Vertex" ))
    28.       {
    29.          int numTargets = serializedObject.targetObjects.Length;
    30.          for (int i = 0; i < numTargets; ++i)
    31.          {
    32.             FindNearestVertex( (AttachToVert)serializedObject.targetObjects[i] );
    33.             serializedObject.ApplyModifiedProperties();
    34.          }
    35.       }
    36.    }
    37.  
    38.    private void FindNearestVertex( AttachToVert target )
    39.    {
    40.       Undo.RegisterCompleteObjectUndo( target, "Find Nearest Vertex" );
    41.       Mesh mesh = target.manager.meshRenderer.sharedMesh;
    42.       float minDistanceSqr = Mathf.Infinity;
    43.       Vector3 position = target.transform.position;
    44.  
    45.       //Scan all vertices to find nearest
    46.       int mc = mesh.vertexCount;
    47.       for (int i = 0; i < mc; i++)
    48.       {
    49.          Vector3 vertex = target.manager.meshRenderer.transform.TransformPoint( mesh.vertices[i] );
    50.          Vector3 diff = position - vertex;
    51.          float distSqr = diff.sqrMagnitude;
    52.          if (distSqr < minDistanceSqr)
    53.          {
    54.             minDistanceSqr = distSqr;
    55.             target.nearestVertexID = i;
    56.             vertexIndex.intValue = i;
    57.          }
    58.       }
    59.    }
    Without the vertexIndex.intValue = i line the code works as mentioned, as in it assigns the variables to the correct value but they all revert to the prefab when the scene is played. With that line, they are all set to the same value of the last item selected;

    Any help is appreciated, thanks!