Unity3D-在游戏开始时初始化服务

问题描述 投票:0回答:1

在初始化下游游戏对象之前,在游戏开始时初始化服务/代码的推荐方法是什么?例如,我有以下代码尝试初始化Google Play游戏。我想确保在单独的游戏对象尝试通过Init()使用登录的用户名更新标签之前,我想确保Social.localUser.userName是完整的(我希望将显示逻辑与GameManager代码分开)。

谢谢

public class GameManager : MonoBehaviour 

    private void Start ()
    {
        PlayServicesClient.Init(() =>
        {
            // More Game Logic
        });
    }
}

// Separate script used in the menu UI
login.text = (Social.localUser.authenticated) ? Social.localUser.userName : "Login";

编辑:我曾尝试按照Eric的建议将Play游戏的初始化移至Awake(),但即使登录代码位于另一个脚本的Start()中,登录文本代码也不会等待Init方法完成。

   private void Awake()
    {
        PlayGamesClient.Init((success) => { });
    }

    // Start method of the other script
    private void Start()
    {
        loginText.text = (Social.localUser.authenticated) ? Social.localUser.userName : "Login";
    }
c# unity3d google-play-games
1个回答
0
投票

然后在Awake函数中将其初始化。

[Awake函数在场景中检测到脚本之后,在开始运行Start之前并且基本上在其他所有事物之前运行。

请参见this以获取更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.