Search Unity

Call button in inspector window from the script

Discussion in 'Scripting' started by Corchado, May 11, 2021.

  1. Corchado

    Corchado

    Joined:
    May 31, 2018
    Posts:
    9
    Hi, i want simulate click this button in inspector window from other scrpit, its possible?


    upload_2021-5-11_9-54-14.png
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Call the same function that button calls?
     
  3. Corchado

    Corchado

    Joined:
    May 31, 2018
    Posts:
    9
    the function hace this code:

    namespace UnityStandardAssets.Utility.Inspector
    {
    #if UNITY_EDITOR
    [CustomPropertyDrawer(typeof (WaypointCircuit.WaypointList))]
    public class WaypointListDrawer : PropertyDrawer
    {
    private float lineHeight = 18;
    private float spacing = 4;


    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {

    EditorGUI.BeginProperty(position, label, property);

    float x = position.x;
    float y = position.y;
    float inspectorWidth = position.width;

    // Draw label

    // Don't make child fields be indented
    var indent = EditorGUI.indentLevel;
    EditorGUI.indentLevel = 0;

    var items = property.FindPropertyRelative("items");
    var titles = new string[] {"Transform", "", "", ""};
    var props = new string[] {"transform", "^", "v", "-"};
    var widths = new float[] {.7f, .1f, .1f, .1f};
    float lineHeight = 18;
    bool changedLength = false;

    if (items.arraySize > 0)
    {
    for (int i = 0; i < items.arraySize; ++i)
    {
    var item = items.GetArrayElementAtIndex(i);
    float rowX = x;
    for (int n = 0; n < props.Length; ++n)
    {
    float w = widths[n]*inspectorWidth;

    // Calculate rects
    Rect rect = new Rect(rowX, y, w, lineHeight);
    rowX += w;

    if (i == -1)
    {
    EditorGUI.LabelField(rect, titles[n]);
    }
    else
    {
    if (n == 0)
    {
    EditorGUI.ObjectField(rect, item.objectReferenceValue, typeof (Transform), true);
    }
    else
    {
    if (GUI.Button(rect, props[n]))
    {
    switch (props[n])
    {
    case "-":
    items.DeleteArrayElementAtIndex(i);
    items.DeleteArrayElementAtIndex(i);
    changedLength = true;
    break;
    case "v":
    if (i > 0)
    {
    items.MoveArrayElement(i, i + 1);
    }
    break;
    case "^":
    if (i < items.arraySize - 1)
    {
    items.MoveArrayElement(i, i - 1);
    }
    break;
    }
    }
    }
    }
    }//fin del for de props

    y += lineHeight + spacing;
    if (changedLength)
    {
    break;
    }
    }//fin del for de items
    }
    else
    {
    // add button
    var addButtonRect = new Rect((x + position.width) - widths[widths.Length - 1]*inspectorWidth, y,
    widths[widths.Length - 1]*inspectorWidth, lineHeight);
    if (GUI.Button(addButtonRect, "+"))
    {
    items.InsertArrayElementAtIndex(items.arraySize);
    }

    y += lineHeight + spacing;
    }

    // add all button
    var addAllButtonRect = new Rect(x, y, inspectorWidth, lineHeight);


    if (GUI.Button(addAllButtonRect, "Assign using all child objects"))
    {
    var circuit = property.FindPropertyRelative("circuit").objectReferenceValue as WaypointCircuit;

    Debug.Log("joder "+circuit);

    var children = new Transform[circuit.transform.childCount];
    int n = 0;
    foreach (Transform child in circuit.transform)
    {
    children[n++] = child;
    }
    Array.Sort(children, new TransformNameComparer());
    circuit.waypointList.items = new Transform[children.Length];
    for (n = 0; n < children.Length; ++n)
    {
    circuit.waypointList.items[n] = children[n];
    }
    }
    y += lineHeight + spacing;

    // rename all button
    var renameButtonRect = new Rect(x, y, inspectorWidth, lineHeight);
    if (GUI.Button(renameButtonRect, "Auto Rename numerically from this order"))
    {
    var circuit = property.FindPropertyRelative("circuit").objectReferenceValue as WaypointCircuit;
    int n = 0;
    foreach (Transform child in circuit.waypointList.items)
    {
    child.name = "Waypoint " + (n++).ToString("000");
    }
    }
    y += lineHeight + spacing;

    // Set indent back to what it was
    EditorGUI.indentLevel = indent;
    EditorGUI.EndProperty();
    }


    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
    SerializedProperty items = property.FindPropertyRelative("items");
    float lineAndSpace = lineHeight + spacing;
    return 40 + (items.arraySize*lineAndSpace) + lineAndSpace;
    }


    // comparer for check distances in ray cast hits
    public class TransformNameComparer : IComparer
    {
    public int Compare(object x, object y)
    {
    return ((Transform) x).name.CompareTo(((Transform) y).name);
    }
    }
    }
    #endif
    }