Keep setter and getter functions in MyManager
then take them and save them in RegisterVariable
:
// in MyManager
static Action<float> varSetter;
static Func<float> varGetter;
static void RegisterVariable(Action<float> setter, Func<float> getter)
{
varSetter = setter;
varGetter = getter;
}
static void SomeFunction()
{
float oldFloatValue = varGetter();
float newFloatValue = oldFloatValue + 5f;
varSetter(newFloatValue);
}
Then, when you call RegisterVariable
, pass getter and setter. I recommend using anonymous functions if you don't plan on calling this frequently.
// in other script
float foobar = 5f;
void SomeOtherFunction()
{
MyManager.RegisterVariable((f) => {foobar = f;}, ()=> {return foobar;});
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…