【问题标题】:Unity Text Event triggers in Editor but not in BuildUnity 文本事件在编辑器中触发,但在构建中不触发
【发布时间】:2018-10-30 05:16:58
【问题描述】:

我在使用 Unity 开发的 C# 游戏时遇到问题。虽然在编辑器中触发了带有动画的文本通知,但在我构建和运行时却没有。

我检查了输出日志并得到了这个。

NullReferenceException: Object reference not set to an instance of an object
  at NarrativeLocation+<InitializePanel>c__Iterator0.MoveNext () [0x00000] 
  in <filename unknown>:0 
  at UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, 
  IntPtr returnValueAddress) [0x00000] in <filename unknown>:0 
UnityEngine.MonoBehaviour:StartCoroutine_Auto_Internal(IEnumerator)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
NarrativeLocation:Update()

NarrativeLocation.Update 的代码

void Update()
    {
        if (!popupIsPlaying && GameState.popupQueue.Count != 0)
        {
            int temp = GameState.popupQueue.Dequeue();
            StartCoroutine(InitializePanel(temp));
        }

        int num = 1;

        while(inZone)
        {
            if (this.gameObject.tag == "NarrativeEvent" + num)
            {
                if (Input.GetKeyDown(KeyCode.E))
                {
                    Destroy(GameObject.FindGameObjectWithTag("Notification"));
                    Destroy(GameObject.FindGameObjectWithTag("NarrativeEvent" + num));

                    Constants.GameState.popupQueue.Enqueue(num);
                }

                return;
            }
            num++;
        }
    }

InitializePanel 的代码

IEnumerator InitializePanel(int num)
{
    popupIsPlaying = true;

    panel = GameObject.Find("Panel").GetComponent<PanelConfig>();
    currentEvent = JSONAssembly.RunJSONFactoryForScene(1);

    StartCoroutine(IntroAnimation());

    panel.characterIsTalking = true;
    panel.Configure(currentEvent.dialogues[num - 1]);

    yield return new WaitForSeconds(6f);

    StartCoroutine(ExitAnimation());

    Debug.Log("Event " + num + " destroyed");

    popupIsPlaying = false;

}

public IEnumerator IntroAnimation()
{
    panelAnimator.SetBool("IntroAnimationIn", true);
    yield break;
}

public IEnumerator ExitAnimation()
{
    panelAnimator.SetBool("IntroAnimationIn", false);
    yield break;

}

当我运行游戏时,面板弹出,没有任何文字。退出动画似乎也没有被调用。

JSON 程序集类。

namespace JSONFactory {
class JSONAssembly {

    private static Dictionary<int, string> _resourceList = new Dictionary<int, string>
    {
        {1, "/Resources/GameScript.json"}
    };

    public static NarrativeEvent RunJSONFactoryForScene(int sceneNumber)
    {
        string resourcePath = PathForScene(sceneNumber);

        if (isValidJSON(resourcePath) == true)
        {
            string jsonString = File.ReadAllText(Application.dataPath + resourcePath);
            NarrativeEvent narrativeEvent = JsonMapper.ToObject<NarrativeEvent>(jsonString);

            return narrativeEvent;
        }
        else
        {
            throw new Exception("JSON is not valid");
        }
    }

    private static string PathForScene(int sceneNumber)
    {
        string resourcePathResult;

        if (_resourceList.TryGetValue(sceneNumber, out resourcePathResult))
        {
            return _resourceList[sceneNumber];
        }
        else
        {
            throw new Exception("Scene not in resource list");
        }
    }

    private static bool isValidJSON(string path)
    {
        return (Path.GetExtension(path) == ".json") ? true : false;
    }
}

}

【问题讨论】:

  • 您必须发布一些代码供我们帮助,NarrativeLocation:Update 将是开始的地方。
  • @B.M.添加了 NarrativeLocation:Upate 的代码。
  • InitializePanel 长什么样子?
  • @B.M.希望这会有所帮助。
  • 这些不是这里的问题,但IntroAnimationExitAnimation 有点奇怪,为什么它们首先是协程?您也没有在InitializePanel 中等待它们,因此您正在安排它们独立运行。例如,我会将StartCoroutine(IntroAnimation()); 替换为panelAnimator.SetBool("IntroAnimationIn", true);,退出也一样。

标签: c# unity3d


【解决方案1】:

在编辑器中触发带有动画的文本通知时, 但不是在我构建和运行时。

我可以发现您的代码无法在构建中运行的几个可能原因。我可能会错过更多内容,但请在下方查看:

1。您尝试从哪里加载 json 文件:

{1, "/Resources/GameScript.json"}

一个。从 Resources 文件夹中读取时,路径中不包含“Resources”。该路径是相对于资源文件夹的。

B。路径中不要包含 .txt、.jpeg、.mp3 等文件扩展名。

要解决这两个问题,替换

{1, "/Resources/GameScript.json"}

{1, "GameScript"}

2。您当前阅读文件的方式:

string jsonString = File.ReadAllText(Application.dataPath + resourcePath);

您当前正在使用File.ReadAllText 读取文件。这将在编辑器中起作用,但在构建中不起作用,因为这不是读取 Resources 文件夹中文件的方式。

Resources 文件夹中的文件使用Resources API 读取。

要解决这个问题,替换

string jsonString = File.ReadAllText(Application.dataPath + resourcePath);

TextAsset txtAsset = Resources.Load<TextAsset>(resourcePath);
string jsonString = txtAsset.text;

确保将 json 文件放在项目中名为“Resources”的文件夹中,并且必须拼写正确。


稍后可能会遇到其他问题:

无限循环:

while(inZone) 代码无法退出,如果您遇到这种情况,您可能会导致程序冻结,因为在该循环中没有代码可以使 inZone 为 false。您必须找到一种方法来重新编写该代码。

【讨论】:

    【解决方案2】:

    在构建中,您不能使用资源路径在运行时使用 File.ReadAllText 加载文件。 Resources/ 目录中的文件在构建过程中被打包成专有的打包资产格式,并且无法通过这种方式检索。

    如果你想以这种方式加载文件,你必须使用 Resources api。

    但是,放置在Application.streamingAssetsPath(又名/Assets/StreamingAssets)中的文件可以在运行时以这种方式读取。该文件夹中的文件按原样保留。

    可以访问它们
    System.IO.Path.Combine(
      Application.streamingAssetsPath, "/Path/To/file.json");
    
    // assuming path is:
    //   /Assets/StreamingAssets/Path/To/file.json
    

    资源可能更接近你想要的。

    或者,Unity 将 json 文件(或任何其他文本文件)导入为 TextAssets。您可以像任何其他类型的资产一样在检查器中引用这些 TextAsset 文件。

    public class MyMono : MonoBehaviour
    {  
        public TextAsset json;
    
        void Start() {
            Debug.Log(json.text);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多