如何解决统一发布空引用异常
我正在尝试使用 pubnub 使我的排行榜统一运行。我试图跟随并改变 this tutorial 但是当我统一运行它时,没有任何显示:
在我的代码中,唯一与教程不同的是发布密钥和订阅密钥,我在其中设置了自己的密钥。
这是存储和播放设置的屏幕截图: 这是 pubnub 功能设置的屏幕截图,上面写着“当模块运行时它会自动设置为开启”:
这是我的代码,当我尝试设置用户名和分数时,控制台没有打印任何内容(请参阅下面的评论):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PubNubAPI;
using UnityEngine.UI;
using SimpleJSON;
public class MyClass
{
public string username;
public string score;
public string refresh;
}
public class leaderboard : MonoBehavIoUr
{
public static PubNub pubnub;
public Text Line1;
public Text Line2;
public Text Line3;
public Text Line4;
public Text Line5;
public Text score1;
public Text score2;
public Text score3;
public Text score4;
public Text score5;
public Button SubmitButton;
public InputField FieldUsername;
public InputField Fieldscore;
//public static leaderboard instance;
//public Object[] tiles = {}
// Use this for initialization
void Start()
{
Button btn = SubmitButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
// Use this for initialization
PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.PublishKey = "pub-c-450b-redacted";
pnConfiguration.SubscribeKey = "sub-c-03aa-redacted";
pnConfiguration.LogVerbosity = PNLogVerbosity.BODY;
pnConfiguration.UUID = Random.Range(0f,999999f).ToString();
pubnub = new PubNub(pnConfiguration);
Debug.Log(pnConfiguration.UUID);
MyClass fireRefreshObject = new MyClass();
fireRefreshObject.refresh = "new user refresh";
string firerefreshobject = JsonUtility.ToJson(fireRefreshObject);
pubnub.Fire() // This will trigger the leaderboard to refresh so it will display for a new user.
.Channel("submit_score")
.Message(firerefreshobject)
.Async((result,status) =>
{
if (status.Error)
{
Debug.Log(status.Error);
Debug.Log(status.ErrorData.Info);
}
else
{
Debug.Log(string.Format("Fire Timetoken: {0}",result.Timetoken));
}
});
pubnub.SubscribeCallback += (sender,e) =>
{
SubscribeEventEventArgs mea = e as SubscribeEventEventArgs;
if (mea.Status != null)
{
}
if (mea.MessageResult != null)
{
Dictionary<string,object> msg = mea.MessageResult.Payload as Dictionary<string,object>;
string[] strArr = msg["username"] as string[];
string[] strscores = msg["score"] as string[];
int usernamevar = 1;
//setting usernames,nothing is printed to the console
foreach (string username in strArr)
{
string usernameobject = "Line" + usernamevar;
GameObject.Find(usernameobject).GetComponent<Text>().text = usernamevar.ToString() + ". " + username.ToString();
usernamevar++;
Debug.Log(username);
}
//setting scores,nothing is printed to the console
int scorevar = 1;
foreach (string score in strscores)
{
string scoreobject = "score" + scorevar;
GameObject.Find(scoreobject).GetComponent<Text>().text = "score: " + score.ToString();
scorevar++;
Debug.Log(score);
}
}
if (mea.PresenceEventResult != null)
{
Debug.Log("In Example,SusbcribeCallback in presence" + mea.PresenceEventResult.Channel + mea.PresenceEventResult.Occupancy + mea.PresenceEventResult.Event);
}
};
pubnub.Subscribe()
.Channels(new List<string>() {
"leaderboard_scores"
})
.WithPresence()
.Execute();
//TaskOnClick();
}
public void TaskOnClick()
{
var usernametext = FieldUsername.text;// this would be set somewhere else in the code
var scoretext = Fieldscore.text;
MyClass myObject = new MyClass();
myObject.username = FieldUsername.text;
myObject.score = Fieldscore.text;
string json = JsonUtility.ToJson(myObject);
pubnub.Publish()
.Channel("submit_score")
.Message(json)
.Async((result,status) =>
{
if (!status.Error)
{
Debug.Log(string.Format("Publish Timetoken: {0}",result.Timetoken));
}
else
{
Debug.Log(status.Error);
Debug.Log(status.ErrorData.Info);
}
});
//Output this to console when the Button is clicked
Debug.Log("You have clicked the button!");
}
}
如果您还有其他需要了解的信息,请告诉我。 对不起我的英语不好。提前致谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。