Pro_Unit

Untitled

Mar 24th, 2021
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1. using System;
  2. using MalbersAnimations.Scriptables;
  3. using UniRx;
  4. using UnityEngine;
  5.  
  6. namespace AnimalSimulator.PaintMode.UI
  7. {
  8.     [Serializable]
  9.     public class RangedSetting : IDisposable
  10.     {
  11.         [SerializeField] private FloatVar _value;
  12.         [SerializeField] private float _min = 0.01f;
  13.         [SerializeField] private float _max = 1.0f;
  14.         public float Value => LerpValue(_value);
  15.         public static implicit operator float(RangedSetting setting) =>
  16.             setting.Value;
  17.  
  18.         private IDisposable _disposable;
  19.  
  20.         public void Construct(Action<float> onValueChanged)
  21.         {
  22.             _disposable = Observable
  23.                 .FromEvent<float>(
  24.                     h => _value.OnValueChanged += h,
  25.                     h => _value.OnValueChanged -= h)
  26.                 .Subscribe(value => onValueChanged(LerpValue(value)));
  27.         }
  28.         private float LerpValue(float value) =>
  29.             Mathf.Lerp(_min, _max, value);
  30.         public void Dispose() =>
  31.             _disposable?.Dispose();
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment