Unity:在JSON上加载图片/精灵 [英] Unity: Load Image/Sprite on JSON
问题描述
我想在json上加载精灵,但是如何?显示文字,但不显示图像.
I want to load sprite on json but how ? Text are showing but not the images.
这是加载我的文本数据的JSON代码.
This is the JSON code to load my text data.
private void myLoadGameData() //LOAD THE DATA
{
string myfilePath = Path.Combine(Application.streamingAssetsPath, mygameDataFileName); //I THINK THIS IS THE PATH OF THE FILE
if (File.Exists(myfilePath))
{
string mydataAsJson = File.ReadAllText(myfilePath); // READ THE FILE
TSGameData myloadedData = JsonUtility.FromJson<TSGameData>(mydataAsJson); // TSGAME DATA IS A ANOTHER SCRIPT THAT HAVE AN ARRAY FOR THE DATA
myRoundData = myloadedData.myRoundData;
} //myRoundData IS A VARIABLE THAT HOLDS THE ARRAY OF TSROUNDDATA TO GET THE DATA
else
{
Debug.LogError("Cannot load game data!");
}
}
这是我的JSON脚本.我不确定这是否是加载图像的有效脚本.
Here is my JSON script. I am not really sure if this is a valid script to load image.
"questionImage":"Assets/ImagesQuiz/NoentryPlate.png"
推荐答案
假设questionImage
是myRoundData
变量中的一个属性,则需要获取字符串并将其用作资产路径,Unity具有一个简单的名称通过路径加载资产的功能
Assuming questionImage
is a property in the myRoundData
variable, you would need to grab the string and use it as an asset path, Unity has a simple function to load assets by path
Resources.Load<T>(string path)
此函数将返回加载的资产引用作为提供的泛型类型.但是,Resources.Load
方法期望所有资产都将存储在Assets
文件夹中的Resources
文件夹中.您可以在多个其他文件夹中嵌套多个Resources
文件夹.例如,您可能拥有以下文件
This function will return a loaded asset reference as the provided generic type. However, the Resources.Load
method expects that all assets will be stored in a Resources
folder in the Assets
folder. You can have multiple Resources
folders nested within various other folders. For example, you could have the following files
- 资产/资源/Test1.png
- 资产/资源/UI/纹理/Test2.png
- 资产/图像/资源/Test3.png
假设其纹理类型设置为Sprite
,则可以使用
Assuming their texture type is set to Sprite
, each one could be accessed with
Resources.Load<Sprite>("Test1");
Resources.Load<Sprite>("UI/Textures/Test2");
Resources.Load<Sprite>("Test3");
一些注意事项
- 如果您的文件具有相同的名称但在不同的资源中
文件夹,它将返回找到的第一个资源. - 您不需要提供文件扩展名.
- 您可以将文件嵌套在文件夹中,只要它之一
父文件夹是
Resources
文件夹. (请参阅 上面的示例)
- If you have files with the same name but in different resources
folders, it will return the first resource found. - You do not need to provide the file extension.
- You can nest files within folders, as long as long as one of its
parent folders is a
Resources
folder. (SeeTest2.png
in the example above)
使用这种方法,您首先需要将存储questionImage
属性值的方式更改为类似的方式
With this approach, you first need to change the way you store your questionImage
property value to something like this
{ "questionImage":"NoentryPlate" }
然后,使用您提供的代码,我们可以添加其他方法来处理此资源加载和Sprite设置部分.我们需要一个带有SpriteRenderer
组件的GameObject的引用(您可以创建一个空的gameObject并自己添加该组件).加载精灵后,可以将SpriteRenderer
组件的sprite
属性设置为刚加载的精灵.
Then, with the code you provided, we could add an additional method to handle this resource loading and sprite setting part. We would need a reference to a GameObject with a SpriteRenderer
component (you could create an empty gameObject and add the component yourself). Once you load the sprite, you can set the sprite
property of the SpriteRenderer
component to the sprite that you just loaded.
public SpriteRenderer MySprite;
private Sprite LoadedSprite = null;
private void myLoadGameData() //LOAD THE DATA
{
string myfilePath = Path.Combine(Application.streamingAssetsPath, mygameDataFileName); //I THINK THIS IS THE PATH OF THE FILE
if (File.Exists(myfilePath))
{
string mydataAsJson = File.ReadAllText(myfilePath); // READ THE FILE
TSGameData myloadedData = JsonUtility.FromJson<TSGameData>(mydataAsJson); // TSGAME DATA IS A ANOTHER SCRIPT THAT HAVE AN ARRAY FOR THE DATA
myRoundData = myloadedData.myRoundData;
// vvv CALL OUR NEW METHOD HERE vvv
LoadSprite(myRoundData.questionImage);
} //myRoundData IS A VARIABLE THAT HOLDS THE ARRAY OF TSROUNDDATA TO GET THE DATA
else
{
Debug.LogError("Cannot load game data!");
}
}
private void LoadSprite (string path)
{
if (LoadedSprite != null)
Resources.UnloadAsset(LoadedSprite);
LoadedSprite = Resources.Load<Sprite>(path);
MySprite.sprite = LoadedSprite;
}
这篇关于Unity:在JSON上加载图片/精灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!