Unity方法只运行第一行代码
Unity method only runs first line of code
我已经在我的项目中完全运行 AdMob 测试广告。
当观看广告时,下面的代码应该 运行,但是只有第一行被执行,其他 2 行被忽略,没有错误。
public void HandleUserEarnedReward(object sender, Reward args)
{
GameControl.control.life += 1;
GameControl.control.Save();
txtLife.text = GameControl.control.life.ToString();
}
我已将相同的代码放在另一个方法中,我用按钮触发该方法进行测试。
在这种情况下,完整的方法是 运行.
public void runtest()
{
GameControl.control.life += 1;
GameControl.control.Save();
txtLife.text = GameControl.control.life.ToString();
}
如果我没看错的话,你是想在用户观看奖励视频后给予奖励。我有一次遇到同样的问题,问题是在“HandleUserEarnedReward”内部执行的函数不会在 unity 的 MainThread 上执行,而是在 Google 的 SDK 线程上执行。
有几种解决方法:
https://github.com/PimDeWitte/UnityMainThreadDispatcher - 使用这个切换到主线程。查看自述文件以获取更多信息。
创建具有假值的全局布尔值。然后在“HandleUserEarnedReward”上将“isRewarded”布尔值更改为 true。创建更新函数来检查布尔值。类似于:
void Update()
{
if (isRewarded)
{
// do all the actions
// reward the player
isRewarded = false; // to make sure this action will happen only once.
}
}
使用协程。协程在“yield return”
后自动神奇地切换到 Unity 的 MainThread
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
Debug.Log(
"HandleRewardBasedVideoRewarded event received for "
+ amount.ToString() + " " + type);
StartCoroutine(AfunctionName());
}
IEnumerator AfunctionName()
{
yield return new WaitForSecondsRealtime(0.1f);
// FB.LogAppEvent("AdmobRewardedView");
Debug.Log("Reward Function Called!!!!!");
GiveReward();
this.RequestRewardBasedVideo();
}
我已经在我的项目中完全运行 AdMob 测试广告。 当观看广告时,下面的代码应该 运行,但是只有第一行被执行,其他 2 行被忽略,没有错误。
public void HandleUserEarnedReward(object sender, Reward args)
{
GameControl.control.life += 1;
GameControl.control.Save();
txtLife.text = GameControl.control.life.ToString();
}
我已将相同的代码放在另一个方法中,我用按钮触发该方法进行测试。 在这种情况下,完整的方法是 运行.
public void runtest()
{
GameControl.control.life += 1;
GameControl.control.Save();
txtLife.text = GameControl.control.life.ToString();
}
如果我没看错的话,你是想在用户观看奖励视频后给予奖励。我有一次遇到同样的问题,问题是在“HandleUserEarnedReward”内部执行的函数不会在 unity 的 MainThread 上执行,而是在 Google 的 SDK 线程上执行。 有几种解决方法:
https://github.com/PimDeWitte/UnityMainThreadDispatcher - 使用这个切换到主线程。查看自述文件以获取更多信息。
创建具有假值的全局布尔值。然后在“HandleUserEarnedReward”上将“isRewarded”布尔值更改为 true。创建更新函数来检查布尔值。类似于:
void Update() { if (isRewarded) { // do all the actions // reward the player isRewarded = false; // to make sure this action will happen only once. } }
使用协程。协程在“yield return”
后自动神奇地切换到 Unity 的 MainThreadpublic void HandleRewardBasedVideoRewarded(object sender, Reward args) { string type = args.Type; double amount = args.Amount; Debug.Log( "HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " + type); StartCoroutine(AfunctionName()); } IEnumerator AfunctionName() { yield return new WaitForSecondsRealtime(0.1f); // FB.LogAppEvent("AdmobRewardedView"); Debug.Log("Reward Function Called!!!!!"); GiveReward(); this.RequestRewardBasedVideo(); }