我正在Unity中设置一个UI按钮,以将信息添加到Google Spreadsheet。但是当我在运行时单击它时,出现“ TypeLoadException”。我不知道如何解决。
在Unity(C#)中,我尝试制作一个UI按钮,该按钮将使用Google Sheets API v4与Google Spreadsheet进行通信。但是,出现以下错误:
TypeLoadException: Could not find method due to a type load error
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:66)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:108)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()
在我看来,这是Unity问题,而不是我的个人代码,但我不确定。代码如下,以防万一:
public void AddToDatabase() {
string json;
string discipline = GameObject.Find("DisciplineButton").transform.GetChild(0).GetComponent<Text>().text;
string title = GameObject.Find("TitleButton").transform.GetChild(0).GetComponent<Text>().text;
string training = "[\"" + string.Join("\", \"", GameObject.Find("EventSystem").GetComponent<DisciplineList>().chosenTraining.ToArray()) + "\"]";
string start = GameObject.Find("StartDateButton").transform.GetChild(0).GetComponent<Text>().text;
string end = GameObject.Find("EndDateButton").transform.GetChild(0).GetComponent<Text>().text;
string availability = GameObject.Find("TimeText").GetComponent<Text>().text;
if (availability == "Part-Time") {
string days = DPW.GetComponent<Text>().text;
string hours = HPD.GetComponent<Text>().text;
json = "[{\"discipline\":\"" + discipline + "\",\"title\":\"" + title + "\",\"training\":\"" + training + "\",\"startDate\":\"" + start + "\",\"endDate\":\"" + end + "\",\"availability\":\"" + availability + "\",\"DPW\":\"" + days + "\",\"HPD\":\"" + hours + "\"}]";
} else {
json = "[{\"discipline\":\"" + discipline + "\",\"title\":\"" + title + "\",\"training\":\"" + training + "\",\"startDate\":\"" + start + "\",\"endDate\":\"" + end + "\",\"availability\":\"" + availability + "\"}]";
}
object data = "";
JsonUtility.FromJsonOverwrite(json, data);
SheetsService sheetsService = new SheetsService(new BaseClientService.Initializer {
HttpClientInitializer = GetCredential(),
ApplicationName = "Google-SheetsSample/0.1",
});
// The ID of the spreadsheet to update.
string spreadsheetId = "value"; // TODO: Update placeholder value.
// The A1 notation of a range to search for a logical table of data.
// Values will be appended after the last row of the table.
string range = "A2:K"; // TODO: Update placeholder value.
// How the input data should be interpreted.
SpreadsheetsResource.ValuesResource.AppendRequest.ValueInputOptionEnum valueInputOption = (SpreadsheetsResource.ValuesResource.AppendRequest.ValueInputOptionEnum)2; // TODO: Update placeholder value.
// How the input data should be inserted.
SpreadsheetsResource.ValuesResource.AppendRequest.InsertDataOptionEnum insertDataOption = (SpreadsheetsResource.ValuesResource.AppendRequest.InsertDataOptionEnum)1; // TODO: Update placeholder value.
// TODO: Assign values to desired properties of `requestBody`:
Data.ValueRange requestBody = new Data.ValueRange {
MajorDimension = "COLUMNS",
Range = "A2:K",
Values = (IList<IList<object>>)data
};
SpreadsheetsResource.ValuesResource.AppendRequest request = sheetsService.Spreadsheets.Values.Append(requestBody, spreadsheetId, range);
request.ValueInputOption = valueInputOption;
request.InsertDataOption = insertDataOption;
// To execute asynchronously in an async method, replace `request.Execute()` as shown:
Data.AppendValuesResponse response = request.Execute();
// Data.AppendValuesResponse response = await request.ExecuteAsync();
// TODO: Change code below to process the `response` object:
Console.WriteLine(JsonConvert.SerializeObject(response));
}
我尝试重新安装API,并从Google Sheet API页面上提供的说明重新开始,但是最后都没有解决我的问题。