How to change one slider if changing other in custom inspector?
I have:
[Range(0, 100)]
public int fItem = 100;
[Range(0, 100)]
public int sItem = 0;
I want to make: if i change the fItem (Im move slider), Editor changing sItem (editor move other slider himself). Like: fItem = 100, Editor makes: sItem = 0. Or: fItem = 70. Editor makes: sItem = 30.
You can implement an OnValidate()
method and keep previous values for each, see which one moved.
If fItem
moved, then adjust sItem
and vice versa.
This is for editor time only, obviously.
Whoa, that worked better than I imagined… I was curious to try it:
using UnityEngine;
public class TwoSliders : MonoBehaviour
{
const int max = 100;
[Range(0, max)]
public int fItem = max;
[Range(0, max)]
public int sItem = 0;
int prevfItem, prevsItem;
void OnValidate()
{
if (fItem != prevfItem)
{
prevfItem = fItem;
sItem = max - fItem;
}
if (sItem != prevsItem)
{
prevsItem = sItem;
fItem = max - sItem;
}
}
}