首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使玩家可以使用资产进行调整?

如何使玩家可以使用资产进行调整?
EN

Stack Overflow用户
提问于 2020-08-12 13:16:20
回答 1查看 71关注 0票数 1

我的游戏包括图像文件和json配置文件,我希望在部署的游戏的文件夹结构中可以访问这些文件,这样玩家就可以轻松地编辑或交换它们。

我曾考虑/尝试下列方法:

  1. 我最初的方法是使用Resources文件夹和代码(如Resources.Load<TextAsset>("Rules.json") )。当然,在构建过程中资源文件夹编译时,这是行不通的。
  2. I研究了AddressablesAssetBundle特性,但它们似乎并不是为了解决这个问题。在询问之后,
  3. 使用了.NET自己的文件方法,转而使用File.ReadAllText(Application.dataPath + Rules.json)之类的代码。这似乎是可行的,但此类文件仍未自动部署,必须手动复制。
  4. 似乎存在这样的StreamingAssets文件夹,因为手册宣称其内容是逐字复制到目标机器上的。我假设它的内容应该与前面的内容一样,使用非统一IO调用(如File.ReadAllText(Application.streamingAssetsPath + Rules.json)

)来读取。

所以是的,什么是“规范”的方法?使用这种方法,是否仍然可以将受影响的文件作为资产(例如类似于Resources.Load<Sprite>(path)的文件),或者是否有必要使用.NET IO方法读取文件,然后手动将它们转换为Unity?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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/

代码语言:javascript
复制
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;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63377353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档