Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace UnityEditor.ShaderGraph.Serialization
  5. {
  6. [Serializable]
  7. struct JsonData<T> : ISerializationCallbackReceiver
  8. where T : JsonObject
  9. {
  10. [NonSerialized]
  11. T m_Value;
  12. [SerializeField]
  13. string m_Id;
  14. public T value => m_Value;
  15. public void OnBeforeSerialize()
  16. {
  17. if (MultiJsonInternal.isSerializing && m_Value != null && MultiJsonInternal.serializedSet.Add(m_Id))
  18. {
  19. MultiJsonInternal.serializationQueue.Add(m_Value);
  20. }
  21. }
  22. public void OnAfterDeserialize()
  23. {
  24. if (MultiJsonInternal.isDeserializing)
  25. {
  26. try
  27. {
  28. if (MultiJsonInternal.valueMap.TryGetValue(m_Id, out var value))
  29. {
  30. m_Value = value.CastTo<T>();
  31. // cast may fail for unknown types, but we can still grab the id from the original UnknownType
  32. m_Id = value.objectId;
  33. }
  34. else
  35. {
  36. }
  37. }
  38. catch (Exception e)
  39. {
  40. // TODO: Allow custom logging function
  41. Debug.LogException(e);
  42. }
  43. }
  44. }
  45. public static implicit operator T(JsonData<T> jsonRef)
  46. {
  47. return jsonRef.m_Value;
  48. }
  49. public static implicit operator JsonData<T>(T value)
  50. {
  51. return new JsonData<T> { m_Value = value, m_Id = value.objectId };
  52. }
  53. public bool Equals(JsonData<T> other)
  54. {
  55. return EqualityComparer<T>.Default.Equals(m_Value, other.m_Value);
  56. }
  57. public bool Equals(T other)
  58. {
  59. return EqualityComparer<T>.Default.Equals(m_Value, other);
  60. }
  61. public override bool Equals(object obj)
  62. {
  63. return obj is JsonData<T> other && Equals(other) || obj is T otherValue && Equals(otherValue);
  64. }
  65. public override int GetHashCode()
  66. {
  67. return EqualityComparer<T>.Default.GetHashCode(m_Value);
  68. }
  69. public static bool operator ==(JsonData<T> left, JsonData<T> right)
  70. {
  71. return left.value == right.value;
  72. }
  73. public static bool operator !=(JsonData<T> left, JsonData<T> right)
  74. {
  75. return left.value != right.value;
  76. }
  77. public static bool operator ==(JsonData<T> left, T right)
  78. {
  79. return left.value == right;
  80. }
  81. public static bool operator !=(JsonData<T> left, T right)
  82. {
  83. return left.value != right;
  84. }
  85. public static bool operator ==(T left, JsonData<T> right)
  86. {
  87. return left == right.value;
  88. }
  89. public static bool operator !=(T left, JsonData<T> right)
  90. {
  91. return left != right.value;
  92. }
  93. public static bool operator ==(JsonData<T> left, JsonRef<T> right)
  94. {
  95. return left.value == right.value;
  96. }
  97. public static bool operator !=(JsonData<T> left, JsonRef<T> right)
  98. {
  99. return left.value != right.value;
  100. }
  101. public static bool operator ==(JsonRef<T> left, JsonData<T> right)
  102. {
  103. return left.value == right.value;
  104. }
  105. public static bool operator !=(JsonRef<T> left, JsonData<T> right)
  106. {
  107. return left.value != right.value;
  108. }
  109. }
  110. }