Skip to content

Commit fdc58b0

Browse files
samlucas-unity0xFA11NoelStephensUnity
authored
feat: Added a way to safely access GlobalObjectIdHash via PrefabHashId [MTT-1645] (#2437)
* Added a way to access the GlobalObjectIdHash via PrefabHashId. * Added PrefabHashId tests. --------- Co-authored-by: Fatih Mar <[email protected]> Co-authored-by: Noel Stephens <[email protected]> Co-authored-by: NoelStephensUnity <[email protected]>
1 parent 8b01cbe commit fdc58b0

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
77
Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).
88

99
## [Unreleased]
10+
1011
### Added
1112

13+
- Added a way to access the GlobalObjectIdHash via PrefabIdHash for use in the Connection Approval Callback. (#2437)
1214
- Added `NetworkTransform.UseHalfFloatPrecision` property that, when enabled, will use half float values for position, rotation, and scale. This yields a 50% bandwidth savings a the cost of precision. (#2388)
1315
- Added `NetworkTransform.UseQuaternionSynchronization` property that, when enabled, will synchronize the entire quaternion. (#2388)
1416
- Added `NetworkTransform.UseQuaternionCompression` property that, when enabled, will use a smallest three implementation reducing a full quaternion synchronization update to the size of an unsigned integer. (#2388)

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,28 @@ public sealed class NetworkObject : MonoBehaviour
1717
[SerializeField]
1818
internal uint GlobalObjectIdHash;
1919

20+
/// <summary>
21+
/// Gets the Prefab Hash Id of this object if the object is registerd as a prefab otherwise it returns 0
22+
/// </summary>
23+
[HideInInspector]
24+
public uint PrefabIdHash
25+
{
26+
get
27+
{
28+
foreach (var prefab in NetworkManager.NetworkConfig.Prefabs.Prefabs)
29+
{
30+
if (prefab.Prefab == gameObject)
31+
{
32+
return GlobalObjectIdHash;
33+
}
34+
}
35+
36+
return 0;
37+
}
38+
}
39+
40+
private bool m_IsPrefab;
41+
2042
#if UNITY_EDITOR
2143
private void OnValidate()
2244
{
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using NUnit.Framework;
2+
using Unity.Netcode.TestHelpers.Runtime;
3+
using UnityEngine;
4+
5+
namespace Unity.Netcode.RuntimeTests
6+
{
7+
/// <summary>
8+
/// Tests properties of NetworkObject for proper functionality.
9+
/// </summary>
10+
public class NetworkObjectPropertyTests : NetcodeIntegrationTest
11+
{
12+
protected override int NumberOfClients => 1;
13+
14+
private NetworkObject m_TestPrefabNetworkObject;
15+
16+
protected override void OnServerAndClientsCreated()
17+
{
18+
// create prefab and get the NetworkObject component attached to it
19+
m_TestPrefabNetworkObject = CreateNetworkObjectPrefab("TestObject").GetComponent<NetworkObject>();
20+
}
21+
22+
/// <summary>
23+
/// Tests PrefabHashId returns correctly when the NetworkObject is not a prefab.
24+
/// </summary>
25+
[Test]
26+
public void TestPrefabHashIdPropertyNotAPrefab()
27+
{
28+
const uint kInvalidPrefabHashId = 0;
29+
30+
var gameObject = new GameObject("TestObject");
31+
var networkObject = gameObject.AddComponent<NetworkObject>();
32+
Assert.AreEqual(kInvalidPrefabHashId, networkObject.PrefabIdHash);
33+
}
34+
35+
/// <summary>
36+
/// Tests PrefabHashId returns correctly when the NetworkObject is a prefab.
37+
/// </summary>
38+
/// <returns></returns>
39+
[Test]
40+
public void TestPrefabHashIdPropertyIsAPrefab()
41+
{
42+
Assert.AreEqual(m_TestPrefabNetworkObject.GlobalObjectIdHash, m_TestPrefabNetworkObject.PrefabIdHash);
43+
}
44+
}
45+
}

com.unity.netcode.gameobjects/Tests/Runtime/NetworkObject/NetworkObjectPropertyTests.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)