Unity格斗游戏MoveFrame数据存储格式及C#解析方案咨询
适合Unity格斗游戏MoveFrame数据的存储方案
一、JSON(你考虑的方案完全可行)
C#和Unity原生就支持JSON解析,用JsonUtility就能搞定,不过得先调整你的MoveFrame类和hitbox结构体——默认JsonUtility要求成员要么是public,要么加[SerializeField]标记,同时类和结构体要加[Serializable]属性才能被序列化。
修改后的代码示例:
using UnityEngine; using System; [Serializable] public class MoveFrame { [SerializeField] private Rect[] hurtboxes; [SerializeField] private hitbox[] hitboxes; [SerializeField] private Rect collision; [SerializeField] private int velocity; [SerializeField] private bool counter; public MoveFrame(Rect[] hurt, hitbox[] hit, Rect coll, int velo, bool ch) { hurtboxes = hurt; hitboxes = hit; collision = coll; velocity = velo; counter = ch; } [Serializable] public struct hitbox { [SerializeField] int hId; [SerializeField] int dmg; [SerializeField] bool scale; [SerializeField] Rect[] ground; [SerializeField] Rect[] air; } }
序列化/解析代码:
// 把MoveFrame数组转成格式化的JSON字符串 MoveFrame[] yourMoveFrames = GetYourMoveFrames(); string json = JsonUtility.ToJson(new MoveFrameWrapper { frames = yourMoveFrames }, prettyPrint: true); // 保存到Unity项目目录 System.IO.File.WriteAllText(Application.dataPath + "/PlayerMoves.json", json); // 从JSON文件读取解析 string jsonContent = System.IO.File.ReadAllText(Application.dataPath + "/PlayerMoves.json"); MoveFrameWrapper wrapper = JsonUtility.FromJson<MoveFrameWrapper>(jsonContent); MoveFrame[] loadedFrames = wrapper.frames; // 因为JsonUtility直接序列化数组有点麻烦,所以加个包装类 [Serializable] public class MoveFrameWrapper { public MoveFrame[] frames; }
生成的JSON格式示例(易读易编辑):
{ "frames": [ { "hurtboxes": [ {"x": 0, "y": 0, "width": 10, "height": 20} ], "hitboxes": [ { "hId": 1, "dmg": 15, "scale": true, "ground": [{"x": 5, "y": 0, "width": 12, "height": 18}], "air": [] } ], "collision": {"x": 0, "y": -5, "width": 8, "height": 10}, "velocity": 3, "counter": false } ] }
二、YAML(更简洁易读的文本格式)
YAML比JSON更清爽,不用写多余的大括号和引号,适合非技术人员修改。Unity里可以用YamlDotNet库(直接在Package Manager搜索安装,或者导入dll),用法和JSON类似,只要类和结构体标记可序列化就行。
YAML示例:
frames: - hurtboxes: - {x: 0, y: 0, width: 10, height: 20} hitboxes: - hId: 1 dmg: 15 scale: true ground: - {x: 5, y: 0, width: 12, height: 18} air: [] collision: {x: 0, y: -5, width: 8, height: 10} velocity: 3 counter: false
三、Unity ScriptableObject(最适合Unity场景的方案)
如果你是在Unity里开发,这绝对是最优解——它是Unity原生的资源格式,能直接在编辑器可视化编辑,不用手动写文本文件,哪怕是不懂代码的策划,也能直接在Inspector面板里调整数值。
实现步骤:
- 创建继承自
ScriptableObject的资源类:
using UnityEngine; // 右键菜单创建资源 [CreateAssetMenu(fileName = "NewMoveSet", menuName = "格斗游戏/动作集合")] public class MoveSet : ScriptableObject { public MoveFrame[] frames; } [System.Serializable] public class MoveFrame { public Rect[] hurtboxes; public hitbox[] hitboxes; public Rect collision; public int velocity; public bool counter; } [System.Serializable] public struct hitbox { public int hId; public int dmg; public bool scale; public Rect[] ground; public Rect[] air; }
- 在Unity编辑器右键→格斗游戏→动作集合,生成一个新的资源文件。
- 双击打开资源,就能在Inspector里直接添加/修改
MoveFrame、hitbox的所有数值,完全可视化。 - 代码里直接引用这个资源:
public MoveSet playerMoveSet; // 在Inspector拖入生成的动作集合资源 void Start() { MoveFrame[] frames = playerMoveSet.frames; // 直接使用动作帧数据 }
总结
- 偏好文本格式的话,JSON是最稳妥的,Unity原生支持不用额外库;YAML更易读,但需要导入第三方库。
- 要是纯Unity开发,ScriptableObject是最佳选择,零文本编辑成本,团队协作时策划也能直接改数值,门槛极低。
内容的提问来源于stack exchange,提问作者MasterMT
相关产品推荐
相关产品推荐

