简体   繁体   English

在InitializeOnLoad上更改GameObject组件

[英]Change GameObject Component on InitializeOnLoad

I created script that i want to use as main script when my game launch. 我在游戏发布时创建了我想用作主脚本的脚本。 Now i want to access specific button text via that script and change it's text. 现在我想通过该脚本访问特定的按钮文本并更改它的文本。 I have problem because my text is not changing. 我有问题,因为我的文字没有改变。 Here is what i was trying: 这是我在尝试的:

static main()
{
    Debug.Log("Up and running");
    GameObject devButtonText = GameObject.Find("devButtonText");
    Text text = devButtonText.GetComponent<Text>();
    text.text = "Test";
}

Button is devButton and text is devButtonText 按钮是devButton ,文本是devButtonText

full script 完整的脚本

using UnityEngine;
using UnityEditor;
using System.Collections;
using UnityEngine.UI;


[InitializeOnLoad]
public class main : MonoBehaviour {


    static main()
    {
        Debug.Log("Up and running");
        GameObject devButton = GameObject.Find("devButton");
        Text text = devButton.GetComponentInChildren<Text>();
        text.text = "Test";
    }

    // Use this for initialization
    void Start () { 

    }

    // Update is called once per frame
    void Update () {

    }
}

I created script that i want to use as main script when my game launch. 我在游戏发布时创建了我想用作主脚本的脚本。

The InitializeOnLoad attribute must be used to run a function when the Unity Editor starts , not the game. 必须使用InitializeOnLoad属性在Unity Editor启动时运行函数,而不是游戏。 Every editor script won't run when you compile your game. 编译游戏时,每个编辑器脚本都不会运行。 Unity documentation Unity文档

Sometimes, it is useful to be able to run some editor script code in a project as soon as Unity launches without requiring action from the user. 有时,只要Unity启动而无需用户操作,就可以在项目中运行一些编辑器脚本代码。


Instead, create an Empty GameObject and attach the following script : 而是创建一个Empty GameObject并附加以下脚本:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;


public class ChangeText : MonoBehaviour
{
    private void Awake()
    {
        GameObject devButton = GameObject.Find("devButton");
        Text text = devButton.GetComponentInChildren<Text>();
        text.text = "Test";
    }
}

Even better : 更好的是:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;


public class ChangeText : MonoBehaviour
{
    // Drag & Drop the desired Text component here
    public Text TextToChange ;

    // Write the new content of the Text component
    public string NewText ;

    private void Awake()
    {
        TextToChange.text = NewText;
    }
}

The script will automatically be called when the scene starts. 场景启动时将自动调用脚本。

When Initializing a MonoBehaviour 初始化MonoBehaviour时

  1. You should use Start, Awake or OnEnable. 您应该使用Start,Awake或OnEnable。

  2. You shouldn't use constructor, static constructor or field initialization (like public GameObject go = GameObject.Find("devButton"); ) 你不应该使用构造函数,静态构造函数或字段初始化(如public GameObject go = GameObject.Find("devButton");

This should work: 这应该工作:

using UnityEngine;
using UnityEditor;
using System.Collections;
using UnityEngine.UI;


public class main : MonoBehaviour {


    void Awake () { 
        Debug.Log("Up and running");
        GameObject devButton = GameObject.Find("devButton");
        Text text = devButton.GetComponentInChildren<Text>();
        text.text = "Test";
    }
}

Edit 编辑

Given the name main I guess this is the starting point of your project so if your script is not attached to any game object then Start, Awake or OnEnable would not be called. 鉴于名称main我猜这是你项目的起点,所以如果你的脚本没有附加到任何游戏对象,那么将不会调用Start,Awake或OnEnable。 In this case you should attach it to a game object and change the unity script execution order and bring the script to the earliest point. 在这种情况下,您应该将它附加到游戏对象并更改统一脚本执行顺序并将脚本带到最早的点。

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

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