com.wallstop-studios.unity-helpers 2.0.0-rc03 → 2.0.0-rc05

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/CHANGELOG.md.meta +7 -7
  2. package/Editor/AnimationEventEditor.cs.meta +11 -11
  3. package/Editor.meta +8 -8
  4. package/LICENSE.md.meta +7 -7
  5. package/README.md.meta +7 -7
  6. package/Runtime/Core/Attributes/AnimationEventAttribute.cs.meta +11 -11
  7. package/Runtime/Core/Attributes/ChildComponentAttribute.cs.meta +11 -11
  8. package/Runtime/Core/Attributes/KSerializableAttribute.cs.meta +11 -11
  9. package/Runtime/Core/Attributes/NotNullAttribute.cs.meta +11 -11
  10. package/Runtime/Core/Attributes/ParentComponent.cs.meta +11 -11
  11. package/Runtime/Core/Attributes/ReadOnlyAttribute.cs.meta +11 -11
  12. package/Runtime/Core/Attributes/RelationalComponentExtensions.cs.meta +11 -11
  13. package/Runtime/Core/DataStructure/Adapters/FastVector2Int.cs.meta +11 -11
  14. package/Runtime/Core/DataStructure/Adapters/FastVector3Int.cs.meta +11 -11
  15. package/Runtime/Core/DataStructure/Adapters/KGuid.cs +31 -17
  16. package/Runtime/Core/DataStructure/Adapters/KGuid.cs.meta +11 -11
  17. package/Runtime/Core/DataStructure/Adapters/KVector2.cs.meta +11 -11
  18. package/Runtime/Core/DataStructure/Adapters.meta +8 -8
  19. package/Runtime/Core/DataStructure/Circle.cs.meta +11 -11
  20. package/Runtime/Core/DataStructure/CyclicBuffer.cs.meta +11 -11
  21. package/Runtime/Core/DataStructure/QuadTree.cs.meta +11 -11
  22. package/Runtime/Core/DataStructure/StringWrapper.cs.meta +11 -11
  23. package/Runtime/Core/DataStructure/TimedCache.cs.meta +11 -11
  24. package/Runtime/Core/DataStructure.meta +8 -8
  25. package/Runtime/Core/Extension/AnimatorExtensions.cs.meta +11 -11
  26. package/Runtime/Core/Extension/CircleExtensions.cs.meta +11 -11
  27. package/Runtime/Core/Extension/ColorExtensions.cs.meta +11 -11
  28. package/Runtime/Core/Extension/DictionaryExtensions.cs.meta +11 -11
  29. package/Runtime/Core/Extension/DirectionExtensions.cs.meta +11 -11
  30. package/Runtime/Core/Extension/HashSetExtensions.cs.meta +11 -11
  31. package/Runtime/Core/Extension/IEnumerableExtensions.cs.meta +11 -11
  32. package/Runtime/Core/Extension/IListExtensions.cs.meta +11 -11
  33. package/Runtime/Core/Extension/LoggingExtensions.cs.meta +11 -11
  34. package/Runtime/Core/Extension/RandomExtensions.cs.meta +11 -11
  35. package/Runtime/Core/Extension/StringExtensions.cs.meta +11 -11
  36. package/Runtime/Core/Extension/UnityExtensions.cs.meta +11 -11
  37. package/Runtime/Core/Extension.meta +8 -8
  38. package/Runtime/Core/Helper/Enumerables.cs.meta +11 -11
  39. package/Runtime/Core/Helper/Geometry.cs.meta +11 -11
  40. package/Runtime/Core/Helper/LifetimeHelpers.cs.meta +11 -11
  41. package/Runtime/Core/Helper/Objects.cs.meta +11 -11
  42. package/Runtime/Core/Helper/WallMath.cs.meta +11 -11
  43. package/Runtime/Core/Helper.meta +8 -8
  44. package/Runtime/Core/Math/Line.cs.meta +11 -11
  45. package/Runtime/Core/Math/Parabola.cs.meta +11 -11
  46. package/Runtime/Core/Math/Range.cs +6 -0
  47. package/Runtime/Core/Math/Range.cs.meta +11 -11
  48. package/Runtime/Core/Math.meta +8 -8
  49. package/Runtime/Core/Model/Direction.cs.meta +11 -11
  50. package/Runtime/Core/Model.meta +8 -8
  51. package/Runtime/Core/OneOf/FastOneOf.cs.meta +11 -11
  52. package/Runtime/Core/OneOf/None.cs.meta +11 -11
  53. package/Runtime/Core/OneOf.meta +8 -8
  54. package/Runtime/Core/Random/AbstractRandom.cs.meta +11 -11
  55. package/Runtime/Core/Random/DotNetRandom.cs +54 -0
  56. package/Runtime/Core/Random/DotNetRandom.cs.meta +3 -0
  57. package/Runtime/Core/Random/IRandom.cs.meta +11 -11
  58. package/Runtime/Core/Random/NativePcgRandom.cs.meta +11 -11
  59. package/Runtime/Core/Random/PcgRandom.cs +29 -20
  60. package/Runtime/Core/Random/PcgRandom.cs.meta +11 -11
  61. package/Runtime/Core/Random/RandomState.cs +13 -2
  62. package/Runtime/Core/Random/RandomState.cs.meta +11 -11
  63. package/Runtime/Core/Random/SquirrelRandom.cs +12 -7
  64. package/Runtime/Core/Random/SquirrelRandom.cs.meta +11 -11
  65. package/Runtime/Core/Random/SystemRandom.cs +15 -9
  66. package/Runtime/Core/Random/SystemRandom.cs.meta +11 -11
  67. package/Runtime/Core/Random/ThreadLocalRandom.cs.meta +11 -11
  68. package/Runtime/Core/Random/UnityRandom.cs +39 -4
  69. package/Runtime/Core/Random/WyRandom.cs +123 -0
  70. package/Runtime/Core/Random/WyRandom.cs.meta +3 -0
  71. package/Runtime/Core/Random/XorShiftRandom.cs +8 -4
  72. package/Runtime/Core/Random.meta +8 -8
  73. package/Runtime/Core/Serialization/JsonConverters/Vector2Converter.cs.meta +11 -11
  74. package/Runtime/Core/Serialization/JsonConverters/Vector3Converter.cs.meta +11 -11
  75. package/Runtime/Core/Serialization/JsonConverters.meta +8 -8
  76. package/Runtime/Core/Serialization/Serializer.cs.meta +11 -11
  77. package/Runtime/Core/Serialization.meta +8 -8
  78. package/Runtime/Core/Threading/SingleThreadedThreadPool.cs.meta +11 -11
  79. package/Runtime/Core/Threading.meta +8 -8
  80. package/Runtime/Core.meta +8 -8
  81. package/Runtime/Protobuf-Net.meta +8 -8
  82. package/Runtime/Utils/AnimationEventEqualityComparer.cs.meta +11 -11
  83. package/Runtime/Utils/AnimatorEnumStateMachine.cs +13 -4
  84. package/Runtime/Utils/AnimatorEnumStateMachine.cs.meta +11 -11
  85. package/Runtime/Utils/Buffers.cs.meta +11 -11
  86. package/Runtime/Utils/CircleLineRenderer.cs.meta +11 -11
  87. package/Runtime/Utils/Oscillator.cs.meta +11 -11
  88. package/Runtime/Utils/SetTextureImportData.cs.meta +11 -11
  89. package/Runtime/Utils.meta +8 -8
  90. package/Runtime/WallstopStudios.UnityHelpers.asmdef.meta +7 -7
  91. package/Runtime.meta +8 -8
  92. package/Tests/Runtime/Performance/RandomPerformanceTests.cs +18 -13
  93. package/Tests/Runtime/Random/DotNetRandomTests.cs +9 -0
  94. package/Tests/Runtime/Random/DotNetRandomTests.cs.meta +3 -0
  95. package/Tests/Runtime/Random/RandomTestBase.cs +69 -15
  96. package/Tests/Runtime/Random/WyRandomTests.cs +9 -0
  97. package/Tests/Runtime/Random/WyRandomTests.cs.meta +3 -0
  98. package/Tests.meta +8 -8
  99. package/Third Party Notices.md.meta +7 -7
  100. package/package.json +1 -1
  101. package/package.json.meta +7 -7
