|
1 | | -# unity-networking-parenting-solution-for-distributed-authority |
| 1 | +# 🎮 Unity Network Parenting Solution |
| 2 | + |
| 3 | +A robust solution for parenting network objects under deeply nested GameObjects in Unity Netcode for GameObjects with **Distributed Authority** topology. |
| 4 | + |
| 5 | +## 🚀 Features |
| 6 | + |
| 7 | +- ✨ **Deep Nesting Support**: Parent network objects to deeply nested child GameObjects |
| 8 | +- 🔄 **Automatic Synchronization**: All clients receive parenting changes automatically |
| 9 | +- 🕐 **Late Join Support**: Players joining mid-game see correct object hierarchy |
| 10 | +- 🎯 **Simple API**: One method call to parent any network object |
| 11 | +- 🏗️ **Distributed Authority**: Designed for distributed authority network topology |
| 12 | + |
| 13 | +## 📋 Prerequisites |
| 14 | + |
| 15 | +- Unity Netcode for GameObjects |
| 16 | +- Network Manager with **Distributed Authority** topology |
| 17 | +- Target network objects must have **Auto Object Parent Sync turned OFF** |
| 18 | +- Network objects to be parented must have **Ownership set to None** |
| 19 | + |
| 20 | +## 🔧 Setup |
| 21 | + |
| 22 | +### 1️⃣ Add Network Parent Centre |
| 23 | + |
| 24 | +Attach the `NetworkParentCentre` component to the **root GameObject** of your network object (e.g., Player): |
| 25 | + |
| 26 | +``` |
| 27 | +Player (Network Object) ← NetworkParentCentre goes here |
| 28 | +├── Arm (child) |
| 29 | +│ └── Hand (child) ← NetworkParent goes here |
| 30 | +└── Other components... |
| 31 | +``` |
| 32 | + |
| 33 | +### 2️⃣ Configure Network Parents |
| 34 | + |
| 35 | +Add `NetworkParent` components to any child GameObject where you want to enable parenting: |
| 36 | + |
| 37 | +```csharp |
| 38 | +// On the Hand GameObject |
| 39 | +NetworkParent networkParent = handGameObject.GetComponent<NetworkParent>(); |
| 40 | +networkParent.NetworkParentId = "Object Holder Network Parent"; |
| 41 | +``` |
| 42 | + |
| 43 | +### 3️⃣ Parent Network Objects |
| 44 | + |
| 45 | +Use the simple API to parent any spawned network object: |
| 46 | + |
| 47 | +```csharp |
| 48 | +// Get reference to the Network Parent Centre |
| 49 | +NetworkParentCentre parentCentre = playerGameObject.GetComponent<NetworkParentCentre>(); |
| 50 | + |
| 51 | +// Parent the network item (e.g., weapon) to the specified parent |
| 52 | +bool success = parentCentre.TryToParentNetworkObject(weaponNetworkObject, "Object Holder Network Parent"); |
| 53 | +``` |
| 54 | + |
| 55 | +## 💡 Example Use Case |
| 56 | + |
| 57 | +Perfect for scenarios like: |
| 58 | +- 🗡️ **Weapon Systems**: Parenting weapons to player hands |
| 59 | +- 📦 **Inventory Items**: Attaching items to specific body parts |
| 60 | +- 🎒 **Equipment**: Mounting gear to character attachment points |
| 61 | +- 🚗 **Vehicle Interactions**: Placing objects inside vehicles |
| 62 | + |
| 63 | +## ⚙️ Configuration Requirements |
| 64 | + |
| 65 | +| Setting | Value | Location | |
| 66 | +|---------|--------|----------| |
| 67 | +| Network Manager Topology | `Distributed Authority` | Network Manager | |
| 68 | +| Auto Object Parent Sync | `OFF` | Network Object to be parented | |
| 69 | +| Object Ownership | `None` | Network Object to be parented | |
| 70 | + |
| 71 | +## 📝 API Reference |
| 72 | + |
| 73 | +### NetworkParentCentre |
| 74 | + |
| 75 | +```csharp |
| 76 | +public bool TryToParentNetworkObject(NetworkObjectReference networkObjectReference, string networkParentId) |
| 77 | +``` |
| 78 | + |
| 79 | +**Parameters:** |
| 80 | +- `networkObjectReference`: The network object to be parented |
| 81 | +- `networkParentId`: The identifier of the target parent |
| 82 | + |
| 83 | +**Returns:** `bool` - Success status of the parenting operation |
| 84 | + |
| 85 | +### NetworkParent |
| 86 | + |
| 87 | +```csharp |
| 88 | +public string NetworkParentId; // Set this to identify the parent location |
| 89 | +``` |
| 90 | + |
| 91 | +## 🔍 How It Works |
| 92 | + |
| 93 | +1. **Initialization**: `NetworkParentCentre` discovers all `NetworkParent` components in children |
| 94 | +2. **Parenting Request**: Call `TryToParentNetworkObject()` with target object and parent ID |
| 95 | +3. **Local Parenting**: Object is immediately parented locally on the owner |
| 96 | +4. **Network Sync**: `ClientRpc` synchronizes the parenting across all clients |
| 97 | +5. **Late Join Support**: New clients automatically receive the correct hierarchy |
| 98 | + |
| 99 | +## 🚨 Important Notes |
| 100 | + |
| 101 | +- ⚠️ Only the **owner** of the NetworkParentCentre can initiate parenting operations |
| 102 | +- 🏗️ NetworkParentCentre must be on a **root GameObject** (no parent) |
| 103 | +- 🔒 Target network objects must have **ownership set to None** |
| 104 | +- 📴 Ensure **Auto Object Parent Sync is disabled** on objects to be parented |
| 105 | + |
| 106 | +## 🤝 Contributing |
| 107 | + |
| 108 | +Feel free to submit issues and enhancement requests! |
| 109 | + |
| 110 | +## 📄 License |
| 111 | + |
| 112 | +[Your License Here] |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +Made with ❤️ for the Unity Netcode community |
0 commit comments