【问题标题】:How to call coroutine from another script? UNITY如何从另一个脚本调用协程?统一
【发布时间】:2020-03-28 16:33:00
【问题描述】:

这是触摸动作的脚本,主游戏就在上面

  var addingGoldPerSec = new GameObject();
    var buttonInstance = addingGoldPerSec.AddComponent<ItemButton>();
    StartCoroutine(buttonInstance.addGoldLoop());

这是我有协程的地方

public double goldPerSec
{
    get
    {
        if (!PlayerPrefs.HasKey("_goldPerSec"))
        {
            return 0;
        }
        string tmpStartGoldPerSec = PlayerPrefs.GetString("_goldPerSec");
        return double.Parse(tmpStartGoldPerSec);
    }
    set
    {
        PlayerPrefs.SetString("_goldPerSec", value.ToString());
    }
}

public void updateUI()
{
        priceDisplayer.text = LargeNumber.ToString(currentCostPerSec).ToString();
        coinDisplayer.text = "PER SEC\n" + LargeNumber.ToString(goldPerSec).ToString();
        levelDisplayer.text = "LEVEL\n" + level + "/150".ToString();
}
public IEnumerator addGoldLoop()
{
    while (DataController.Instance.gold <= 1e36)
    {
        DataController.Instance.gold += goldPerSec;
        if (Gameplay.Instance.booster == 1)
        {
            yield return new WaitForSeconds(0.25f);
        }
        else if (Gameplay.Instance.booster == 0)
        {
            yield return new WaitForSeconds(1.0f);
        }
    }
}

这是我管理存储在 PlayerPrefs 中的数据的第三个脚本

 public void loadItemButton(ItemButton itemButton)
{
    itemButton.level = PlayerPrefs.GetInt("_level",1);
    PlayerPrefs.GetString("_costPerSec", itemButton.currentCostPerSec.ToString());
    PlayerPrefs.GetString("_goldPerSec",itemButton.goldPerSec.ToString());
}
public void saveItemButton(ItemButton itemButton)
{
    PlayerPrefs.SetInt("_level", itemButton.level);
    PlayerPrefs.SetString("_costPerSec", itemButton.currentCostPerSec.ToString());
    PlayerPrefs.SetString("_goldPerSec", itemButton.goldPerSec.ToString());
}

我有第二个脚本,它是一个附加到几个游戏对象的协程,其中存在一个升级按钮,每次升级都会增加你赚取的秒数,这里的主要问题是协程在我触摸屏幕后就停止了,所以我在主脚本中编写了另一个代码,这样协程即使在触摸屏幕后也能继续工作,所以我编写了一个脚本,游戏对象在哪里,但它只是抛出 NullReferenceException,尝试使用 TryCatch 进行检查并抛出问题来自我在主脚本上创建的游戏对象,但如果这样我需要附加到主脚本存在的主对象,比如存在协同程序的 10 多个游戏对象,我认为单例不是方式,它通过在 Awake 上实例化删除了我上面的所有信息,我从没想过制作静态,所以我按照你告诉我的做了,我需要更改几乎我的代码,每个文本都附加到每个游戏对象中,以制作一个非静态成员与静态成员一起工作需要删除 Monobehaviour 但这只会让游戏爆炸,感谢您的帮助。

【问题讨论】:

  • 脚本链接到游戏对象?

标签: c# unity3d


【解决方案1】:

创建两个脚本并附加它们,例如,在主摄像头处。第一个脚本包含带有所有变量的计时器:

使用 UnityEngine; 使用 System.Collections;

public class TimerToCall : MonoBehaviour {
 //Create your variable
 ...
 //Your function timer
 public IEnumerator Counter() {
  ...
 }
}

在第二个脚本中,您将调用计时器:

public class callingTimer : MonoBehaviour {
 void Start() {
  //TimerToCall script linked to Main Camera, so
  StartCoroutine(Camera.main.GetComponent<TimerToCall>().Counter());
 }
}

例如,如果您想让第二个脚本不链接到任何 gameObjet,您可以使用静态属性:

在第一个链接的脚本中:

void Start()
{
    StartCoroutine(CallingTimer.ExampleCoroutine());   
} 

在未链接的第二个脚本中,您使用静态属性:

using System.Collections;
using UnityEngine;

public class CallingTimer
{
    public static IEnumerator ExampleCoroutine()
    {
        //Print the time of when the function is first called.
        Debug.Log("Started Coroutine at timestamp : " + Time.time);

        //yield on a new YieldInstruction that waits for 5 seconds.
        yield return new WaitForSeconds(5);

        //After we have waited 5 seconds print the time again.
        Debug.Log("Finished Coroutine at timestamp : " + Time.time);
    }
}

【讨论】:

  • 您好,我将脚本用作 Singleton 并使用 12 个升级按钮,无法将其附加到单个相机,因为我需要将其附加到 12 个升级按钮上。
  • 如果您不想链接脚本,我已经添加了第二个解决方案..
  • 感谢您的帮助,但我遇到了 Monobehaviour 和静态成员的问题,您能帮帮我吗?现在刚刚编辑。
  • 很难理解你想要什么......你的解释让我感到困惑......你能重新表述你的问题并完整显示脚本:调用协程和协程的调用者..因为你混合大量对象...我需要展示您如何定义所有变量
  • 好的,Frenchy 已经对其进行了更新,感谢您帮助我,通过不同的方式尝试了很多天,但没有任何想法,因为我是 Unity 的初学者,我将不胜感激在您的帮助下,感谢您抽出宝贵时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 2022-08-02
  • 2012-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-22
相关资源
最近更新 更多