SequenceSelectorNameFormater.cs 888 B

1234567891011121314151617181920212223242526272829303132
  1. using System;
  2. using System.Collections.Generic;
  3. namespace UnityEditor.Timeline
  4. {
  5. // Class used for uniquely format names used in the GenericMenu. We can't add duplicate MenuItem in GenericMenu
  6. // so that's why we need to keep information about the text we want to uniquely format.
  7. class SequenceMenuNameFormater
  8. {
  9. Dictionary<int, int> m_UniqueItem = new Dictionary<int, int>();
  10. public string Format(string text)
  11. {
  12. var key = text.GetHashCode();
  13. var index = 0;
  14. if (m_UniqueItem.ContainsKey(key))
  15. {
  16. index = m_UniqueItem[key];
  17. index++;
  18. m_UniqueItem[key] = index;
  19. }
  20. else
  21. {
  22. m_UniqueItem.Add(key, index);
  23. return text;
  24. }
  25. return $"{text}{index}";
  26. }
  27. }
  28. }