如何解决如何使玩家可以访问资产进行修改?
我的游戏包括图像文件和json配置文件,我希望它们可以在已部署的游戏的文件夹结构中访问,以便玩家可以轻松地对其进行编辑或交换。
我已经考虑/尝试了以下方法:
- 我最初的方法是使用
Resources
文件夹和代码 例如Resources.Load<TextAsset>("Rules.json")
。当然, 此操作不起作用,因为在构建过程中会编译资源文件夹。 - 我研究了
Addressables
和AssetBundle
功能,但它们似乎并非旨在解决此问题。 - 询问后,我开始使用.NET自己的文件方法,
用于
File.ReadAllText(Application.dataPath + Rules.json)
之类的代码。似乎可以使用,但是此类文件仍不会自动部署,必须手动复制。 - 似乎
StreamingAssets
文件夹已存在,因为该手册通告其内容是在目标计算机上逐字复制的。我假设应该像上一章那样阅读其内容,并使用非统一IO调用,例如File.ReadAllText(Application.streamingAssetsPath + Rules.json)
?
是的,对此的“规范”方法是什么?使用这种方法,仍然有可能将受影响的文件作为资产(例如类似于Resources.Load<Sprite>(path)
的文件),还是有必要使用.NET IO方法读取文件,然后将其手动转换为Unity对象?
解决方法
在Unity论坛上问了同样的问题之后,建议我使用StreamingAssets
文件夹,并告诉我必须使用.NET IO方法。
此处显示了如何使用标准IO将精灵作为文件加载为示例的示例:https://forum.unity.com/threads/generating-sprites-dynamically-from-png-or-jpeg-files-in-c.343735/
static public Sprite LoadSpriteFromFile(
string filename,float PixelsPerUnit = 100.0f,SpriteMeshType type = SpriteMeshType.FullRect)
{
// Load a PNG or JPG image from disk to a Texture2D,assign this texture to a new sprite and return its reference
Texture2D SpriteTexture = LoadTexture(filename);
Sprite NewSprite = Sprite.Create(
SpriteTexture,new Rect(0,SpriteTexture.width,SpriteTexture.height),new Vector2(0,0),PixelsPerUnit,type);
return NewSprite;
}
static private Texture2D LoadTexture(string FilePath)
{
// Load a PNG or JPG file from disk to a Texture2D
// Returns null if load fails
Texture2D Tex2D;
byte[] FileData;
if (File.Exists(FilePath))
{
FileData = File.ReadAllBytes(FilePath);
Tex2D = new Texture2D(2,2);
// Load the imagedata into the texture (size is set automatically)
if (Tex2D.LoadImage(FileData))
{
return Tex2D; // If data = readable -> return texture
}
}
return null;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。