@@ -0,0 +1,54 @@
1
+ namespace UnityHelpers.Core.Random
2
+ {
3
+ using System;
4
+ using System.Runtime.Serialization;
5
+ using System.Text.Json.Serialization;
6
+
7
+ [Serializable]
8
+ [DataContract]
9
+ public sealed class DotNetRandom : AbstractRandom
10
+ {
11
+ [JsonPropertyName("State")]
12
+ [DataMember(Name = "State")]
13
+ public override RandomState InternalState =>
14
+ new RandomState(unchecked((ulong)_seed), state2: _numberGenerated);
15
+
16
+ private ulong _numberGenerated;
17
+ private int _seed;
18
+ private Random _random;
19
+
20
+ public DotNetRandom()
21
+ : this(Guid.NewGuid()) { }
22
+
23
+ public DotNetRandom(Guid guid)
24
+ {
25
+ _seed = guid.GetHashCode();
26
+ _random = new Random(_seed);
27
+ }
28
+
29
+ [JsonConstructor]
30
+ public DotNetRandom(RandomState state)
31
+ {
32
+ _seed = unchecked((int)state.State1);
33
+ _random = new Random(_seed);
34
+ _numberGenerated = 0;
35
+ ulong generationCount = state.State2;
36
+
37
+ while (_numberGenerated < generationCount)
38
+ {
39
+ _ = NextUint();
40
+ }
41
+ }
42
+
43
+ public override uint NextUint()
44
+ {
45
+ ++_numberGenerated;
46
+ return unchecked((uint)_random.Next(int.MinValue, int.MaxValue));
47
+ }
48
+
49
+ public override IRandom Copy()
50
+ {
51
+ return new DotNetRandom(InternalState);
52
+ }
53
+ }
54
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: a01f07b12a4e4da0b9d41986ff6a6c43
3
+ timeCreated: 1732481642
@@ -1,11 +1,11 @@
1
- fileFormatVersion: 2
2
- guid: ddc39031f4659de4bbdd61deab3d299a
3
- MonoImporter:
4
- externalObjects: {}
5
- serializedVersion: 2
6
- defaultReferences: []
7
- executionOrder: 0
8
- icon: {instanceID: 0}
9
- userData:
10
- assetBundleName:
11
- assetBundleVariant:
1
+ fileFormatVersion: 2
2
+ guid: ddc39031f4659de4bbdd61deab3d299a
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
@@ -1,11 +1,11 @@
1
- fileFormatVersion: 2
2
- guid: 738be60c0f0a5ad428b38b79c266f408
3
- MonoImporter:
4
- externalObjects: {}
5
- serializedVersion: 2
6
- defaultReferences: []
7
- executionOrder: 0
8
- icon: {instanceID: 0}
9
- userData:
10
- assetBundleName:
11
- assetBundleVariant:
1
+ fileFormatVersion: 2
2
+ guid: 738be60c0f0a5ad428b38b79c266f408
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
@@ -2,24 +2,29 @@
2
2
  {
3
3
  using System;
4
4
  using System.Runtime.Serialization;
5
+ using System.Text.Json.Serialization;
5
6
 
6
7
  /// <summary>
7
8
  /// Implementation based off of the reference PCG Random, found here: https://www.pcg-random.org/index.html
8
9
  /// </summary>
9
10
  [Serializable]
10
11
  [DataContract]
11
- public sealed class PcgRandom : AbstractRandom, IEquatable<PcgRandom>, IComparable, IComparable<PcgRandom>
12
+ public sealed class PcgRandom
13
+ : AbstractRandom,
14
+ IEquatable<PcgRandom>,
15
+ IComparable,
16
+ IComparable<PcgRandom>
12
17
  {
18
+ [DataMember(Name = "State")]
19
+ [JsonPropertyName("State")]
13
20
  public static IRandom Instance => ThreadLocalRandom<PcgRandom>.Instance;
14
21
 
15
- [DataMember(Name = "Increment")]
16
22
  internal readonly ulong _increment;
17
- [DataMember(Name = "State")]
23
+
18
24
  internal ulong _state;
19
25
 
20
- public PcgRandom() : this(Guid.NewGuid())
21
- {
22
- }
26
+ public PcgRandom()
27
+ : this(Guid.NewGuid()) { }
23
28
 
24
29
  public PcgRandom(Guid guid)
25
30
  {
@@ -28,11 +33,12 @@
28
33
  _increment = BitConverter.ToUInt64(guidArray, sizeof(ulong));
29
34
  }
30
35
 
31
- public PcgRandom(RandomState randomState)
36
+ [JsonConstructor]
37
+ public PcgRandom(RandomState state)
32
38
  {
33
- _state = randomState.State1;
34
- _increment = randomState.State2;
35
- _cachedGaussian = randomState.Gaussian;
39
+ _state = state.State1;
40
+ _increment = state.State2;
41
+ _cachedGaussian = state.Gaussian;
36
42
  }
37
43
 
38
44
  public PcgRandom(ulong increment, ulong state)
@@ -45,11 +51,11 @@
45
51
  {
46
52
  // Start with a nice prime
47
53
  _increment = 6554638469UL;
48
- _state = unchecked((ulong) seed);
54
+ _state = unchecked((ulong)seed);
49
55
  _increment = NextUlong();
50
56
  }
51
57
 
52
- public override RandomState InternalState => new (_state, _increment, _cachedGaussian);
58
+ public override RandomState InternalState => new(_state, _increment, _cachedGaussian);
53
59
 
54
60
  public override uint NextUint()
55
61
  {
@@ -71,7 +77,14 @@
71
77
  }
72
78
 
73
79
  // ReSharper disable once CompareOfFloatsByEqualityOperator
74
- return _increment == other._increment && _state == other._state && _cachedGaussian == other._cachedGaussian;
80
+ return _increment == other._increment
81
+ && _state == other._state
82
+ && _cachedGaussian == other._cachedGaussian;
83
+ }
84
+
85
+ public override bool Equals(object obj)
86
+ {
87
+ return Equals(obj as PcgRandom);
75
88
  }
76
89
 
77
90
  public int CompareTo(PcgRandom other)
@@ -87,6 +100,7 @@
87
100
  {
88
101
  return 0;
89
102
  }
103
+
90
104
  if (_state < other._state)
91
105
  {
92
106
  return -1;
@@ -114,9 +128,9 @@
114
128
  return _cachedGaussian.Value.CompareTo(other._cachedGaussian.Value);
115
129
  }
116
130
 
117
- public override bool Equals(object other)
131
+ public int CompareTo(object obj)
118
132
  {
119
- return Equals(other as PcgRandom);
133
+ return CompareTo(obj as PcgRandom);
120
134
  }
121
135
 
122
136
  public override int GetHashCode()
@@ -129,11 +143,6 @@
129
143
  return $"{{\"Increment\": {_increment}, \"State\": {_state}}}";
130
144
  }
131
145
 
132
- public int CompareTo(object other)
133
- {
134
- return CompareTo(other as PcgRandom);
135
- }
136
-
137
146
  public override IRandom Copy()
138
147
  {
139
148
  return new PcgRandom(InternalState);
@@ -1,11 +1,11 @@
1
- fileFormatVersion: 2
2
- guid: 5206bd9e6b6b7a24cb5a88faba0e4777
3
- MonoImporter:
4
- externalObjects: {}
5
- serializedVersion: 2
6
- defaultReferences: []
7
- executionOrder: 0
8
- icon: {instanceID: 0}
9
- userData:
10
- assetBundleName:
11
- assetBundleVariant:
1
+ fileFormatVersion: 2
2
+ guid: 5206bd9e6b6b7a24cb5a88faba0e4777
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
@@ -1,6 +1,7 @@
1
1
  namespace UnityHelpers.Core.Random
2
2
  {
3
3
  using System;
4
+ using System.Text.Json.Serialization;
4
5
  using Extension;
5
6
  using Helper;
6
7
  using ProtoBuf;
@@ -27,6 +28,7 @@
27
28
 
28
29
  [ProtoMember(1)]
29
30
  private ulong _state1;
31
+
30
32
  [ProtoMember(2)]
31
33
  private ulong _state2;
32
34
 
@@ -38,6 +40,7 @@
38
40
 
39
41
  private int _hashCode;
40
42
 
43
+ [JsonConstructor]
41
44
  public RandomState(ulong state1, ulong state2 = 0, double? gaussian = null)
42
45
  {
43
46
  _state1 = state1;
@@ -71,14 +74,22 @@
71
74
  public bool Equals(RandomState other)
72
75
  {
73
76
  // ReSharper disable once CompareOfFloatsByEqualityOperator
74
- return _state1 == other._state1 && _state2 == other._state2 && _hasGaussian == other._hasGaussian && (!_hasGaussian || _gaussian == other._gaussian);
77
+ return _state1 == other._state1
78
+ && _state2 == other._state2
79
+ && _hasGaussian == other._hasGaussian
80
+ && (!_hasGaussian || _gaussian == other._gaussian);
75
81
  }
76
82
 
77
83
  public override int GetHashCode()
78
84
  {
79
85
  if (_hashCode == 0)
80
86
  {
81
- return _hashCode = Objects.ValueTypeHashCode(_state1, _state2, _hasGaussian, _gaussian);
87
+ return _hashCode = Objects.ValueTypeHashCode(
88
+ _state1,
89
+ _state2,
90
+ _hasGaussian,
91
+ _gaussian
92
+ );
82
93
  }
83
94
 
84
95
  return _hashCode;
@@ -1,11 +1,11 @@
1
- fileFormatVersion: 2
2
- guid: 41d80559b2118924ba94e768d005a4e6
3
- MonoImporter:
4
- externalObjects: {}
5
- serializedVersion: 2
6
- defaultReferences: []
7
- executionOrder: 0
8
- icon: {instanceID: 0}
9
- userData:
10
- assetBundleName:
11
- assetBundleVariant:
1
+ fileFormatVersion: 2
2
+ guid: 41d80559b2118924ba94e768d005a4e6
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
@@ -1,8 +1,12 @@
1
1
  namespace UnityHelpers.Core.Random
2
2
  {
3
3
  using System;
4
+ using System.Runtime.Serialization;
5
+ using System.Text.Json.Serialization;
4
6
 
5
7
  // https://youtu.be/LWFzPP8ZbdU?t=2673
8
+ [DataContract]
9
+ [Serializable]
6
10
  public sealed class SquirrelRandom : AbstractRandom
7
11
  {
8
12
  private const uint BitNoise1 = 0xB5297A4D;
@@ -10,28 +14,29 @@
10
14
  private const uint BitNoise3 = 0x1B56C4E9;
11
15
  private const int LargePrime = 198491317;
12
16
 
13
- public static readonly SquirrelRandom Instance = new();
17
+ public static readonly SquirrelRandom Instance = ThreadLocalRandom<SquirrelRandom>.Instance;
18
+
19
+ [JsonPropertyName("State")]
20
+ [DataMember(Name = "State")]
21
+ public override RandomState InternalState => new(_position, gaussian: _cachedGaussian);
14
22
 
15
23
  private uint _position;
16
24
 
17
- public SquirrelRandom() : this(Guid.NewGuid().GetHashCode())
18
- {
19
-
20
- }
25
+ public SquirrelRandom()
26
+ : this(Guid.NewGuid().GetHashCode()) { }
21
27
 
22
28
  public SquirrelRandom(int seed)
23
29
  {
24
30
  _position = unchecked((uint)seed);
25
31
  }
26
32
 
33
+ [JsonConstructor]
27
34
  public SquirrelRandom(RandomState randomState)
28
35
  {
29
36
  _position = unchecked((uint)randomState.State1);
30
37
  _cachedGaussian = randomState.Gaussian;
31
38
  }
32
39
 
33
- public override RandomState InternalState => new(_position, gaussian: _cachedGaussian);
34
-
35
40
  public override uint NextUint()
36
41
  {
37
42
  return _position = NextUintInternal(_position);
@@ -1,11 +1,11 @@
1
- fileFormatVersion: 2
2
- guid: 3f2a41ab545b08e4b9bd2a8025bf839a
3
- MonoImporter:
4
- externalObjects: {}
5
- serializedVersion: 2
6
- defaultReferences: []
7
- executionOrder: 0
8
- icon: {instanceID: 0}
9
- userData:
10
- assetBundleName:
11
- assetBundleVariant:
1
+ fileFormatVersion: 2
2
+ guid: 3f2a41ab545b08e4b9bd2a8025bf839a
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
@@ -1,12 +1,21 @@
1
1
  namespace UnityHelpers.Core.Random
2
2
  {
3
3
  using System;
4
+ using System.Runtime.Serialization;
5
+ using System.Text.Json.Serialization;
4
6
 
5
7
  /// <summary>
6
8
  /// Implementation dependent upon .Net's Random class.
7
9
  /// </summary>
10
+ [Serializable]
11
+ [DataContract]
8
12
  public sealed class SystemRandom : AbstractRandom
9
13
  {
14
+ [JsonPropertyName("State")]
15
+ [DataMember(Name = "State")]
16
+ public override RandomState InternalState =>
17
+ new(unchecked((ulong)inext), unchecked((ulong)inextp), _cachedGaussian);
18
+
10
19
  /*
11
20
  Copied from Random.cs source. Apparently it isn't guaranteed to be the
12
21
  same across platforms and we depend on that.
@@ -17,9 +26,8 @@
17
26
 
18
27
  public static IRandom Instance => ThreadLocalRandom<SystemRandom>.Instance;
19
28
 
20
- public SystemRandom() : this (Environment.TickCount)
21
- {
22
- }
29
+ public SystemRandom()
30
+ : this(Environment.TickCount) { }
23
31
 
24
32
  public SystemRandom(int seed)
25
33
  {
@@ -48,6 +56,7 @@
48
56
  this.inextp = 21;
49
57
  }
50
58
 
59
+ [JsonConstructor]
51
60
  public SystemRandom(RandomState randomState)
52
61
  {
53
62
  inext = unchecked((int)randomState.State1);
@@ -55,8 +64,6 @@
55
64
  _cachedGaussian = randomState.Gaussian;
56
65
  }
57
66
 
58
- public override RandomState InternalState => new(unchecked((ulong)inext), unchecked((ulong)inextp), _cachedGaussian);
59
-
60
67
  public override uint NextUint()
61
68
  {
62
69
  int inext = this.inext;
@@ -75,7 +82,7 @@
75
82
  this.SeedArray[index1] = num;
76
83
  this.inext = index1;
77
84
  this.inextp = index2;
78
- return unchecked((uint) num);
85
+ return unchecked((uint)num);
79
86
  }
80
87
 
81
88
  public override double NextDouble()
@@ -84,15 +91,14 @@
84
91
  do
85
92
  {
86
93
  generated = unchecked((int)NextUint()) * 4.6566128752458E-10;
87
- }
88
- while (generated < 0 || 1 <= generated);
94
+ } while (generated < 0 || 1 <= generated);
89
95
 
90
96
  return generated;
91
97
  }
92
98
 
93
99
  public override float NextFloat()
94
100
  {
95
- return (float) NextDouble();
101
+ return (float)NextDouble();
96
102
  }
97
103
 
98
104
  public override IRandom Copy()
@@ -1,11 +1,11 @@
1
- fileFormatVersion: 2
2
- guid: d00769758e05b2f478ea85ce53650d44
3
- MonoImporter:
4
- externalObjects: {}
5
- serializedVersion: 2
6
- defaultReferences: []
7
- executionOrder: 0
8
- icon: {instanceID: 0}
9
- userData:
10
- assetBundleName:
11
- assetBundleVariant:
1
+ fileFormatVersion: 2
2
+ guid: d00769758e05b2f478ea85ce53650d44
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
@@ -1,11 +1,11 @@
1
- fileFormatVersion: 2
2
- guid: 0d9f5b15009b2ff48ba3d66fd8a7fc53
3
- MonoImporter:
4
- externalObjects: {}
5
- serializedVersion: 2
6
- defaultReferences: []
7
- executionOrder: 0
8
- icon: {instanceID: 0}
9
- userData:
10
- assetBundleName:
11
- assetBundleVariant:
1
+ fileFormatVersion: 2
2
+ guid: 0d9f5b15009b2ff48ba3d66fd8a7fc53
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
@@ -1,16 +1,51 @@
1
1
  namespace UnityHelpers.Core.Random
2
2
  {
3
3
  using System;
4
+ using System.Runtime.Serialization;
5
+ using System.Text.Json.Serialization;
4
6
 
7
+ [Serializable]
8
+ [DataContract]
5
9
  public sealed class UnityRandom : AbstractRandom
6
10
  {
7
- public static readonly UnityRandom Instance = new UnityRandom();
11
+ public static readonly UnityRandom Instance = new();
8
12
 
9
- private UnityRandom()
13
+ [JsonPropertyName("State")]
14
+ [DataMember(Name = "State")]
15
+ public override RandomState InternalState
10
16
  {
17
+ get
18
+ {
19
+ unchecked
20
+ {
21
+ return new RandomState(
22
+ (ulong)(_seed ?? 0),
23
+ gaussian: _seed != null ? 0.0 : null
24
+ );
25
+ }
26
+ }
27
+ }
28
+
29
+ private readonly int? _seed;
30
+
31
+ public UnityRandom(int? seed = null)
32
+ {
33
+ if (seed != null)
34
+ {
35
+ _seed = seed.Value;
36
+ UnityEngine.Random.InitState(seed.Value);
37
+ }
38
+ }
39
+
40
+ [JsonConstructor]
41
+ public UnityRandom(RandomState state)
42
+ {
43
+ unchecked
44
+ {
45
+ _seed = state.Gaussian != null ? (int)state.State1 : null;
46
+ }
11
47
  }
12
48
 
13
- public override RandomState InternalState => throw new NotSupportedException("Unity Random does not expose its internal state");
14
49
  public override uint NextUint()
15
50
  {
16
51
  return unchecked((uint)UnityEngine.Random.Range(int.MinValue, int.MaxValue));
@@ -18,7 +53,7 @@
18
53
 
19
54
  public override IRandom Copy()
20
55
  {
21
- throw new NotSupportedException("Unity Random does not support copying / seeding");
56
+ return new UnityRandom(_seed);
22
57
  }
23
58
  }
24
59
  }