问题描述
限时送ChatGPT账号..我在 c# 控制台应用程序中尝试了这个并且它起作用了,但是一旦我将它推入统一程序中,它就没有工作,请帮忙
i tried this in a c# console aplication and it worked but as soon as i shoved it in a unity program it diddent work plz help
using System.Collections.Generic;
using UnityEngine;
using RestSharp;
public class corona : MonoBehaviour
{
RestSharp.RestClient client = new RestClient("https://www.worldometers.info/coronavirus/");
RestSharp.RestRequest request = new RestRequest(Method.GET);
string content = client.Execute(request).Content;
string[] words = content.Split(' ');
string line = words[832];
string[] lineCut = line.Split('>');
string Scases = LineLineCut[0] + LineLineCut[1];'''
推荐答案
您展示的代码在 C# 控制台应用程序中也不起作用,因为您有引用其他字段的字段初始值设定项(例如 content
字段初始值设定项使用 request
).
The code you've shown would not have worked in a C# console application either, because you have field initializers referring to other fields (e.g. the content
field initializer using request
).
您可能希望将所有代码放在一个方法中:
You probably want to put all of that code in a method instead:
public class Corona : MonoBehavior
{
public void SomeMethod()
{
RestSharp.RestClient client = new RestClient("https://www.worldometers.info/coronavirus/");
RestSharp.RestRequest request = new RestRequest(Method.GET);
string content = client.Execute(request).Content;
string[] words = content.Split(' ');
string line = words[832];
string[] lineCut = line.Split('>');
// Note: you haven't shown a declaration for LineLineCut.
// You may have just meant lineCt
string Scases = LineLineCut[0] + LineLineCut[1];
// ...
}
}
我强烈怀疑您的工作控制台应用程序具有与此类似的代码.
I strongly suspect that your working console application had code similar to this.
您可能还需要将 RestSharp
代码更改为更加以 Unity 为中心的 HTTP 堆栈 - 我不知道 RestSharp
在 Unity 中是否可用.我还建议尝试找到一个不那么脆弱的数据源 - 访问 words[832]
感觉就像是在等待出错.
You may need to change the RestSharp
code as well to a more Unity-centric HTTP stack - I don't know whether RestSharp
is available in Unity. I'd also recommend trying to find a data source which isn't as brittle - accessing words[832]
feels like it's just waiting to go wrong.
然后,您还需要确定如何在正确的时间以某种 Unity 特定的方式调用该方法.但首先要解决的是您的字段初始值设定项编译器错误.
You'll then need to work out how to call the method at the right time, in some Unity-specific way, as well. But the first thing to get past is your field initializer compiler error.
这篇关于CS0236 字段初始值设定项无法引用非静态字段、方法或属性“corona.line"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论