我的游戏包括图像文件和json配置文件,我希望在部署的游戏的文件夹结构中可以访问这些文件,这样玩家就可以轻松地编辑或交换它们。
我曾考虑/尝试下列方法:
Resources文件夹和代码(如Resources.Load<TextAsset>("Rules.json") )。当然,在构建过程中资源文件夹编译时,这是行不通的。Addressables和AssetBundle特性,但它们似乎并不是为了解决这个问题。在询问之后,File.ReadAllText(Application.dataPath + Rules.json)之类的代码。这似乎是可行的,但此类文件仍未自动部署,必须手动复制。StreamingAssets文件夹,因为手册宣称其内容是逐字复制到目标机器上的。我假设它的内容应该与前面的内容一样,使用非统一IO调用(如File.ReadAllText(Application.streamingAssetsPath + Rules.json))来读取。
所以是的,什么是“规范”的方法?使用这种方法,是否仍然可以将受影响的文件作为资产(例如类似于Resources.Load<Sprite>(path)的文件),或者是否有必要使用.NET IO方法读取文件,然后手动将它们转换为Unity?
发布于 2020-08-13 18:31:24
在联合论坛上问了同样的问题之后,我被建议使用StreamingAssets文件夹,并告诉我有必要对它使用.NET IO方法。
关于如何使用标准IO以文件的形式加载sprites的示例可以在这里看到: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,
0,
SpriteTexture.width,
SpriteTexture.height),
new Vector2(0, 0),
PixelsPerUnit,
0,
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);
// If the image is blurrier than what you get with a manual Unity import, try tweaking these two lines:
Tex2D.wrapMode = TextureWrapMode.Clamp;
Tex2d.filterMode = FilterMode.Bilinear;
// Load the imagedata into the texture (size is set automatically)
if (Tex2D.LoadImage(FileData))
{
return Tex2D; // If data = readable -> return texture
}
}
return null;
}https://stackoverflow.com/questions/63377353
复制相似问题