C# If语句或for循环

C# If语句或for循环,c#,for-loop,if-statement,unity3d,C#,For Loop,If Statement,Unity3d,每次用户触摸屏幕时,我都会调用一个方法。一旦他们触摸屏幕足够多次,脚本所连接的对象将被停用 有问题的脚本附加到我基于动态数据循环的布局上 假设我有10个问题,这意味着我将有10个布局,并附上10个版本的脚本 我不确定我是否采取了最好的方法,但我被困住了,因为我是Unity/C的新手,我希望得到一些指导 我需要脚本根据屏幕索引检查不同的索引。目前,if语句(在下面的方法中)工作得很好,但是,因为我不知道会有很多屏幕(由于处理动态数据),我需要一种在没有大量if语句列表的情况下执行这些检查的方法 我

每次用户触摸屏幕时,我都会调用一个方法。一旦他们触摸屏幕足够多次,脚本所连接的对象将被停用

有问题的脚本附加到我基于动态数据循环的布局上

假设我有10个问题,这意味着我将有10个布局,并附上10个版本的脚本

我不确定我是否采取了最好的方法,但我被困住了,因为我是Unity/C的新手,我希望得到一些指导

我需要脚本根据屏幕索引检查不同的索引。目前,if语句(在下面的方法中)工作得很好,但是,因为我不知道会有很多屏幕(由于处理动态数据),我需要一种在没有大量if语句列表的情况下执行这些检查的方法

我知道我可以使用for循环,但我的问题是我需要根据屏幕索引更改计数(如if语句所示)。是否有可能使用循环来完成我所追求的目标,或者我是否需要改变实现这一目标的方式

我面临的一个问题是,将新脚本添加到每个实例之间唯一可用的常量是ScreenIndex count变量。因此,我在if声明中一再提到这一点

我尝试将我的方法包装为(ScreenIndex=0;ScreenIndex,但计数不同步。我必须使用嵌套循环吗?我已经试过了,但到目前为止没有成功

我的方法当前如下所示:

//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
        }
    }
}
//Scratch pad 1-由于每次刮取像素时都会调用此方法,因此需要使用嵌套语句。
公共进度1(浮动进度)
{
//设置i的计数,以确保可以检查屏幕索引
int i=0;
//在循环帮助条件以确保我们知道用户当前所在的页面时,继续循环i,直到它与用户所在的索引匹配为止。
//这是必要的,因为每次调用此方法时,我都会不断重置,因此while循环将确保我与索引匹配
而(i!=transform.parent.gameObject.transform.parent.gameObject.GetComponent().ScreenIndex)
{
i++;
}
//一旦用户刮伤了超过50%的焊盘,请将其停用
如果(数学循环(进度*100f)>=50)
{
//如果用户划伤超过50%,请停止用户进一步划伤,并检查是否正确。
transform.parent.gameObject.transform.GetChild(0).GetChild(3).GetChild(0).GetChild(0).gameObject.SetActive(false);
//循环遍历包含答案是否正确的数组
//如果屏幕索引与页码相同
if(transform.parent.gameObject.transform.parent.gameObject.GetComponent().ScreenIndex==i)
{
int ScreenIndex=transform.parent.gameObject.transform.parent.gameObject.GetComponent().ScreenIndex;
如果(屏幕索引==0)
{
Debug.Log(“X(应该是0)=”+屏幕索引);
//如果用户位于屏幕索引0,则循环应仅在此处查询位置0。如果用户位于位置1,则循环应为
//查询位置4-这是因为位置1,2,3将在“OnEraseProgressScratch2”、“OnEraseProgressScratch3”和“OnEraseProgressScratch4”中查询。
如果(UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString()包含(“正确”))
{
Debug.Log(“答案1正确”);
}
}
else if(屏幕索引==1)
{
屏幕索引+=3;
Debug.Log(“X(应该是4)=”+屏幕索引);
如果(UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString()包含(“正确”))
{
Debug.Log(“答案1正确”);
}
}
否则如果(屏幕索引==2)
{
屏幕索引+=6;
Log(“X(应该是8)=”+屏幕索引);
如果(UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString()包含(“正确”))
{
Debug.Log(“答案1正确”);
}
}
否则如果(屏幕索引==3)
{
屏幕索引+=9;
Log(“X(应该是12)=”+屏幕索引);
如果(UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString()包含(“正确”))
{
Debug.Log(“答案1正确”);
}
}
否则如果(屏幕索引==4)
{
屏幕索引+=12;
Debug.Log(“X(应该是16)=”+屏幕索引);
如果(UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString()包含(“正确”))
{
Debug.Log(“答案1正确”);
}
}
否则如果(屏幕索引==5)
{
屏幕索引+=15;
Debug.Log(“X(应该是20)=”+屏幕索引);
如果(UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString()包含(“正确”))
{
Debug.Log(“答案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;

我很难理解你想做什么。你能试着让它更简洁吗?只需要一句话:不需要所有的if/else,也不需要永远“继续”。看看你的个案,你会发现