-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
There is an already better solution inside Unity URP package - try to serach inside the pachage itself #19
Comments
Did they really only implement this into the URP package instead of making it a separate package? 😒 In the meantime I've quickly turned azix's plugin into a UPM package myself which can be imported via |
SerializedDictionary in UnityEngine.Rendering Code: using System;
using System.Collections.Generic;
namespace UnityEngine.Rendering
{
//
// Unity can't serialize Dictionary so here's a custom wrapper that does. Note that you have to
// extend it before it can be serialized as Unity won't serialized generic-based types either.
//
// Example:
// public sealed class MyDictionary : SerializedDictionary<KeyType, ValueType> {}
//
/// <summary>
/// Serialized Dictionary
/// </summary>
/// <typeparam name="K">Key Type</typeparam>
/// <typeparam name="V">Value Type</typeparam>
[Serializable]
public class SerializedDictionary<K, V> : Dictionary<K, V>, ISerializationCallbackReceiver
{
[SerializeField]
List<K> m_Keys = new List<K>();
[SerializeField]
List<V> m_Values = new List<V>();
/// <summary>
/// OnBeforeSerialize implementation.
/// </summary>
public void OnBeforeSerialize()
{
m_Keys.Clear();
m_Values.Clear();
foreach (var kvp in this)
{
m_Keys.Add(kvp.Key);
m_Values.Add(kvp.Value);
}
}
/// <summary>
/// OnAfterDeserialize implementation.
/// </summary>
public void OnAfterDeserialize()
{
for (int i = 0; i < m_Keys.Count; i++)
Add(m_Keys[i], m_Values[i]);
m_Keys.Clear();
m_Values.Clear();
}
}
} |
In which Unity version is this? The fact that this is only in "UnityEngine.Rendering" instead of just "UnityEngine" tells me that this is only meant to be used internally because the Unity Serializer still to this day cannot handle Dictionaries natively (without having to loop around by using the "ISerializationCallbackReceiver" interface) which is a complete travesty in and of itself! I've also found no CustomEditor for this implementation (at least in the documentation linked above), meaning that the Inspector will probably just display the keys and values as two Lists instead of side-by-side and without any "Add" or "Remove" buttons, making azix's implementation the better solution (which also contains a "SerializableHashSet" class). |
I don't think you're supposed to use it. I was just pointing to where the code from that one random comment can be found. It doesn't come with a custom editor, but rather with two List fields. |
Try to get the SerializedUnityDicitonary from URP package
The text was updated successfully, but these errors were encountered: