-
Notifications
You must be signed in to change notification settings - Fork 447
/
Copy pathNetworkPrefabsList.cs
95 lines (84 loc) · 3.22 KB
/
NetworkPrefabsList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
namespace Unity.Netcode
{
/// <summary>
/// A ScriptableObject for holding a network prefabs list, which can be
/// shared between multiple NetworkManagers.
///
/// When NetworkManagers hold references to this list, modifications to the
/// list at runtime will be picked up by all NetworkManagers that reference it.
/// </summary>
[CreateAssetMenu(fileName = "NetworkPrefabsList", menuName = "Netcode/Network Prefabs List")]
public class NetworkPrefabsList : ScriptableObject
{
internal delegate void OnAddDelegate(NetworkPrefab prefab);
internal OnAddDelegate OnAdd;
internal delegate void OnRemoveDelegate(NetworkPrefab prefab);
internal OnRemoveDelegate OnRemove;
[SerializeField]
internal bool IsDefault;
[FormerlySerializedAs("Prefabs")]
[SerializeField]
internal List<NetworkPrefab> List = new List<NetworkPrefab>();
/// <summary>
/// Read-only view into the prefabs list, enabling iterating and examining the list.
/// Actually modifying the list should be done using <see cref="Add"/>
/// and <see cref="Remove"/>.
/// </summary>
public IReadOnlyList<NetworkPrefab> PrefabList => List;
/// <summary>
/// Adds a prefab to the prefab list. Performing this here will apply the operation to all
/// <see cref="NetworkManager"/>s that reference this list.
/// </summary>
/// <param name="prefab">The NetworkPrefab to add to the shared list</param>
public void Add(NetworkPrefab prefab)
{
List.Add(prefab);
OnAdd?.Invoke(prefab);
}
/// <summary>
/// Removes a prefab from the prefab list. Performing this here will apply the operation to all
/// <see cref="NetworkManager"/>s that reference this list.
/// </summary>
/// <param name="prefab">The NetworkPrefab to remove from the shared list</param>
public void Remove(NetworkPrefab prefab)
{
List.Remove(prefab);
OnRemove?.Invoke(prefab);
}
/// <summary>
/// Check if the given GameObject is present as a prefab within the list
/// </summary>
/// <param name="prefab">The prefab to check</param>
/// <returns>Whether or not the prefab exists</returns>
public bool Contains(GameObject prefab)
{
for (int i = 0; i < List.Count; i++)
{
if (List[i].Prefab == prefab)
{
return true;
}
}
return false;
}
/// <summary>
/// Check if the given NetworkPrefab is present within the list
/// </summary>
/// <param name="prefab">The prefab to check</param>
/// <returns>Whether or not the prefab exists</returns>
public bool Contains(NetworkPrefab prefab)
{
for (int i = 0; i < List.Count; i++)
{
if (List[i].Equals(prefab))
{
return true;
}
}
return false;
}
}
}