Hy guys,
I have two scripts. One UI script which attached to the button (OnClick Event) in Unity editor. And Script that drawing line with line render.
I used to keep DrawBoundary() in Update now I so every time I click with the mouse button it will start drawing. I would like to add a UI button. So mouse click works only when I click UI button first. And only then I can draw a line with a mouse click.
I have created an event in UI and subscribe to this event in main script, but it doesn't work. Basically the DrawBoundary() function has to run continuously after bUI button was clicked.
I assume I have to pass bool variable to main script somehow ? So I can have to functions in UI Script. When button clicked I draw, when button click again I stop.
I am trying to this too scripts completely unaware of each other...
Thank you
Main Script:
void Start()
{
UISystem.onClick += DrawBoundary;
}
// Update is called once per frame
void Update()
{
//DrawBoundary(); Before I had this method in Update
}
//Add point on mouse click
public void DrawBoundary()
{
if (Input.GetMouseButtonDown(0))
{
//Adds position to array
_lineRenderer.positionCount++;
// Sets new position
_lineRenderer.SetPosition(_positionIndexCount, GetMouseWorldPosition());
//Changes to next position index
_positionIndexCount++;
//Instantiate point icons
Instantiate(boundaryPoint, GetMouseWorldPosition(), Quaternion.identity);
BoundaryCollider2D();
}
//Avoid loop line when second pointed is added
if (_positionIndexCount > 2)
{
_lineRenderer.loop = true;
}
}
UI Script with Event:
public delegate void ActionClick();
public static event ActionClick onClick;
public void ButtonClick()
{
if (onClick != null)
{
onClick();
}
}