0
\$\begingroup\$
using UnityEngine;
using System.Collections;

public class CurvedLinePoint : MonoBehaviour
{
    [HideInInspector] public bool showGizmo = true;
    [HideInInspector] public float gizmoSize = 0.1f;
    [HideInInspector] public Color gizmoColor = new Color(1, 0, 0, 0.5f);

    void OnDrawGizmos()
    {
        if (ResetLineRendererPositions.hasReseted == false)
        {
            if (showGizmo == true)
            {
                Gizmos.color = gizmoColor;

                Gizmos.DrawSphere(this.transform.position, gizmoSize);
            }
        }
    }

    //update parent line when this point moved
    void OnDrawGizmosSelected()
    {
        if (ResetLineRendererPositions.hasReseted == false)
        {
            CurvedLineRenderer curvedLine = this.transform.parent.GetComponent<CurvedLineRenderer>();

            if (curvedLine != null)
            {
                curvedLine.Update();
            }
        }
    }
}

Then I created two scripts one is editor type with two buttons. One to reset the linerenderer positions and one to generate the positions :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ResetLineRendererPositions : MonoBehaviour
{
    public LineRenderer lineRenderer;
    public static bool hasReseted = false;

    public void ResetLinerendererPositions()
    {
        lineRenderer.positionCount = 0;
        hasReseted = true;
    }

    public void GenerateLineRendererPositions()
    {
        hasReseted = false;
    }
}

And

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(ResetLineRendererPositions))]
public class ResetLineRendererPositionsButton : Editor
{
    public override void OnInspectorGUI()
    {
        ResetLineRendererPositions myScript = (ResetLineRendererPositions)target;
        if (GUILayout.Button("Reset Linerenderer Positions"))
        {
            myScript.ResetLinerendererPositions();
        }

        GUILayout.Space(30);

        if (GUILayout.Button("Generate Linerenderer Positions"))
        {
            myScript.GenerateLineRendererPositions();
        }
    }
}

The buttons are working the problem is when I click on the button "Generate Linerenderer Positions" then I have to select the linerenderer object in the hierarchy to make it take affect and draw the gizmos.

I want that when I click the "Generate Linerenderer Positions" button it will automatic run the code in the OnDrawGizmos and OnDrawGizmosSelected functions.

\$\endgroup\$
1
  • \$\begingroup\$ Tried to add [ExecuteInEditMode] in the top of the CurvedLinePoint script but it didn't change anything. \$\endgroup\$
    – Daniel Lip
    Commented Feb 12, 2021 at 21:05

1 Answer 1

2
\$\begingroup\$

The buttons are working the problem is when I click on the button "Generate Linerenderer Positions" then I have to select the linerenderer object in the hierarchy to make it take affect and draw the gizmos.

OnDrawGizmosSelected() is only called by the Editor when you select that object or one of its parents. Although you could have your button call OnDrawGizmosSelected(), that's not what OnDrawGizmosSelected() is for.

Also, OnDrawGizmos()/OnDrawGizmosSelected() are only intended for drawing Gizmos and other Unity Editor markup. You should not be calling other code, such as curvedLine.Update(), from within either of these functions.

It seems like you should have the GUI button call curvedLine.Update(), rather than involving OnDrawGizmosSelected().

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .