Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).

## [Unreleased]

### Added

- Added a way to access the GlobalObjectIdHash via PrefabIdHash for use in the Connection Approval Callback. (#2437)
- 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)
- Added `NetworkTransform.UseQuaternionSynchronization` property that, when enabled, will synchronize the entire quaternion. (#2388)
- 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)
Expand Down
22 changes: 22 additions & 0 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@ public sealed class NetworkObject : MonoBehaviour
[SerializeField]
internal uint GlobalObjectIdHash;

/// <summary>
/// Gets the Prefab Hash Id of this object if the object is registerd as a prefab otherwise it returns 0
/// </summary>
[HideInInspector]
public uint PrefabIdHash
{
get
{
foreach (var prefab in NetworkManager.NetworkConfig.Prefabs.Prefabs)
{
if (prefab.Prefab == gameObject)
{
return GlobalObjectIdHash;
}
}

return 0;
}
}

private bool m_IsPrefab;

#if UNITY_EDITOR
private void OnValidate()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using NUnit.Framework;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine;

namespace Unity.Netcode.RuntimeTests
{
/// <summary>
/// Tests properties of NetworkObject for proper functionality.
/// </summary>
public class NetworkObjectPropertyTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 1;

private NetworkObject m_TestPrefabNetworkObject;

protected override void OnServerAndClientsCreated()
{
// create prefab and get the NetworkObject component attached to it
m_TestPrefabNetworkObject = CreateNetworkObjectPrefab("TestObject").GetComponent<NetworkObject>();
}

/// <summary>
/// Tests PrefabHashId returns correctly when the NetworkObject is not a prefab.
/// </summary>
[Test]
public void TestPrefabHashIdPropertyNotAPrefab()
{
const uint kInvalidPrefabHashId = 0;

var gameObject = new GameObject("TestObject");
var networkObject = gameObject.AddComponent<NetworkObject>();
Assert.AreEqual(kInvalidPrefabHashId, networkObject.PrefabIdHash);
}

/// <summary>
/// Tests PrefabHashId returns correctly when the NetworkObject is a prefab.
/// </summary>
/// <returns></returns>
[Test]
public void TestPrefabHashIdPropertyIsAPrefab()
{
Assert.AreEqual(m_TestPrefabNetworkObject.GlobalObjectIdHash, m_TestPrefabNetworkObject.PrefabIdHash);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.