如何从另一个脚本编辑活动场景的游戏对象?
How to edit Active scene's GameObject from another script?
我正在制作一个 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 ();
}
}
}
更新未体现在场景中。
如何更新到活动场景?
我找到方法了,我需要搜索GameObject的child。
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;
}
}
}
我看到你找到了解决方案,太棒了。如果您能在 Whosebug 中分享解决方案,那就太棒了,这样任何遇到类似问题的人都可以使用您的解决方案来帮助他们。作为奖励,如果您愿意,可以对您的代码进行一些重构,使其更易于管理和阅读。
// 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();
}
}
}
我正在制作一个 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 ();
}
}
}
更新未体现在场景中。 如何更新到活动场景?
我找到方法了,我需要搜索GameObject的child。
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;
}
}
}
我看到你找到了解决方案,太棒了。如果您能在 Whosebug 中分享解决方案,那就太棒了,这样任何遇到类似问题的人都可以使用您的解决方案来帮助他们。作为奖励,如果您愿意,可以对您的代码进行一些重构,使其更易于管理和阅读。
// 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();
}
}
}