JSON和Unity,在游戏中显示图像 [英] JSON and Unity , Showing image on game

查看:309
本文介绍了JSON和Unity,在游戏中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Quiz Game应用程序,并且游戏中有图像.我想显示图像..文本显示得很好,但是图像却没有.这是我的JSON C#代码.

I have a Quiz Game app and I have images on the game. I want to show images.. Text are showing very well but the Images are not. here is my JSON C# Code.

点击此处查看图片

调用我的对象和变量.

To call my objects and variables.

 public TSRoundData[] myRoundData;

public TSPlayerProgress myplayerProgress;

private string mygameDataFileName = "TSdatabase.json";

我的JSON代码

  private void myLoadGameData()
{
    string myfilePath = Path.Combine(Application.streamingAssetsPath, mygameDataFileName);

    if (File.Exists(myfilePath))
    {
        string mydataAsJson = File.ReadAllText(myfilePath);
        TSGameData myloadedData = JsonUtility.FromJson<TSGameData>(mydataAsJson);
        myRoundData = myloadedData.myRoundData;

    }
    else
    {
        Debug.LogError("Cannot load game data!");
    }
}

我的JSON脚本代码

      "questionImage":"Assets/ImagesQuiz/NoentryPlate.png",

推荐答案

您将图像路径保存在json中,请尝试从资源文件夹中加载它. 尝试这样:

You holding your image path in json, soo try load it from resource folder. Try like that:

    private Sprite myImage;

    private void myLoadGameData()
    {
        string myfilePath = Path.Combine(Application.streamingAssetsPath, mygameDataFileName);

        if (File.Exists(myfilePath))
        {
            string mydataAsJson = File.ReadAllText(myfilePath);
            TSGameData myloadedData = JsonUtility.FromJson<TSGameData>(mydataAsJson);
            myRoundData = myloadedData.myRoundData;
            myImage = Resources.Load<Sprite>(myloadedData.myRoundData.questionImage);
        }
        else
        {
            Debug.LogError("Cannot load game data!");
        }
    }

这篇关于JSON和Unity,在游戏中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