This repository has been archived on 2025-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs
Unity Technologies 60e2dabef4 com.unity.netcode.gameobjects@1.0.0-pre.7
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).

## [1.0.0-pre.7] - 2022-04-01

### Added

- Added editor only check prior to entering into play mode if the currently open and active scene is in the build list and if not displays a dialog box asking the user if they would like to automatically add it prior to entering into play mode. (#1828)
- Added `UnityTransport` implementation and `com.unity.transport` package dependency (#1823)
- Added `NetworkVariableWritePermission` to `NetworkVariableBase` and implemented `Owner` client writable netvars. (#1762)
- `UnityTransport` settings can now be set programmatically. (#1845)
- `FastBufferWriter` and Reader IsInitialized property. (#1859)

### Changed

- Updated `UnityTransport` dependency on `com.unity.transport` to 1.0.0 (#1849)

### Removed

- Removed `SnapshotSystem` (#1852)
- Removed `com.unity.modules.animation`, `com.unity.modules.physics` and `com.unity.modules.physics2d` dependencies from the package (#1812)
- Removed `com.unity.collections` dependency from the package (#1849)

### Fixed
- Fixed in-scene placed NetworkObjects not being found/ignored after a client disconnects and then reconnects. (#1850)
- Fixed issue where `UnityTransport` send queues were not flushed when calling `DisconnectLocalClient` or `DisconnectRemoteClient`. (#1847)
- Fixed NetworkBehaviour dependency verification check for an existing NetworkObject not searching from root parent transform relative GameObject. (#1841)
- Fixed issue where entries were not being removed from the NetworkSpawnManager.OwnershipToObjectsTable. (#1838)
- Fixed ClientRpcs would always send to all connected clients by default as opposed to only sending to the NetworkObject's Observers list by default. (#1836)
- Fixed clarity for NetworkSceneManager client side notification when it receives a scene hash value that does not exist in its local hash table. (#1828)
- Fixed client throws a key not found exception when it times out using UNet or UTP. (#1821)
- Fixed network variable updates are no longer limited to 32,768 bytes when NetworkConfig.EnsureNetworkVariableLengthSafety is enabled. The limits are now determined by what the transport can send in a message. (#1811)
- Fixed in-scene NetworkObjects get destroyed if a client fails to connect and shuts down the NetworkManager. (#1809)
- Fixed user never being notified in the editor that a NetworkBehaviour requires a NetworkObject to function properly. (#1808)
- Fixed PlayerObjects and dynamically spawned NetworkObjects not being added to the NetworkClient's OwnedObjects (#1801)
- Fixed issue where NetworkManager would continue starting even if the NetworkTransport selected failed. (#1780)
- Fixed issue when spawning new player if an already existing player exists it does not remove IsPlayer from the previous player (#1779)
- Fixed lack of notification that NetworkManager and NetworkObject cannot be added to the same GameObject with in-editor notifications (#1777)
- Fixed parenting warning printing for false positives (#1855)
2022-04-01 00:00:00 +00:00

529 lines
17 KiB
C#

using System;
using System.Collections.Generic;
using Unity.Collections;
namespace Unity.Netcode
{
/// <summary>
/// Event based NetworkVariable container for syncing Lists
/// </summary>
/// <typeparam name="T">The type for the list</typeparam>
public class NetworkList<T> : NetworkVariableBase where T : unmanaged, IEquatable<T>
{
private NativeList<T> m_List = new NativeList<T>(64, Allocator.Persistent);
private NativeList<NetworkListEvent<T>> m_DirtyEvents = new NativeList<NetworkListEvent<T>>(64, Allocator.Persistent);
/// <summary>
/// Delegate type for list changed event
/// </summary>
/// <param name="changeEvent">Struct containing information about the change event</param>
public delegate void OnListChangedDelegate(NetworkListEvent<T> changeEvent);
/// <summary>
/// The callback to be invoked when the list gets changed
/// </summary>
public event OnListChangedDelegate OnListChanged;
public NetworkList() { }
public NetworkList(IEnumerable<T> values = default,
NetworkVariableReadPermission readPerm = DefaultReadPerm,
NetworkVariableWritePermission writePerm = DefaultWritePerm)
: base(readPerm, writePerm)
{
foreach (var value in values)
{
m_List.Add(value);
}
}
/// <inheritdoc />
public override void ResetDirty()
{
base.ResetDirty();
m_DirtyEvents.Clear();
}
/// <inheritdoc />
public override bool IsDirty()
{
// we call the base class to allow the SetDirty() mechanism to work
return base.IsDirty() || m_DirtyEvents.Length > 0;
}
/// <inheritdoc />
public override void WriteDelta(FastBufferWriter writer)
{
if (base.IsDirty())
{
writer.WriteValueSafe((ushort)1);
writer.WriteValueSafe(NetworkListEvent<T>.EventType.Full);
WriteField(writer);
return;
}
writer.WriteValueSafe((ushort)m_DirtyEvents.Length);
for (int i = 0; i < m_DirtyEvents.Length; i++)
{
writer.WriteValueSafe(m_DirtyEvents[i].Type);
switch (m_DirtyEvents[i].Type)
{
case NetworkListEvent<T>.EventType.Add:
{
NetworkVariable<T>.Write(writer, m_DirtyEvents[i].Value);
}
break;
case NetworkListEvent<T>.EventType.Insert:
{
writer.WriteValueSafe(m_DirtyEvents[i].Index);
NetworkVariable<T>.Write(writer, m_DirtyEvents[i].Value);
}
break;
case NetworkListEvent<T>.EventType.Remove:
{
NetworkVariable<T>.Write(writer, m_DirtyEvents[i].Value);
}
break;
case NetworkListEvent<T>.EventType.RemoveAt:
{
writer.WriteValueSafe(m_DirtyEvents[i].Index);
}
break;
case NetworkListEvent<T>.EventType.Value:
{
writer.WriteValueSafe(m_DirtyEvents[i].Index);
NetworkVariable<T>.Write(writer, m_DirtyEvents[i].Value);
}
break;
case NetworkListEvent<T>.EventType.Clear:
{
//Nothing has to be written
}
break;
}
}
}
/// <inheritdoc />
public override void WriteField(FastBufferWriter writer)
{
writer.WriteValueSafe((ushort)m_List.Length);
for (int i = 0; i < m_List.Length; i++)
{
NetworkVariable<T>.Write(writer, m_List[i]);
}
}
/// <inheritdoc />
public override void ReadField(FastBufferReader reader)
{
m_List.Clear();
reader.ReadValueSafe(out ushort count);
for (int i = 0; i < count; i++)
{
NetworkVariable<T>.Read(reader, out T value);
m_List.Add(value);
}
}
/// <inheritdoc />
public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
{
reader.ReadValueSafe(out ushort deltaCount);
for (int i = 0; i < deltaCount; i++)
{
reader.ReadValueSafe(out NetworkListEvent<T>.EventType eventType);
switch (eventType)
{
case NetworkListEvent<T>.EventType.Add:
{
NetworkVariable<T>.Read(reader, out T value);
m_List.Add(value);
if (OnListChanged != null)
{
OnListChanged(new NetworkListEvent<T>
{
Type = eventType,
Index = m_List.Length - 1,
Value = m_List[m_List.Length - 1]
});
}
if (keepDirtyDelta)
{
m_DirtyEvents.Add(new NetworkListEvent<T>()
{
Type = eventType,
Index = m_List.Length - 1,
Value = m_List[m_List.Length - 1]
});
}
}
break;
case NetworkListEvent<T>.EventType.Insert:
{
reader.ReadValueSafe(out int index);
NetworkVariable<T>.Read(reader, out T value);
m_List.InsertRangeWithBeginEnd(index, index + 1);
m_List[index] = value;
if (OnListChanged != null)
{
OnListChanged(new NetworkListEvent<T>
{
Type = eventType,
Index = index,
Value = m_List[index]
});
}
if (keepDirtyDelta)
{
m_DirtyEvents.Add(new NetworkListEvent<T>()
{
Type = eventType,
Index = index,
Value = m_List[index]
});
}
}
break;
case NetworkListEvent<T>.EventType.Remove:
{
NetworkVariable<T>.Read(reader, out T value);
int index = m_List.IndexOf(value);
if (index == -1)
{
break;
}
m_List.RemoveAt(index);
if (OnListChanged != null)
{
OnListChanged(new NetworkListEvent<T>
{
Type = eventType,
Index = index,
Value = value
});
}
if (keepDirtyDelta)
{
m_DirtyEvents.Add(new NetworkListEvent<T>()
{
Type = eventType,
Index = index,
Value = value
});
}
}
break;
case NetworkListEvent<T>.EventType.RemoveAt:
{
reader.ReadValueSafe(out int index);
T value = m_List[index];
m_List.RemoveAt(index);
if (OnListChanged != null)
{
OnListChanged(new NetworkListEvent<T>
{
Type = eventType,
Index = index,
Value = value
});
}
if (keepDirtyDelta)
{
m_DirtyEvents.Add(new NetworkListEvent<T>()
{
Type = eventType,
Index = index,
Value = value
});
}
}
break;
case NetworkListEvent<T>.EventType.Value:
{
reader.ReadValueSafe(out int index);
NetworkVariable<T>.Read(reader, out T value);
if (index >= m_List.Length)
{
throw new Exception("Shouldn't be here, index is higher than list length");
}
var previousValue = m_List[index];
m_List[index] = value;
if (OnListChanged != null)
{
OnListChanged(new NetworkListEvent<T>
{
Type = eventType,
Index = index,
Value = value,
PreviousValue = previousValue
});
}
if (keepDirtyDelta)
{
m_DirtyEvents.Add(new NetworkListEvent<T>()
{
Type = eventType,
Index = index,
Value = value,
PreviousValue = previousValue
});
}
}
break;
case NetworkListEvent<T>.EventType.Clear:
{
//Read nothing
m_List.Clear();
if (OnListChanged != null)
{
OnListChanged(new NetworkListEvent<T>
{
Type = eventType,
});
}
if (keepDirtyDelta)
{
m_DirtyEvents.Add(new NetworkListEvent<T>()
{
Type = eventType
});
}
}
break;
case NetworkListEvent<T>.EventType.Full:
{
ReadField(reader);
ResetDirty();
}
break;
}
}
}
/// <inheritdoc />
public IEnumerator<T> GetEnumerator()
{
return m_List.GetEnumerator();
}
/// <inheritdoc />
public void Add(T item)
{
m_List.Add(item);
var listEvent = new NetworkListEvent<T>()
{
Type = NetworkListEvent<T>.EventType.Add,
Value = item,
Index = m_List.Length - 1
};
HandleAddListEvent(listEvent);
}
/// <inheritdoc />
public void Clear()
{
m_List.Clear();
var listEvent = new NetworkListEvent<T>()
{
Type = NetworkListEvent<T>.EventType.Clear
};
HandleAddListEvent(listEvent);
}
/// <inheritdoc />
public bool Contains(T item)
{
int index = NativeArrayExtensions.IndexOf(m_List, item);
return index != -1;
}
/// <inheritdoc />
public bool Remove(T item)
{
int index = NativeArrayExtensions.IndexOf(m_List, item);
if (index == -1)
{
return false;
}
m_List.RemoveAt(index);
var listEvent = new NetworkListEvent<T>()
{
Type = NetworkListEvent<T>.EventType.Remove,
Value = item
};
HandleAddListEvent(listEvent);
return true;
}
/// <inheritdoc />
public int Count => m_List.Length;
/// <inheritdoc />
public int IndexOf(T item)
{
return m_List.IndexOf(item);
}
/// <inheritdoc />
public void Insert(int index, T item)
{
m_List.InsertRangeWithBeginEnd(index, index + 1);
m_List[index] = item;
var listEvent = new NetworkListEvent<T>()
{
Type = NetworkListEvent<T>.EventType.Insert,
Index = index,
Value = item
};
HandleAddListEvent(listEvent);
}
/// <inheritdoc />
public void RemoveAt(int index)
{
m_List.RemoveAt(index);
var listEvent = new NetworkListEvent<T>()
{
Type = NetworkListEvent<T>.EventType.RemoveAt,
Index = index
};
HandleAddListEvent(listEvent);
}
/// <inheritdoc />
public T this[int index]
{
get => m_List[index];
set
{
m_List[index] = value;
var listEvent = new NetworkListEvent<T>()
{
Type = NetworkListEvent<T>.EventType.Value,
Index = index,
Value = value
};
HandleAddListEvent(listEvent);
}
}
private void HandleAddListEvent(NetworkListEvent<T> listEvent)
{
m_DirtyEvents.Add(listEvent);
OnListChanged?.Invoke(listEvent);
}
public int LastModifiedTick
{
get
{
// todo: implement proper network tick for NetworkList
return NetworkTickSystem.NoTick;
}
}
public override void Dispose()
{
m_List.Dispose();
m_DirtyEvents.Dispose();
}
}
/// <summary>
/// Struct containing event information about changes to a NetworkList.
/// </summary>
/// <typeparam name="T">The type for the list that the event is about</typeparam>
public struct NetworkListEvent<T>
{
/// <summary>
/// Enum representing the different operations available for triggering an event.
/// </summary>
public enum EventType : byte
{
/// <summary>
/// Add
/// </summary>
Add,
/// <summary>
/// Insert
/// </summary>
Insert,
/// <summary>
/// Remove
/// </summary>
Remove,
/// <summary>
/// Remove at
/// </summary>
RemoveAt,
/// <summary>
/// Value changed
/// </summary>
Value,
/// <summary>
/// Clear
/// </summary>
Clear,
/// <summary>
/// Full list refresh
/// </summary>
Full
}
/// <summary>
/// Enum representing the operation made to the list.
/// </summary>
public EventType Type;
/// <summary>
/// The value changed, added or removed if available.
/// </summary>
public T Value;
/// <summary>
/// The previous value when "Value" has changed, if available.
/// </summary>
public T PreviousValue;
/// <summary>
/// the index changed, added or removed if available
/// </summary>
public int Index;
}
}