简体   繁体   English

具有不同返回类型的相同方法

[英]Same methods with different return types

I have two methods almost same in body but different in return type.我有两种方法在正文中几乎相同,但返回类型不同。 One is IEnumerator and another is void .一个是 IEnumerator ,另一个是 void 。 Is there any Smart way of writing code so that I have to write less lines of code.有没有什么聪明的编写代码的方法,这样我就必须编写更少的代码行。

IEnumerator CreatePath(int val)
{
        if (val < worlds.Length - 1) {
                int totalChild = CountChildren (Path [val].transform);
                MeshRenderer[] child = Path [val].transform.GetComponentsInChildren<MeshRenderer> ();

                for (int i = 0; i < totalChild; i++) {
                        yield return new WaitForSeconds (0.2f);

                        child [i].enabled = true;   
                }
        }

}

And

void CreatePath_(int val)
{
        if (val < worlds.Length - 1) {
                int totalChild = CountChildren (Path [val].transform);
                MeshRenderer[] child = Path [val].transform.GetComponentsInChildren<MeshRenderer> ();

                for (int i = 0; i < totalChild; i++) {
                        child [i].enabled = true;   
                }
        }
}

And the whole script以及整个剧本

public GameObject[] worlds,Path;
public  int ClearedWorldValue = -1;
private int posArray;

void Start()
{
    SetMapState ();
}

void SetMapState()
{
        for (int i = 0; i < 3; i++) {
                ChangeColor (i,0);

        }

}

void ChangeColor(int val,float pause){
        if (val!=-1) {
            worlds [val].transform.GetChild (0).GetComponent<SpriteRenderer>().enabled = false;
            worlds [val].transform.GetChild (1).GetComponent<SpriteRenderer>().enabled = true;  
                StartCoroutine (CreatePath (val, pause));
                ChangeColortoRed (val);
        }

}

void ChangeColortoRed(int val)
{
        if (val < worlds.Length-1) {
                worlds [val + 1].transform.GetChild (0).GetComponent<SpriteRenderer> ().enabled = true;
        }
}

IEnumerator CreatePath(int val,float pause)
{
        if (val < worlds.Length - 1) {
                int totalChild = CountChildren (Path [val].transform);
                MeshRenderer[] child = Path [val].transform.GetComponentsInChildren<MeshRenderer> ();

                for (int i = 0; i < totalChild; i++) {
                        yield return new WaitForSeconds (pause);

                        child [i].enabled = true;   
                }
        }

}



int CountChildren(Transform a)
{
        int childCount = 0;
        foreach (Transform b in a)
        {
            childCount ++;
            childCount += CountChildren (b);
        }
        return childCount;
}

void Update()
{
    ChangeColor (ClearedWorldValue,0.2f);
}

you can either make it generic or return object.您可以将其设为通用或返回对象。 The best is to extract the repeating part in a separate method.最好是用单独的方法提取重复部分。

Methods have quite different behavior, because IEnumerator one has WaitForSeconds inside, so you can add a new parameter pause to the method with default value, which will be equal to pause duration.方法有完全不同的行为,因为IEnumerator one 里面有WaitForSeconds ,所以你可以给方法添加一个新的参数pause ,默认值等于 pauseduration。

IEnumerator CreatePath(int val, float pause = 0)
{
        if (val < worlds.Length - 1) {
                int totalChild = CountChildren (Path [val].transform);
                MeshRenderer[] child = Path [val].transform.GetComponentsInChildren<MeshRenderer> ();

                for (int i = 0; i < totalChild; i++) {
                        yield return new WaitForSeconds (pause);

                        child [i].enabled = true;   
                }
        }

}

Then you can use CreatePath (1) method without pause(or with default pause) or CreatePath(1, 100) with specified pause duration.然后您可以使用没有暂停(或默认暂停)或具有指定暂停持续时间的CreatePath(1, 100) CreatePath (1)方法。

But I suggest to split it to three methods, one base method and two that will call the base method, with different params, so your method name will represent a behavior of a method.但我建议将其拆分为三个方法,一个基本方法和两个调用基本方法的方法,使用不同的参数,因此您的方法名称将代表方法的行为。

IEnumerator CreatePathWithPause(int val, float pause)
{
        if (val < worlds.Length - 1) {
                int totalChild = CountChildren (Path [val].transform);
                MeshRenderer[] child = Path [val].transform.GetComponentsInChildren<MeshRenderer> ();

                for (int i = 0; i < totalChild; i++) {
                        yield return new WaitForSeconds (pause);

                        child [i].enabled = true;   
                }
        }

}

IEnumerator CreatePath(int val) 
{
    return CreatePathWithPause(val, 0)
}

IEnumerator CreatePathWithDefaultPause(int val) 
{
    return CreatePathWithPause(val, 0.2f)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM