繁体   English   中英

将 static 成员访问到另一个 class 不起作用?

[英]Accessing static member to another class not working?

我遇到了一个问题,当用户选择特定的 object 时,我试图访问 static 成员以使该消息显示在我的数据库中。 顺便说一句,我的前端我正在统一使用它。 那么,谁能帮我解决这个问题。 谢谢您的帮助。

这是我的代码:

     public  void NextButton()
    {
        if (highlightSet == true)
        {

            var httpWebRequest =
                (HttpWebRequest) WebRequest.Create("https://PROJECT_URL.firebaseio.com/brokenComp.json");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "PUT";


            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {


          string missingObjectCount = TextDisplay.Message; 
                streamWriter.Write(missingObjectCount);
            }

            var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();

            }


    }

文本显示:

    public class TextDisplay: MonoBehaviour
{

   public static string Message;

 }

更新的代码

     public void StartUpload()
    {
        StartCoroutine(Upload());
    }

    public void NextButton()
    {
        if (highlightSet == true)
        {
            IEnumerator Upload()
            {
                byte[] myData = System.Text.Encoding.UTF8.GetBytes(TextDisplay.Message);
                using (UnityWebRequest www = UnityWebRequest.Put("https://PROJECT_URL.firebaseio.com/brokenComp.json", myData))
                {
                    yield return www.Send();

                    if (www.isNetworkError || www.isHttpError)
                    {
                        Debug.Log(www.error);
                    }
                    else
                    {
                        Debug.Log("Upload complete!");
                    }
                }
            } 

如果您从 web 请求中收到 404 错误,那是因为 Firebase 无法找到您请求的资源。 确保您的 URL ( https://PROJECT_URL.firebaseio.com/brokenComp.json ) 是正确的。

首先HTTP 错误 404表示未找到提供的 URL。 这与static字段无关。


由于我们看不到/如果您为TextDisplay.Message分配值,它可能始终是null (您还使用断点确认了这一点。)我至少会使用空string对其进行初始化以避免异常。

public static string Message;

既然你要求它:

正如评论中所说,我宁愿使用UnityWebRequest.Put在请求完成之前不阻塞整个线程。

这只是 API 中的示例(稍作修改),但我想它非常简单,您应该可以将它用于您的目的

public class MyBehavior : MonoBehaviour 
{
    public void NextButton()
    {
        StartCoroutine(Upload());
    }

    IEnumerator Upload()
    {
        byte[] myData = System.Text.Encoding.UTF8.GetBytes(TextDisplay.Message);
        using (UnityWebRequest www = UnityWebRequest.Put(YOUR_URL, myData))
        {
            yield return www.Send();

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Upload complete!");
            }
        }
    }
}

顺便说一句,有什么阻碍你使用Firebase SDK吗?

会有类似的东西

public class MyScript: MonoBehaviour {


  void Start() {
    // Set up the Editor before calling into the realtime database.
    FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://YOUR-FIREBASE-APP.firebaseio.com/");

    // Get the root reference location of the database.
    reference = FirebaseDatabase.DefaultInstance.RootReference;
  }

  public void NextButton()
  {
      // ported the variable names from your latest question
      mDatabaseRef.Child("messages").Child("message").SetValueAsync(TextDisplay.Message);
  }
}

暂无
暂无

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

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