Unity简单代码

时间:2018-05-06 16:50:18

标签: c# unity3d google-cloud-save

我正在关注Unity Cloud Save教程,并且我遇到了以下代码。

#region Saved Games
//making a string out of game data (highscores...)
string GameDataToString()
{
    return Mascota._monedas.ToString();
}

我不知道如何使用" return"添加更多变量。在这个结构中。我还不太清楚使用return。

2 个答案:

答案 0 :(得分:1)

If you have several variables, you can concatenate them.

An example:

EVENT_SYSTEM_FOREGROUND

Which would yield following output:

SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, NULL, &window_change_hook, 0, 0, WINEVENT_OUTOFCONTEXT); ... void CALLBACK window_change_hook(HWINEVENTHOOK, DWORD, HWND hwnd, LONG, LONG, DWORD, DWORD) { CString str; GetWindowText(GetAncestor(hwnd, GA_ROOT), str.GetBuffer(100), 100); str.ReleaseBuffer(); ::SetDlgItemText(h_this_app_wnd, IDC_EDIT1, str); }

There are many ways to concat strings:

int a = 123; int b = 9999; string c = "data"; string GameDataToString() { return a.ToString() + "; " + b.ToString() + "; " + c; }

123; 9999; data

"hello" + "; " + "world"

string var1 = "hello";
string var2 = "world";
string var3 = $"{var1}; {var2}";

答案 1 :(得分:0)

使用元组(C#7中的新功能,我能够在Unity上工作)和使用out参数,有两种方法可以做到这一点。

C#7允许您使用其优美的语法return多个值:

public (string String1, string OtherString) GetStrings()
{
    return ("This is String 1", "This is String 2");
}

...

Debug.Log(GetStrings().String1);       // Logs "This is String 1"
Debug.Log(GetStrings().OtherString);   // Logs "This is String 2

设置返回类型时,您不需要指定名称。这正是您正在寻找的,但设置起来很棘手(但值得)。

然后是#34;旧学校"获得多次返回的方法是使用out参数,如下所示:

public void GetStrings(out string string1, out string otherString)
{
    string1     = "This is String 1";
    otherString = "This is String 2";
}

... 

string string1;
string otherString;

GetStrings(out string1, out otherString);

Debug.Log(string1);       // Logs "This is String 1"
Debug.Log(otherString);   // Logs "This is String 2"

out参数通常可以在Unity中完成,但C#7会添加out个变量。 请注意我们必须首先声明变量,即使我们在调用GetStrings()之后我们还没有使用它们? C#7允许我们将代码简化为:

public void GetStrings(out string string1, out string otherString)
{
    string1     = "This is String 1";
    otherString = "This is String 2";
}

... 

GetStrings(out string string1, out string otherString);  // Declaration for variables here

Debug.Log(string1);       // Logs "This is String 1"
Debug.Log(otherString);   // Logs "This is String 2"

这将会起作用,但同样需要C#7。