Exemplo n.º 1
0
 private void DictionaryChanged(NetworkDictionaryEvent <int, string> dictionaryEvent)
 {
     if (!IsOwner && !m_GotNetworkSet)
     {
         Debug.Log("Received: " + dictionaryEvent.Key + ":" + dictionaryEvent.Value);
         m_GotNetworkDictionary = true;
     }
 }
Exemplo n.º 2
0
        /// <inheritdoc />
        public void ReadDelta(Stream stream, bool keepDirtyDelta, ushort localTick, ushort remoteTick)
        {
            using (var reader = PooledNetworkReader.Get(stream))
            {
                ushort deltaCount = reader.ReadUInt16Packed();
                for (int i = 0; i < deltaCount; i++)
                {
                    NetworkDictionaryEvent <TKey, TValue> .EventType eventType = (NetworkDictionaryEvent <TKey, TValue> .EventType)reader.ReadBits(3);
                    switch (eventType)
                    {
                    case NetworkDictionaryEvent <TKey, TValue> .EventType.Add:
                    {
                        TKey   key   = (TKey)reader.ReadObjectPacked(typeof(TKey));
                        TValue value = (TValue)reader.ReadObjectPacked(typeof(TValue));
                        m_Dictionary.Add(key, value);

                        if (OnDictionaryChanged != null)
                        {
                            OnDictionaryChanged(new NetworkDictionaryEvent <TKey, TValue>
                                {
                                    Type  = eventType,
                                    Key   = key,
                                    Value = value
                                });
                        }

                        if (keepDirtyDelta)
                        {
                            m_DirtyEvents.Add(new NetworkDictionaryEvent <TKey, TValue>()
                                {
                                    Type  = eventType,
                                    Key   = key,
                                    Value = value
                                });
                        }
                    }
                    break;

                    case NetworkDictionaryEvent <TKey, TValue> .EventType.Remove:
                    {
                        TKey   key = (TKey)reader.ReadObjectPacked(typeof(TKey));
                        TValue value;
                        m_Dictionary.TryGetValue(key, out value);
                        m_Dictionary.Remove(key);

                        if (OnDictionaryChanged != null)
                        {
                            OnDictionaryChanged(new NetworkDictionaryEvent <TKey, TValue>
                                {
                                    Type  = eventType,
                                    Key   = key,
                                    Value = value
                                });
                        }

                        if (keepDirtyDelta)
                        {
                            m_DirtyEvents.Add(new NetworkDictionaryEvent <TKey, TValue>()
                                {
                                    Type  = eventType,
                                    Key   = key,
                                    Value = value
                                });
                        }
                    }
                    break;

                    case NetworkDictionaryEvent <TKey, TValue> .EventType.RemovePair:
                    {
                        TKey   key   = (TKey)reader.ReadObjectPacked(typeof(TKey));
                        TValue value = (TValue)reader.ReadObjectPacked(typeof(TValue));
                        m_Dictionary.Remove(new KeyValuePair <TKey, TValue>(key, value));

                        if (OnDictionaryChanged != null)
                        {
                            OnDictionaryChanged(new NetworkDictionaryEvent <TKey, TValue>
                                {
                                    Type  = eventType,
                                    Key   = key,
                                    Value = value
                                });
                        }

                        if (keepDirtyDelta)
                        {
                            m_DirtyEvents.Add(new NetworkDictionaryEvent <TKey, TValue>()
                                {
                                    Type  = eventType,
                                    Key   = key,
                                    Value = value
                                });
                        }
                    }
                    break;

                    case NetworkDictionaryEvent <TKey, TValue> .EventType.Clear:
                    {
                        //read nothing
                        m_Dictionary.Clear();

                        if (OnDictionaryChanged != null)
                        {
                            OnDictionaryChanged(new NetworkDictionaryEvent <TKey, TValue>
                                {
                                    Type = eventType
                                });
                        }

                        if (keepDirtyDelta)
                        {
                            m_DirtyEvents.Add(new NetworkDictionaryEvent <TKey, TValue>
                                {
                                    Type = eventType
                                });
                        }
                    }
                    break;

                    case NetworkDictionaryEvent <TKey, TValue> .EventType.Value:
                    {
                        TKey   key   = (TKey)reader.ReadObjectPacked(typeof(TKey));
                        TValue value = (TValue)reader.ReadObjectPacked(typeof(TValue));

                        m_Dictionary[key] = value;

                        if (OnDictionaryChanged != null)
                        {
                            OnDictionaryChanged(new NetworkDictionaryEvent <TKey, TValue>
                                {
                                    Type  = eventType,
                                    Key   = key,
                                    Value = value
                                });
                        }

                        if (keepDirtyDelta)
                        {
                            m_DirtyEvents.Add(new NetworkDictionaryEvent <TKey, TValue>()
                                {
                                    Type  = eventType,
                                    Key   = key,
                                    Value = value
                                });
                        }
                    }
                    break;
                    }
                }
            }
        }