I made myself a prefab system. It came out pretty minimalistic. And I’m very happy with it. Even nested prefabs are working (literally better than Unity). Sharing it, so maybe someone will find it useful or give me feedback (because I’m not very confident with C++). And maybe we will make it into PR. Licensed under CC0.
Here is the video demonstration:
So, basically, each node has a reference to a XMLfile, if it is empty, then nothing happens, it’s a normal node, if it is pointing to a file, then:
- on save will skip saving it’s children and components
- on load this node will load object from this xml file, and applying it’s transform to it.
I also added live reload handling. And save prefab button. (Using this marvelous hack)
Here is a full code: Node.h and Node.cpp
I use Urho1.7, so the code is a bit old.
Basically, I added two new attributes:
URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Prefab", GetPrefab, SetPrefab, ResourceRef, ResourceRef(XMLFile::GetTypeStatic()), AM_FILE);
URHO3D_ACCESSOR_ATTRIBUTE("Save Prefab", GetFalse, SavePrefab, bool, false, AM_EDIT);
In Node::SaveXML, after saving id and attributes:
if (prefab_ != NULL)
return true;
And then this four new functions:
And lastly I had to make room in the editor interface, AttributeEditor.as
const uint MIN_NODE_ATTRIBUTES = 7;
const uint MAX_NODE_ATTRIBUTES = 11;