If语句或for循环

时间:2018-07-13 21:37:06

标签: c# for-loop if-statement unity3d

每次用户触摸屏幕时,我都会调用一种方法。一旦他们触摸屏幕足够多的时间,该脚本所附加的对象就会被停用。

有问题的脚本已附加到我根据动态数据进行循环显示的布局中。

所以说我有10个问题,这意味着我将有10个布局以及10个脚本版本。

我不确定我采用的是最佳方法,但是我受困于此,因为我是Unity / C#的新手,所以希望获得一些指导。

我需要脚本根据屏幕索引检查其他索引。当前,if语句(在下面的方法中)可以完美地工作,但是,由于我不知道会有很多屏幕(由于处理动态数据),我需要一种无需大量if列表即可进行这些检查的方法陈述。

我知道我可以使用for循环,但是我的问题是我需要根据屏幕索引来更改计数(如我的if语句所示)。是否可以通过循环来完成我想做的事情,还是需要更改处理此问题的方式?

我面临的一个问题是,向每个实例添加新脚本之间唯一可用的常量是ScreenIndex count变量。因此,为什么要在我的if语句中重复提及这一点。

我尝试将方法包装在for (ScreenIndex = 0; ScreenIndex < UIHandler.GetdynamicQuestionArray().Count; ScreenIndex += 4)中,但计数不同步。我是否必须使用嵌套循环?到目前为止,我已经尝试过但没有成功。

我的方法当前如下所示:

//Scratch pad 1 - Due to this method being called everytime a pixel is scratched, nestled if statements need to be used.
public void OnEraseProgressScratch1(float progress)
{
    //Set a count of i, to make sure the screen index can be checked
    int i = 0;

    //While loop to help condition to make sure we know what page the user is currently on, keep looping i until it matches the index the user is on.
    //This is necessary because i will keep getting reset everytime this method is called, so the while loop will make sure i matches the index
    while (i != transform.parent.gameObject.transform.parent.gameObject.GetComponent<TeamQuizScreen>().ScreenIndex)
    {
        i++;
    }
    //Once the user has scratched more than 50% of the pad, deactivate it
    if (Mathf.Round(progress * 100f) >= 50)
    {
        //If the user has scratched more than 50% stop the user from scratching any further and check if they are correct.
        transform.parent.gameObject.transform.GetChild(0).GetChild(3).GetChild(0).GetChild(0).gameObject.SetActive(false);
        //Loop through the array that holds if an answer is correct or not

        //If the screen index is the same as the page number
        if (transform.parent.gameObject.transform.parent.gameObject.GetComponent<TeamQuizScreen>().ScreenIndex == i)
        {
            int ScreenIndex = transform.parent.gameObject.transform.parent.gameObject.GetComponent<TeamQuizScreen>().ScreenIndex;

            if (ScreenIndex == 0)
            {
                Debug.Log("X (should be 0) = " + ScreenIndex);
                //If the user is at screen index 0, then a loop should be querying position 0 only here. If the user is at position 1 then a loop should be 
                //querying position 4 - this is because position 1,2,3 will be queried in 'OnEraseProgressScratch2', 'OnEraseProgressScratch3' and 'OnEraseProgressScratch4'.
                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }

            }
            else if(ScreenIndex == 1)
            {
                ScreenIndex += 3;

                Debug.Log("X (should be 4) = " + ScreenIndex);

                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }

            }
            else if (ScreenIndex == 2)
            {
                ScreenIndex += 6;

                Debug.Log("X (should be 8) = " + ScreenIndex);
                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }
            }
            else if (ScreenIndex == 3)
            {
                ScreenIndex += 9;

                Debug.Log("X (should be 12) = " + ScreenIndex);

                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }
            }
            else if (ScreenIndex == 4)
            {
                ScreenIndex += 12;

                Debug.Log("X (should be 16) = " + ScreenIndex);
                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }

            }
            else if (ScreenIndex == 5)
            {
                ScreenIndex += 15;
                Debug.Log("X (should be 20) = " + ScreenIndex);

                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }
            }
            //This will need to continue based on the number of questions in the hierarchy
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用switch语句进行检查。

switch (ScreenIndex)
{
    case 0:
        Debug.Log ("X (should be 0) = " + ScreenIndex);
        //If the user is at screen index 0, then a loop should be querying position 0 only here. If the user is at position 1 then a loop should be 
        //querying position 4 - this is because position 1,2,3 will be queried in 'OnEraseProgressScratch2', 'OnEraseProgressScratch3' and 'OnEraseProgressScratch4'.
        if (UIHandler.GetDynamicCorrectAnswerArray () [ScreenIndex].ToString ().Contains ("Correct"))
        {
            Debug.Log ("Answer 1 is correct");
        }
    break;
    case 1:
        ScreenIndex += 3;

        Debug.Log ("X (should be 4) = " + ScreenIndex);

        if (UIHandler.GetDynamicCorrectAnswerArray () [ScreenIndex].ToString ().Contains ("Correct"))
        {
            Debug.Log ("Answer 1 is correct");
        }
    break; //... and so on

或者...您可以采用另一种方法。

public void Method (int screenIndex, int AddToIndex)
{
    Debug.LogFormat ("X (should be {0}) = {1}", ScreenIndex, ScreenIndex + AddToIndex);
    ScreenIndex += AddToIndex

    if (UIHandler.GetDynamicCorrectAnswerArray () [ScreenIndex].ToString ().Contains ("Correct"))
    {
        Debug.Log ("Answer 1 is correct");
    }
}

并为您的方法创建一个公式

int toAdd = ScreenIndex * 3;