如何从另一个脚本编辑活动场景的GameObject?

时间:2017-12-10 09:36:39

标签: unity3d unityscript unity3d-2dtools

我正在制作一个Unity项目,我需要从另一个不同的脚本(未链接到该活动场景)编辑当前活动场景的某些UI的文本。

我的工作是

            Scene scene = SceneManager.GetActiveScene();
            Debug.Log (scene.name);
            if (scene.name == "RangeView") 
            {
                List<GameObject> activeObjects = new List<GameObject>();
                scene.GetRootGameObjects( activeObjects );
                for (int i = 0; i < activeObjects.Count; ++i)
                {
                    GameObject gameObject = activeObjects[ i ];
                    if (gameObject.name == "Clubdigit") {
                        gameObject.GetComponent<Text> ().text = 10.ToString ();
                    } 
                    else if (gameObject.name == "Balldigit") {
                        gameObject.GetComponent<Text>().text = 10.ToString ();
                    } else if (gameObject.name == "Distancedigit") {
                        gameObject.GetComponent<Text> ().text = 10.ToString ();
                    } else if (gameObject.name == "Ballspeeddigit") {
                        gameObject.GetComponent<Text> ().text = 10.ToString ();
                    } else if (gameObject.name == "Distancedigit2") {
                        gameObject.GetComponent<Text> ().text = 10.ToString ();
                    } else if (gameObject.name == "Backspindigit") {
                        gameObject.GetComponent<Text> ().text = 10.ToString ();
                    } else if (gameObject.name == "Sidespindigit") {
                        gameObject.GetComponent<Text> ().text = 10.ToString ();
                    }else if (gameObject.name == "Launchangleindigit") {
                        gameObject.GetComponent<Text> ().text = 10.ToString ();
                    }
                }

            }

更新未反映在场景中。 如何更新到活动场景?

2 个答案:

答案 0 :(得分:1)

我看到你找到了解决方案,很棒。如果您要在Stackoverflow中共享解决方案,那将是非常棒的,因此具有类似问题的其他任何人都可以使用您的解决方案来帮助他们。作为一种享受,如果您愿意,可以对代码进行一些重构,以便于管理和阅读。

// Put the dependency Using System.Linq; at the top

List<String> objectsToChange = new List<String>() 
{
  "Balldigit",
  "Distancedigit",
  "Ballspeeddigit",
  "Distancedigit2",
  "Backspindigit",
  "Sidespindigit",
  "Launchangleindigit"
}

Scene scene = SceneManager.GetActiveScene();
Debug.Log (scene.name);

if (scene.name == "RangeView") 
{
    List<GameObject> activeObjects = new List<GameObject>();
    scene.GetRootGameObjects( activeObjects );
    foreach (GameObject activeObject in activeObjects)
    {
        if (objectsToChange.Contains(activeObject.name)) 
        {
          activeObject.GetComponent<Text>().text = 10.ToString();
        }
    }

}

答案 1 :(得分:0)

我找到了方法,我需要搜索GameObject的孩子。

                List<GameObject> activeObjects = new List<GameObject>();
                scene.GetRootGameObjects( activeObjects );
                for (int i = 0; i < activeObjects.Count; ++i)
                {
                    GameObject gameObject = activeObjects[ i ];
                    Debug.Log (gameObject.name);
                    if (gameObject.name == "Canvas") {
                        foreach (Transform firstchild in gameObject.transform) {
                            Debug.Log ("firstchild name " + firstchild.name);
                            if (firstchild.name == "Swinginfo") {
                                foreach (Transform secondchild in firstchild.transform) {
                                    if (secondchild.name == "Clubdigit") {
                                        secondchild.GetComponent<Text> ().text = 10.ToString ();//result[0].ToString();
                                    } 
                                    else if (secondchild.name == "Balldigit") {
                                        secondchild.GetComponent<Text>().text = 10.ToString ();//result[0].ToString();
                                    } else if (secondchild.name == "Distancedigit") {
                                        secondchild.GetComponent<Text> ().text = 10.ToString ();//0.ToString ();
                                    } 
                                }

                            }else if(firstchild.name == "Swinginfo2"){
                                foreach (Transform secondchild in firstchild.transform) {
                                    if (secondchild.name == "Ballspeeddigit") {
                                        secondchild.GetComponent<Text> ().text = 10.ToString ();//result[0].ToString ();
                                    } else if (secondchild.name == "Distancedigit2") {
                                        secondchild.GetComponent<Text> ().text = 10.ToString ();//0.ToString ();
                                    } else if (secondchild.name == "Backspindigit") {
                                        secondchild.GetComponent<Text> ().text = 10.ToString ();//12.ToString ();
                                    } else if (secondchild.name == "Sidespindigit") {
                                        secondchild.GetComponent<Text> ().text = 10.ToString ();//13.ToString ();
                                    }else if (secondchild.name == "Launchangleindigit") {
                                        secondchild.GetComponent<Text> ().text = 10.ToString ();//result[3].ToString ();
                                    }
                                }

                            }
                        }
                        break;
                    } 

                }

编辑: 根据建议,我的最新更新是

            if (scene.name == "RangeView") 
            {
                List<String> objectsToChange = new List<String> () {
                    "Balldigit",
                    "Distancedigit",
                    "Ballspeeddigit",
                    "Distancedigit2",
                    "Backspindigit",
                    "Sidespindigit",
                    "Launchangleindigit"
                };
                List<GameObject> activeObjects = new List<GameObject>();
                scene.GetRootGameObjects( activeObjects );
                for (int i = 0; i < activeObjects.Count; ++i)
                {
                    GameObject gameObject = activeObjects[ i ];
                    Debug.Log (gameObject.name);
                    if (gameObject.name == "Canvas") 
                    {
                        foreach (Transform firstchild in gameObject.transform) {
                            Debug.Log ("firstchild name " + firstchild.name);
                            if (firstchild.name == "Swinginfo") {
                                foreach (Transform secondchild in firstchild.transform) {
                                    Debug.Log ("secondchild name " + secondchild.name);
                                    if (objectsToChange.Contains(secondchild.name)) 
                                    {
                                        secondchild.GetComponent<Text>().text = 10.ToString();
                                    }
                                }

                            }else if(firstchild.name == "Swinginfo2"){
                                foreach (Transform secondchild in firstchild.transform) {
                                    Debug.Log ("secondchild name " + secondchild.name);
                                    if (objectsToChange.Contains(secondchild.name)) 
                                    {
                                        secondchild.GetComponent<Text>().text = 10.ToString();
                                    }
                                }

                            }
                        }
                        break;
                    } 

                }

            }