How to read data from Unity3D's prefab file? (Parse prefab on binary level)

So, I am trying to create an test application for online Unity game but to do what I need it to do, I must read datas from a hex offset located in a .prefab file. And then, i will load prefabs on server-side from data. For an anti-collider hack from client-side.

I must read datas like unityVer, localScale, localPosition, localSize, collider size… etc.

Edit: I must make it on a simple console application, not in Unity.

My not working code :

try {
            string[] prefabFiles = Directory.GetFiles(Path.Combine(Environment.CurrentDirectory, "data\\prefabs\\"));
            foreach (var prefab in prefabFiles) {
                if (prefab != null && prefab.Split('.')[1] == "prefab") {
                    try {
                        using (var reader = new BinaryReader(new FileStream(prefab, FileMode.Open, FileAccess.Read))) {
                            FileInfo info = new FileInfo(prefab);
                            Console.WriteLine("Analyzing File : " + info.Name);

                            reader.BaseStream.Position = 0xA;

                            //string unityVer = reader...

                            //float localPositionX = reader...
                            //float localPositionY = reader...
                            //float localPositionZ = reader...

                            //float localScaleX = reader...
                            //float localScaleY = reader...
                            //float localScaleZ = reader...

                            //float colliderRadius = reader...
                        }
                        Console.WriteLine("File parse success.");
                    } catch { Console.WriteLine("Parse error in the data file."); }
                }
            }
        } catch (Exception e_Ex) {
            Console.WriteLine(e_Ex.ToString());
        }

My Prefab’s Hex Tree:

I am new to reading and writing data located in a hex data location so any help would be awesome!

I don’t really have any experience working with prefab files (or any asset file for that matter) in binary, but from what I know, you can’t really trust the order of a file, so using an offset is probably not the way to go. If you do wish to parse asset files manually, and outside of unity, I would recommend having those be text files.

Under ProjectSettings->Editor, you can set asset serialization into a text mode. This will create a YAML file for each asset as opposed to having them be binary. There is somewhere in the manual an explanation on how that is constructed, and this should be much easier to read and parse.

1 Like