Version: Unity 6.6 Alpha (6000.6)
LanguageEnglish
  • C#

SerializedProperty.GetDictionaryDuplicateEntryIndices

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public int[] GetDictionaryDuplicateEntryIndices();

Returns

int[] An array of zero-based indices for the duplicate entries in the dictionary, or an empty array if the property is not a serialized dictionary or has no duplicates.

Description

Returns the indices of duplicate entries in the serialized dictionary that this property refers to.

A serialized dictionary can hold duplicate keys temporarily so that a user can edit and resolve them in the Inspector. The runtime Dictionary enforces unique keys: when the same key appears more than once in the serialized data, Unity keeps the first occurrence and reports the rest as duplicates. The returned indices are positions in the dictionary's underlying serialized array of key/value pairs, in insertion order, and stay stable across Inspector re-sorts because they reference the serialized array rather than the displayed row order. Use this method from a custom inspector or other Editor tooling to highlight, skip, or otherwise handle the duplicate rows. The array is empty when the property is not a serialized dictionary or when there are no duplicate entries.

This API is not supported when the underlying SerializedObject represents multiple selected targets, and throws an InvalidOperationException in that case. Multi-object editing of dictionaries is not supported.

Additional resources: DictionaryHeaderAttribute, SerializeField, SerializedObject.

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[CreateAssetMenu(fileName = "GameStats", menuName = "GameStats")] internal class GameStats : ScriptableObject { [DictionaryHeader(keyColumnLabel = "Name", valueColumnLabel = "Score", keyColumnFraction = 0.6f)] [SerializeField] private Dictionary<string, int> playerScores = new();

// Right-click the GameStats asset's Inspector header and choose "Log duplicates" // from the 3-dot menu to log any duplicate-key rows in playerScores. [ContextMenu("Log duplicates")] private void LogDuplicates() { using (var serializedObject = new SerializedObject(this)) { var dictionaryProperty = serializedObject.FindProperty(nameof(playerScores)); int[] duplicateIndices = dictionaryProperty.GetDictionaryDuplicateEntryIndices(); if (duplicateIndices.Length == 0) Debug.Log($"No duplicate entries in '{nameof(playerScores)}'."); else Debug.Log($"Duplicate entry indices in '{nameof(playerScores)}': {string.Join(", ", duplicateIndices)}"); } } }