-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding struct to sync a prefab over network using its hash
- Loading branch information
1 parent
77a4d4a
commit 15e7bbe
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System.Collections.Generic; | ||
using Mirage.Serialization; | ||
|
||
namespace Mirage | ||
{ | ||
public struct SyncPrefab | ||
{ | ||
public NetworkIdentity Prefab; | ||
public int PrefabHash; | ||
|
||
public SyncPrefab(NetworkIdentity prefab) : this() | ||
{ | ||
Prefab = prefab; | ||
} | ||
|
||
public SyncPrefab(int hash) : this() | ||
{ | ||
PrefabHash = hash; | ||
} | ||
|
||
/// <summary> | ||
/// Searches ClientObjectManager to find a prefab using its hash | ||
/// </summary> | ||
/// <param name="manager"></param> | ||
/// <returns></returns> | ||
public NetworkIdentity FindPrefab(ClientObjectManager manager) | ||
{ | ||
if (Prefab == null && PrefabHash != 0) | ||
{ | ||
var handler = manager.GetSpawnHandler(PrefabHash); | ||
Prefab = handler.Prefab; | ||
} | ||
return Prefab; | ||
} | ||
|
||
/// <summary> | ||
/// Searches ClientObjectManager to find a prefab using its hash | ||
/// </summary> | ||
/// <param name="manager"></param> | ||
/// <returns></returns> | ||
public NetworkIdentity FindPrefab(IEnumerable<NetworkIdentity> collection) | ||
{ | ||
if (Prefab == null && PrefabHash != 0) | ||
{ | ||
foreach (var item in collection) | ||
{ | ||
if (item.PrefabHash == PrefabHash) | ||
{ | ||
Prefab = item; | ||
break; | ||
} | ||
} | ||
} | ||
return Prefab; | ||
} | ||
} | ||
|
||
public static class SyncPrefabSerialize | ||
{ | ||
public static void WriteSyncPrefab(this NetworkWriter writer, SyncPrefab value) | ||
{ | ||
if (value.PrefabHash == 0) | ||
value.PrefabHash = GetPrefabHash(value); | ||
|
||
writer.WriteInt32(value.PrefabHash); | ||
} | ||
|
||
private static int GetPrefabHash(SyncPrefab value) | ||
{ | ||
var prefab = value.Prefab; | ||
if (prefab == null) | ||
return 0; | ||
|
||
return prefab.PrefabHash; | ||
} | ||
|
||
public static SyncPrefab ReadSyncPrefab(this NetworkReader reader) | ||
{ | ||
var hash = reader.ReadInt32(); | ||
return new SyncPrefab(hash); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.