【问题标题】:How to edit Active scene's GameObject from another script?如何从另一个脚本编辑活动场景的游戏对象?
【发布时间】:2017-12-10 09:36:39
【问题描述】:

我正在制作一个 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 ();
                    }
                }

            }

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

【问题讨论】:

  • 我没有完全关注你...如果脚本没有运行,什么都不会发生。你能解释一下你这样做的目的吗?此外,我将进行一些重构,同时有助于提高代码的可读性。
  • 谢谢。我找到了方法。我需要搜索游戏对象的孩子。

标签: c# unity3d unity3d-2dtools


【解决方案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();
        }
    }

}

【讨论】:

    【解决方案2】:

    我找到了方法,我需要搜索 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;
                        } 
    
                    }
    
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多