Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JsonRef.cs 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace UnityEditor.ShaderGraph.Serialization
  5. {
  6. [Serializable]
  7. struct JsonRef<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. }
  18. public void OnAfterDeserialize()
  19. {
  20. if (MultiJsonInternal.isDeserializing)
  21. {
  22. try
  23. {
  24. if (MultiJsonInternal.valueMap.TryGetValue(m_Id, out var value))
  25. {
  26. m_Value = value.CastTo<T>();
  27. m_Id = m_Value.objectId;
  28. }
  29. else
  30. {
  31. m_Value = null;
  32. m_Id = null;
  33. }
  34. }
  35. catch (Exception e)
  36. {
  37. Debug.LogException(e);
  38. }
  39. }
  40. }
  41. public static implicit operator T(JsonRef<T> jsonRef)
  42. {
  43. return jsonRef.m_Value;
  44. }
  45. public static implicit operator JsonRef<T>(T value)
  46. {
  47. return new JsonRef<T> { m_Value = value, m_Id = value?.objectId };
  48. }
  49. public bool Equals(JsonRef<T> other)
  50. {
  51. return EqualityComparer<T>.Default.Equals(m_Value, other.m_Value);
  52. }
  53. public bool Equals(T other)
  54. {
  55. return EqualityComparer<T>.Default.Equals(m_Value, other);
  56. }
  57. public override bool Equals(object obj)
  58. {
  59. return obj is JsonRef<T> other && Equals(other) || obj is T otherValue && Equals(otherValue);
  60. }
  61. public override int GetHashCode()
  62. {
  63. return EqualityComparer<T>.Default.GetHashCode(m_Value);
  64. }
  65. public static bool operator ==(JsonRef<T> left, JsonRef<T> right)
  66. {
  67. return left.value == right.value;
  68. }
  69. public static bool operator !=(JsonRef<T> left, JsonRef<T> right)
  70. {
  71. return left.value != right.value;
  72. }
  73. public static bool operator ==(JsonRef<T> left, T right)
  74. {
  75. return left.value == right;
  76. }
  77. public static bool operator !=(JsonRef<T> left, T right)
  78. {
  79. return left.value != right;
  80. }
  81. public static bool operator ==(T left, JsonRef<T> right)
  82. {
  83. return left == right.value;
  84. }
  85. public static bool operator !=(T left, JsonRef<T> right)
  86. {
  87. return left != right.value;
  88. }
  89. }
  90. }