Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按钮点击事件后,Unity连续运行功能_C#_Unity3d_Events - Fatal编程技术网

C# 按钮点击事件后,Unity连续运行功能

C# 按钮点击事件后,Unity连续运行功能,c#,unity3d,events,C#,Unity3d,Events,嗨,伙计们 我有两个脚本。一个UI脚本,附加到Unity editor中的按钮OnClick事件。并使用线渲染为绘制线编写脚本 我过去经常更新DrawBoundary,现在我每次用鼠标点击它都会开始绘图。 我想添加一个UI按钮。所以,只有在我首先单击UI按钮时,鼠标单击才有效。只有这样我才能用鼠标点击画一条线 我在UI中创建了一个事件,并在主脚本中订阅了该事件,但它不起作用。基本上,单击bUI按钮后,DrawBoundary函数必须连续运行 我假设我必须以某种方式将bool变量传递给主脚本?所以

嗨,伙计们

我有两个脚本。一个UI脚本,附加到Unity editor中的按钮OnClick事件。并使用线渲染为绘制线编写脚本

我过去经常更新DrawBoundary,现在我每次用鼠标点击它都会开始绘图。 我想添加一个UI按钮。所以,只有在我首先单击UI按钮时,鼠标单击才有效。只有这样我才能用鼠标点击画一条线

我在UI中创建了一个事件,并在主脚本中订阅了该事件,但它不起作用。基本上,单击bUI按钮后,DrawBoundary函数必须连续运行

我假设我必须以某种方式将bool变量传递给主脚本?所以我可以在UI脚本中创建函数。当按钮点击我画,当按钮再次点击我停止

我也在尝试这样做,完全不知道对方

多谢各位

主脚本:

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脚本:

 public delegate void ActionClick();
 public static event ActionClick onClick;

    public void ButtonClick()
    {
        if (onClick != null) 
        {
            onClick(); 
        }
    }

是的,你需要一本书。您将在更新函数中检查布尔值,并在其为真时绘制。您可以使用publicstatic在ui脚本中添加一个,也可以从主脚本访问它。然后,当您按下按钮时,您将更改布尔值

比如:

总的来说:

void Update()
{
    if(UI.isdraw)
        DrawBoundary();
}
void Update()
{
    if(UI.isdraw)
        DrawBoundary();
}