| | | 1 | | #nullable enable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using Hertzole.ScriptableValues.Helpers; |
| | | 5 | | using UnityEngine; |
| | | 6 | | #if SCRIPTABLE_VALUES_ADDRESSABLES |
| | | 7 | | using UnityEngine.AddressableAssets; |
| | | 8 | | using UnityEngine.ResourceManagement.AsyncOperations; |
| | | 9 | | #endif |
| | | 10 | | |
| | | 11 | | namespace Hertzole.ScriptableValues |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// The type of value reference. |
| | | 15 | | /// </summary> |
| | | 16 | | public enum ValueReferenceType |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// A value you can set in the editor. |
| | | 20 | | /// </summary> |
| | | 21 | | Constant = 0, |
| | | 22 | | /// <summary> |
| | | 23 | | /// References a <see cref="ScriptableValue{T}" />. |
| | | 24 | | /// </summary> |
| | | 25 | | Reference = 1, |
| | | 26 | | #if SCRIPTABLE_VALUES_ADDRESSABLES |
| | | 27 | | /// <summary> |
| | | 28 | | /// References a <see cref="ScriptableValue{T}" /> that is addressable. |
| | | 29 | | /// </summary> |
| | | 30 | | Addressable = 2 |
| | | 31 | | #endif |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Allows you to reference a <see cref="ScriptableValue{T}" /> or a constant value. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <typeparam name="T">The type that the value should be.</typeparam> |
| | | 38 | | [Serializable] |
| | | 39 | | #if ODIN_INSPECTOR |
| | | 40 | | [Sirenix.OdinInspector.DrawWithUnity] |
| | | 41 | | #endif |
| | | 42 | | public class ValueReference<T> |
| | | 43 | | { |
| | | 44 | | [SerializeField] |
| | 6120 | 45 | | internal T constantValue = default!; |
| | | 46 | | [SerializeField] |
| | 6120 | 47 | | internal ScriptableValue<T> referenceValue = null!; |
| | | 48 | | #if SCRIPTABLE_VALUES_ADDRESSABLES |
| | | 49 | | [SerializeField] |
| | 6120 | 50 | | internal AssetReferenceT<ScriptableValue<T>> addressableReference = null!; |
| | | 51 | | #endif |
| | | 52 | | [SerializeField] |
| | | 53 | | internal ValueReferenceType valueType; |
| | | 54 | | |
| | | 55 | | #if UNITY_EDITOR |
| | | 56 | | [NonSerialized] |
| | 6120 | 57 | | internal T oldValue = default!; |
| | | 58 | | #endif |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Get or set the current value. This can be changed at runtime. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <remarks> |
| | | 64 | | /// If <see cref="ValueType" /> is set to <see cref="ValueReferenceType.Constant" />, this will set the cons |
| | | 65 | | /// If <see cref="ValueType" /> is set to <see cref="ValueReferenceType.Reference" />, this will set the val |
| | | 66 | | /// referenced <see cref="ScriptableValue{T}" />. |
| | | 67 | | /// If <see cref="ValueType" /> is set to <see cref="ValueReferenceType.Addressable" />, this will set the v |
| | | 68 | | /// referenced <see cref="ScriptableValue{T}" /> if it is loaded. |
| | | 69 | | /// </remarks> |
| | | 70 | | public T Value |
| | | 71 | | { |
| | 50232 | 72 | | get { return GetValue(); } |
| | 17916 | 73 | | set { SetValue(value, true); } |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Get the type of value reference. |
| | | 78 | | /// </summary> |
| | | 79 | | public ValueReferenceType ValueType |
| | | 80 | | { |
| | 0 | 81 | | get { return valueType; } |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Checks if the value is an addressable value. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <remarks>This will always return <c>false</c> if the addressables package isn't installed.</remarks> |
| | | 88 | | public bool IsAddressable |
| | | 89 | | { |
| | | 90 | | get |
| | 0 | 91 | | { |
| | | 92 | | #if SCRIPTABLE_VALUES_ADDRESSABLES |
| | 0 | 93 | | return valueType == ValueReferenceType.Addressable; |
| | | 94 | | #else |
| | | 95 | | return false; |
| | | 96 | | #endif |
| | 0 | 97 | | } |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Checks if the addressable asset is loaded. |
| | | 102 | | /// </summary> |
| | | 103 | | /// <remarks>This will always return <c>true</c> if the addressables package isn't installed.</remarks> |
| | | 104 | | public bool IsAddressableLoaded |
| | | 105 | | { |
| | | 106 | | get |
| | 1980 | 107 | | { |
| | | 108 | | #if SCRIPTABLE_VALUES_ADDRESSABLES |
| | 1980 | 109 | | return AssetHandle.IsDone && AssetHandle.Result != null; |
| | | 110 | | #else |
| | | 111 | | return true; |
| | | 112 | | #endif |
| | 1980 | 113 | | } |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | // These are only used for constant values, and as a buffer for addressable assets that are not loaded yet. |
| | | 117 | | internal event ValueEventHandler<T>? OnValueChangingInternal; |
| | | 118 | | internal event ValueEventHandler<T>? OnValueChangedInternal; |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Called before the current value is set. |
| | | 122 | | /// </summary> |
| | | 123 | | public event ValueEventHandler<T> OnValueChanging |
| | | 124 | | { |
| | | 125 | | add |
| | 1524 | 126 | | { |
| | 1524 | 127 | | if (valueType == ValueReferenceType.Reference) |
| | 396 | 128 | | { |
| | 396 | 129 | | referenceValue.OnValueChanging += value; |
| | 396 | 130 | | } |
| | | 131 | | #if SCRIPTABLE_VALUES_ADDRESSABLES |
| | 1128 | 132 | | else if (valueType == ValueReferenceType.Addressable && AssetHandle.IsValid() && AssetHandle.Result != n |
| | 156 | 133 | | { |
| | 156 | 134 | | AssetHandle.Result.OnValueChanging += value; |
| | 156 | 135 | | } |
| | | 136 | | #endif |
| | | 137 | | else |
| | 972 | 138 | | { |
| | 972 | 139 | | OnValueChangingInternal += value; |
| | 972 | 140 | | } |
| | 1524 | 141 | | } |
| | | 142 | | remove |
| | 1104 | 143 | | { |
| | 1104 | 144 | | if (valueType == ValueReferenceType.Reference) |
| | 396 | 145 | | { |
| | 396 | 146 | | referenceValue.OnValueChanging -= value; |
| | 396 | 147 | | } |
| | | 148 | | #if SCRIPTABLE_VALUES_ADDRESSABLES |
| | 708 | 149 | | else if (valueType == ValueReferenceType.Addressable && AssetHandle.IsValid() && AssetHandle.Result != n |
| | 312 | 150 | | { |
| | 312 | 151 | | AssetHandle.Result.OnValueChanging -= value; |
| | 312 | 152 | | } |
| | | 153 | | #endif |
| | | 154 | | else |
| | 396 | 155 | | { |
| | 396 | 156 | | OnValueChangingInternal -= value; |
| | 396 | 157 | | } |
| | 1104 | 158 | | } |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Called after the current value is set. |
| | | 163 | | /// </summary> |
| | | 164 | | public event ValueEventHandler<T> OnValueChanged |
| | | 165 | | { |
| | | 166 | | add |
| | 1836 | 167 | | { |
| | 1836 | 168 | | if (valueType == ValueReferenceType.Reference) |
| | 552 | 169 | | { |
| | 552 | 170 | | referenceValue.OnValueChanged += value; |
| | 552 | 171 | | } |
| | | 172 | | #if SCRIPTABLE_VALUES_ADDRESSABLES |
| | 1284 | 173 | | else if (valueType == ValueReferenceType.Addressable && AssetHandle.IsValid() && AssetHandle.Result != n |
| | 156 | 174 | | { |
| | 156 | 175 | | AssetHandle.Result.OnValueChanged += value; |
| | 156 | 176 | | } |
| | | 177 | | #endif |
| | | 178 | | else |
| | 1128 | 179 | | { |
| | 1128 | 180 | | OnValueChangedInternal += value; |
| | 1128 | 181 | | } |
| | 1836 | 182 | | } |
| | | 183 | | remove |
| | 1416 | 184 | | { |
| | 1416 | 185 | | if (valueType == ValueReferenceType.Reference) |
| | 552 | 186 | | { |
| | 552 | 187 | | referenceValue.OnValueChanged -= value; |
| | 552 | 188 | | } |
| | | 189 | | #if SCRIPTABLE_VALUES_ADDRESSABLES |
| | 864 | 190 | | else if (valueType == ValueReferenceType.Addressable && AssetHandle.IsValid() && AssetHandle.Result != n |
| | 312 | 191 | | { |
| | 312 | 192 | | AssetHandle.Result.OnValueChanged -= value; |
| | 312 | 193 | | } |
| | | 194 | | #endif |
| | | 195 | | else |
| | 552 | 196 | | { |
| | 552 | 197 | | OnValueChangedInternal -= value; |
| | 552 | 198 | | } |
| | 1416 | 199 | | } |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | /// <summary> |
| | | 203 | | /// Creates a new <see cref="ValueReference{T}" /> of type <see cref="ValueReferenceType.Reference" />. |
| | | 204 | | /// </summary> |
| | 156 | 205 | | public ValueReference() |
| | 156 | 206 | | { |
| | 156 | 207 | | valueType = ValueReferenceType.Reference; |
| | 156 | 208 | | constantValue = default!; |
| | 156 | 209 | | referenceValue = null!; |
| | | 210 | | #if SCRIPTABLE_VALUES_ADDRESSABLES |
| | 156 | 211 | | addressableReference = null!; |
| | | 212 | | #endif |
| | 156 | 213 | | } |
| | | 214 | | |
| | | 215 | | /// <summary> |
| | | 216 | | /// Creates a new <see cref="ValueReference{T}" /> of type <see cref="ValueReferenceType.Constant" /> with t |
| | | 217 | | /// constant value. |
| | | 218 | | /// </summary> |
| | | 219 | | /// <param name="constantValue">The constant value.</param> |
| | 2100 | 220 | | public ValueReference(T constantValue) |
| | 2100 | 221 | | { |
| | 2100 | 222 | | valueType = ValueReferenceType.Constant; |
| | 2100 | 223 | | this.constantValue = constantValue; |
| | 2100 | 224 | | } |
| | | 225 | | |
| | | 226 | | /// <summary> |
| | | 227 | | /// Creates a new <see cref="ValueReference{T}" /> of type <see cref="ValueReferenceType.Reference" /> with |
| | | 228 | | /// reference value. |
| | | 229 | | /// </summary> |
| | | 230 | | /// <param name="referenceValue">The reference value.</param> |
| | 1740 | 231 | | public ValueReference(ScriptableValue<T> referenceValue) |
| | 1740 | 232 | | { |
| | 1740 | 233 | | valueType = ValueReferenceType.Reference; |
| | 1740 | 234 | | this.referenceValue = referenceValue; |
| | 1740 | 235 | | } |
| | | 236 | | |
| | | 237 | | #if SCRIPTABLE_VALUES_ADDRESSABLES |
| | | 238 | | /// <summary> |
| | | 239 | | /// Creates a new <see cref="ValueReference{T}" /> of type <see cref="ValueReferenceType.Addressable" /> wit |
| | | 240 | | /// addressable reference. |
| | | 241 | | /// </summary> |
| | | 242 | | /// <param name="addressableReference">The addressable reference.</param> |
| | 2124 | 243 | | public ValueReference(AssetReferenceT<ScriptableValue<T>> addressableReference) |
| | 2124 | 244 | | { |
| | 2124 | 245 | | valueType = ValueReferenceType.Addressable; |
| | 2124 | 246 | | this.addressableReference = addressableReference; |
| | 2124 | 247 | | } |
| | | 248 | | #endif |
| | | 249 | | |
| | | 250 | | private T GetValue() |
| | 16980 | 251 | | { |
| | 16980 | 252 | | switch (valueType) |
| | | 253 | | { |
| | | 254 | | case ValueReferenceType.Constant: |
| | 5184 | 255 | | return constantValue; |
| | | 256 | | case ValueReferenceType.Reference: |
| | 5136 | 257 | | return referenceValue.Value; |
| | | 258 | | #if SCRIPTABLE_VALUES_ADDRESSABLES |
| | | 259 | | case ValueReferenceType.Addressable: |
| | 6348 | 260 | | if (AssetHandle.IsValid() && AssetHandle.IsDone && AssetHandle.Result != null) |
| | 5952 | 261 | | { |
| | 5952 | 262 | | return AssetHandle.Result.Value; |
| | | 263 | | } |
| | | 264 | | |
| | 396 | 265 | | throw new InvalidOperationException( |
| | | 266 | | $"Addressable asset is not loaded yet. Make sure you've called {nameof(LoadAddressableAssetAsync |
| | | 267 | | #endif |
| | | 268 | | default: |
| | 312 | 269 | | throw new NotSupportedException($"No supported value type for {valueType}."); |
| | | 270 | | } |
| | 16272 | 271 | | } |
| | | 272 | | |
| | | 273 | | private void SetValue(in T value, in bool notify) |
| | 7176 | 274 | | { |
| | 7176 | 275 | | T previousValue = Value; |
| | 6624 | 276 | | if (EqualityHelper.Equals(previousValue, value)) |
| | 288 | 277 | | { |
| | 288 | 278 | | return; |
| | | 279 | | } |
| | | 280 | | |
| | 6336 | 281 | | switch (valueType) |
| | | 282 | | { |
| | | 283 | | case ValueReferenceType.Constant: |
| | 2178 | 284 | | SetValueConstant(value, previousValue, notify); |
| | 2178 | 285 | | break; |
| | | 286 | | case ValueReferenceType.Reference: |
| | 2178 | 287 | | SetValueReference(value, notify); |
| | 2178 | 288 | | break; |
| | | 289 | | #if SCRIPTABLE_VALUES_ADDRESSABLES |
| | | 290 | | case ValueReferenceType.Addressable: |
| | 1980 | 291 | | SetValueAddressable(value, notify); |
| | 1980 | 292 | | break; |
| | | 293 | | #endif |
| | | 294 | | default: |
| | 0 | 295 | | throw new NotSupportedException($"No supported value type for {valueType}."); |
| | | 296 | | } |
| | 6624 | 297 | | } |
| | | 298 | | |
| | | 299 | | private void SetValueConstant(in T value, in T previousValue, in bool notify) |
| | 2178 | 300 | | { |
| | 2178 | 301 | | if (notify) |
| | 1866 | 302 | | { |
| | 1866 | 303 | | OnValueChangingInternal?.Invoke(previousValue, value); |
| | 1866 | 304 | | } |
| | | 305 | | |
| | 2178 | 306 | | constantValue = value; |
| | | 307 | | |
| | 2178 | 308 | | if (notify) |
| | 1866 | 309 | | { |
| | 1866 | 310 | | OnValueChangedInternal?.Invoke(previousValue, value); |
| | 1866 | 311 | | } |
| | 2178 | 312 | | } |
| | | 313 | | |
| | | 314 | | private void SetValueReference(in T value, in bool notify) |
| | 2178 | 315 | | { |
| | 2178 | 316 | | if (notify) |
| | 1866 | 317 | | { |
| | 1866 | 318 | | referenceValue.Value = value; |
| | 1866 | 319 | | } |
| | | 320 | | else |
| | 312 | 321 | | { |
| | 312 | 322 | | referenceValue.SetValueWithoutNotify(value); |
| | 312 | 323 | | } |
| | 2178 | 324 | | } |
| | | 325 | | |
| | | 326 | | #if SCRIPTABLE_VALUES_ADDRESSABLES |
| | | 327 | | private void SetValueAddressable(in T value, in bool notify) |
| | 1980 | 328 | | { |
| | 1980 | 329 | | if (IsAddressableLoaded) |
| | 1980 | 330 | | { |
| | 1980 | 331 | | if (notify) |
| | 1602 | 332 | | { |
| | 1602 | 333 | | AssetHandle.Result.Value = value; |
| | 1602 | 334 | | } |
| | | 335 | | else |
| | 378 | 336 | | { |
| | 378 | 337 | | AssetHandle.Result.SetValueWithoutNotify(value); |
| | 378 | 338 | | } |
| | 1980 | 339 | | } |
| | | 340 | | else |
| | 0 | 341 | | { |
| | 0 | 342 | | Debug.LogWarning("Addressable asset is not loaded yet. Make sure you've called LoadAddressableAssetAsync |
| | 0 | 343 | | } |
| | 1980 | 344 | | } |
| | | 345 | | #endif |
| | | 346 | | |
| | | 347 | | /// <inheritdoc cref="ScriptableValue{T}.SetValueWithoutNotify(T)" /> |
| | | 348 | | public void SetValueWithoutNotify(T value) |
| | 1020 | 349 | | { |
| | 1020 | 350 | | SetValue(value, false); |
| | 1020 | 351 | | } |
| | | 352 | | |
| | | 353 | | #if SCRIPTABLE_VALUES_ADDRESSABLES |
| | | 354 | | private bool IsValidAddressable() |
| | 0 | 355 | | { |
| | 0 | 356 | | return valueType == ValueReferenceType.Addressable && AssetHandle.IsValid() && AssetHandle.IsDone && AssetHa |
| | 0 | 357 | | } |
| | | 358 | | #endif |
| | | 359 | | |
| | | 360 | | #if UNITY_EDITOR |
| | | 361 | | internal void SetPreviousValue() |
| | 48 | 362 | | { |
| | 48 | 363 | | oldValue = constantValue; |
| | 48 | 364 | | } |
| | | 365 | | |
| | | 366 | | internal void SetEditorValue() |
| | 48 | 367 | | { |
| | 48 | 368 | | if (!Application.isPlaying || EqualityHelper.Equals(oldValue, Value)) |
| | 36 | 369 | | { |
| | 36 | 370 | | return; |
| | | 371 | | } |
| | | 372 | | |
| | 12 | 373 | | OnValueChangingInternal?.Invoke(oldValue, Value); |
| | 12 | 374 | | OnValueChangedInternal?.Invoke(oldValue, Value); |
| | | 375 | | |
| | 12 | 376 | | oldValue = Value; |
| | 48 | 377 | | } |
| | | 378 | | #endif |
| | | 379 | | |
| | | 380 | | #if SCRIPTABLE_VALUES_ADDRESSABLES |
| | | 381 | | /// <summary> |
| | | 382 | | /// The handle to the loaded addressable asset. |
| | | 383 | | /// </summary> |
| | | 384 | | /// <remarks> |
| | | 385 | | /// This will be a default <see cref="AsyncOperationHandle{TObject}" /> if |
| | | 386 | | /// <see cref="LoadAddressableAssetAsync" /> hasn't been called yet. |
| | | 387 | | /// </remarks> |
| | 42708 | 388 | | public AsyncOperationHandle<ScriptableValue<T>> AssetHandle { get; private set; } |
| | | 389 | | |
| | | 390 | | /// <summary> |
| | | 391 | | /// Loads the addressable asset asynchronously. |
| | | 392 | | /// </summary> |
| | | 393 | | /// <param name="onLoaded">Optional callback that is called when the asset is loaded.</param> |
| | | 394 | | /// <returns>The handle to the loaded addressable asset.</returns> |
| | | 395 | | /// <exception cref="NotSupportedException"><see cref="ValueType" /> is not <see cref="ValueReferenceType.Addres |
| | | 396 | | public AsyncOperationHandle<ScriptableValue<T>> LoadAddressableAssetAsync(Action<AsyncOperationHandle<Scriptable |
| | 1572 | 397 | | { |
| | 1572 | 398 | | if (valueType != ValueReferenceType.Addressable) |
| | 0 | 399 | | { |
| | 0 | 400 | | throw new NotSupportedException("The value type is not addressable."); |
| | | 401 | | } |
| | | 402 | | |
| | 1572 | 403 | | AssetHandle = Addressables.LoadAssetAsync<ScriptableValue<T>>(addressableReference); |
| | | 404 | | |
| | 1572 | 405 | | AssetHandle.Completed += OnAssetHandleOnCompleted; |
| | | 406 | | |
| | 1572 | 407 | | return AssetHandle; |
| | | 408 | | |
| | | 409 | | void OnAssetHandleOnCompleted(AsyncOperationHandle<ScriptableValue<T>> handle) |
| | 1572 | 410 | | { |
| | 1572 | 411 | | if (handle.Status == AsyncOperationStatus.Succeeded) |
| | 1572 | 412 | | { |
| | 1572 | 413 | | referenceValue = handle.Result; |
| | 1572 | 414 | | referenceValue.OnValueChanging += OnValueChangingInternal; |
| | 1572 | 415 | | referenceValue.OnValueChanged += OnValueChangedInternal; |
| | 1572 | 416 | | } |
| | | 417 | | |
| | 1572 | 418 | | onLoaded?.Invoke(handle); |
| | | 419 | | // Clean up the event as to not have any memory leaks. |
| | 1572 | 420 | | handle.Completed -= OnAssetHandleOnCompleted; |
| | 1572 | 421 | | } |
| | 1572 | 422 | | } |
| | | 423 | | |
| | | 424 | | /// <summary> |
| | | 425 | | /// Releases the addressable asset. |
| | | 426 | | /// </summary> |
| | | 427 | | public void ReleaseAddressableAsset() |
| | 1572 | 428 | | { |
| | 1572 | 429 | | if (valueType != ValueReferenceType.Addressable) |
| | 0 | 430 | | { |
| | 0 | 431 | | return; |
| | | 432 | | } |
| | | 433 | | |
| | | 434 | | // Make sure to remove all old listeners. |
| | 1572 | 435 | | if (referenceValue != null) |
| | 1572 | 436 | | { |
| | 1572 | 437 | | referenceValue.OnValueChanging -= OnValueChangingInternal; |
| | 1572 | 438 | | referenceValue.OnValueChanged -= OnValueChangedInternal; |
| | 1572 | 439 | | } |
| | | 440 | | |
| | 1572 | 441 | | if (AssetHandle.IsValid()) |
| | 1572 | 442 | | { |
| | 1572 | 443 | | referenceValue = null!; |
| | 1572 | 444 | | Addressables.Release(AssetHandle); |
| | 1572 | 445 | | } |
| | 1572 | 446 | | } |
| | | 447 | | #endif |
| | | 448 | | } |
| | | 449 | | } |