按钮变换事件

我有一个难题。我正在尝试做类似的事情:我有 3 个按钮,我点击一个,它增加,我点击另一个,它增加,前一个返回到原始大小。


    private void PigeonholesHasClicked(GameObject target, MouseEventType type)

{

    if (type == MouseEventType.CLICK)

    {

        TargetIndex = System.Array.IndexOf(Pigeonholes, target);

        PigeonholesTrasnform();

    }

}


Vector3 mPosition;

int lastIndex = 0;

public float mIndex;

private void PigeonholesTrasnform()

{

    mPosition = new Vector3(Pigeonholes[TargetIndex].transform.position.x - mIndex, Pigeonholes[TargetIndex].transform.position.y, 0f);

    Pigeonholes[TargetIndex].transform.DOMove(mPosition, 0.5f);

    Pigeonholes[TargetIndex].GetComponent<BoxCollider2D>().enabled = false;

    if (TargetIndex != lastIndex)

    {

        mPosition = new Vector3(Pigeonholes[lastIndex].transform.position.x + mIndex, Pigeonholes[lastIndex].transform.position.y, 0f);

        Pigeonholes[lastIndex].transform.DOMove(mPosition, 0.5f);

        for (int i = 0; i < Pigeonholes.Length; i++)

        {

            if (i != TargetIndex)

                Pigeonholes[i].GetComponent<BoxCollider2D>().enabled = true;

        }

    }

    lastIndex = TargetIndex;

}

问题是,如果我点击 1 或 2 按钮,0 按钮会向右移动 0.4。如果我先开始输入 0,然后按 1 或 2,那么一切正常。想法是 lastIndex 为 0最初,如果我把它设为-1,它会给我一个错误。帮助!!!


慕标5832272
浏览 130回答 1
1回答

撒科打诨

当您开始时,您还没有选择任何内容=>您实际上不希望它0最初是。我lastIndex = -1;最初会离开,但只是忽略这些线条mPosition = new Vector3(Pigeonholes[lastIndex].transform.position.x + mIndex, Pigeonholes[lastIndex].transform.position.y, 0f);Pigeonholes[lastIndex].transform.DOMove(mPosition, 0.5f);如果它仍然是-1-> 表示之前没有选择任何内容:if(lastIndex >= 0){&nbsp; &nbsp; mPosition = new Vector3(Pigeonholes[lastIndex].transform.position.x + mIndex, Pigeonholes[lastIndex].transform.position.y, 0f);&nbsp; &nbsp; Pigeonholes[lastIndex].transform.DOMove(mPosition, 0.5f);}
打开App,查看更多内容
随时随地看视频慕课网APP