循环后只重命名了最后一个游戏对象?

时间:2015-05-14 02:57:36

标签: c# unity3d gameobject

我将所有游戏对象保存在.txt文件中。当我尝试使用文本文件加载我的地​​图时,除了gameobject.name之外,一切都按预期工作。所有对象都放置在正确的位置并带有好标签。这是代码的一部分。

void OnMouseDown(){

    if (GlobalManager.MapName != "") {
        string path = @"c:\Users\Ashes\Desktop\Rebel2\" + GlobalManager.MapName + ".txt";
        if (loadedMap == GlobalManager.MapName) {
            txtMessage2 = "This map is already open";
            StartCoroutine (Message1 ());
        }
        if (!File.Exists (path)) {
            txtMessage2 = "This map name not exist, place object to create the map";
            StartCoroutine (Message1 ());

        }
        if (File.Exists (path)) {
            if (loadedMap != GlobalManager.MapName) {
                {
                    loadedMap = GlobalManager.MapName;
                    GameObject[] Tree = GameObject.FindGameObjectsWithTag ("Tree");

                    foreach (GameObject item in Tree) {
                        Destroy (item);
                    }

                    GameObject[] House = GameObject.FindGameObjectsWithTag ("House");

                    foreach (GameObject item in House) {
                        Destroy (item);
                    }

                    GameObject[] Road = GameObject.FindGameObjectsWithTag ("Road");

                    foreach (GameObject item in Road) {
                        Destroy (item);
                    }

                    GameObject[] Farm = GameObject.FindGameObjectsWithTag ("Farm");

                    foreach (GameObject item in Farm) {
                        Destroy (item);
                    }

                    GameObject[] Wall = GameObject.FindGameObjectsWithTag ("Wall");

                    foreach (GameObject item in Wall) {
                        Destroy (item);
                    }

                }

                using (StreamReader sr = File.OpenText(path)) {
                    string s = "";
                    while ((s = sr.ReadLine()) != null) {
                        overLoadtxt = s;
                        string[] splitArray = overLoadtxt.Split (' ');
                        loadStr1 = splitArray [0]; 
                        loadStr2 = splitArray [1]; 
                        loadStr3 = splitArray [2]; 
                        loadStr4 = splitArray [3]; 
                        loadStr5 = splitArray [4]; 
                        loadStr6 = splitArray [5]; 



                        if (loadStr2 == "House") {
                            Instantiate (House);
                            if (loadStr6 == "LuxHouse") {
                                House.GetComponent<SpriteRenderer> ().sprite = img1;
                            }
                            if (loadStr6 == "House1") {
                                House.GetComponent<SpriteRenderer> ().sprite = img2;
                            }
                            if (loadStr6 == "Greentex") {
                                House.GetComponent<SpriteRenderer> ().sprite = img3;
                            }

                            House.tag = "House";
                            House.transform.localScale = new Vector3 (0.5f, 0.5f, 0);
                            House.transform.eulerAngles = new Vector3 (0, 0, float.Parse (loadStr5));
                            House.transform.position = new Vector3 (float.Parse (loadStr3), float.Parse (loadStr4), 0);
                            GlobalManager.intHouseName ++;
                            House.name = loadStr1;
                            Debug.Log (House.name);
                        }

                        if (loadStr2 == "Road") {
                            Instantiate (Road);
                            if (loadStr6 == "dirthroad") {
                                Road.GetComponent<SpriteRenderer> ().sprite = img4;
                            }
                            Road.tag = "Road";
                            Road.transform.localScale = new Vector3 (0.5f, 0.3f, 0);
                            Road.transform.eulerAngles = new Vector3 (0, 0, float.Parse (loadStr5));
                            Road.transform.position = new Vector3 (float.Parse (loadStr3), float.Parse (loadStr4), 0);
                            GlobalManager.intRoadName ++;
                            Road.name = loadStr1;
                            Debug.Log (Road.name);
                        }
                    }
                }
            }
        }
    }
}

debug.log显示正确的东西,house1,house2,road1,house3 ......但只有最后一个gameobject.name才会应用更改。

我对这个机制毫无头绪。

0 个答案:

没有答案