是否有可能使 RactiveProperty public 在 UniRx 中获得私有集
Is it possible to make RactiveProperty public get private set in UniRx
我有两个 class。其中之一具有 BoolReactiveProperty。我希望第二个 class 只能订阅而不能更改值。这是我当前的代码
public class SourceToggle : MonoBehaviour
{
public BoolReactiveProperty boolRP { get; private set; }
= new BoolReactiveProperty();
private void Start()
{
boolRP.Subscribe(b => GetComponent<Toggle>().isOn = b);
GetComponent<Toggle>()
.OnValueChangedAsObservable()
.Subscribe(b => boolRP.SetValueAndForceNotify(b));
}
}
public class SubscribedToggle : MonoBehaviour
{
[SerializeField]
private SourceToggle sourceToggle;
private void Start()
{
sourceToggle.boolRP
.Subscribe(b => GetComponent<Toggle>().isOn = b);
GetComponent<Toggle>()
.OnValueChangedAsObservable()
.Subscribe(b => sourceToggle.boolRP.SetValueAndForceNotify(b));
}
}
我不完全明白你想阻止什么。我想你想阻止你的 SubscribedToggle
在你的 ReactiveProperty 上调用 SetValueAndForceNotify
?
然后在您的 SourceToggle
class.
中声明一个 public IReadOnlyReactiveProperty
并管理一个私有 BoolReactiveProperty
public class SourceToggle : MonoBehaviour
{
private BoolReactiveProperty isOn;
private Toggle toggle;
public IReadOnlyReactiveProperty<bool> IsOn => isOn;
private void Awake()
{
toggle = GetComponent<Toggle>();
isOn = new BoolReactiveProperty(toggle.isOn);
toggle
.OnValueChangedAsObservable()
.Subscribe(b => isOn.SetValueAndForceNotify(b));
isOn.Subscribe(b => toggle.isOn = b);
}
}
public class SubscribedToggle : MonoBehaviour
{
[SerializeField]
private SourceToggle sourceToggle;
private void Start()
{
sourceToggle.IsOn
.Subscribe(b => GetComponent<Toggle>().isOn = b);
// GetComponent<Toggle>()
// .OnValueChangedAsObservable()
// .Subscribe(b => sourceToggle.IsOn.SetValueAndForceNotify(b)); // Impossible to call SetValueAndForceNotify
}
}
感谢@Hellium,我找到了解决方案
public class SourceToggle : MonoBehaviour
{
public ReadOnlyReactiveProperty<bool> IsOn { get; private set; }
private void Awake()
{
Toggle toggle = GetComponent<Toggle>();
IsOn = new ReadOnlyReactiveProperty<bool>(toggle.OnValueChangedAsObservable());
}
}
public class SubscribedToggle : MonoBehaviour
{
[SerializeField]
private SourceToggle sourceToggle;
private void Start()
{
Toggle toggle = GetComponent<Toggle>();
sourceToggle.IsOn
.Subscribe(b => toggle.isOn = b);
}
}
我有两个 class。其中之一具有 BoolReactiveProperty。我希望第二个 class 只能订阅而不能更改值。这是我当前的代码
public class SourceToggle : MonoBehaviour
{
public BoolReactiveProperty boolRP { get; private set; }
= new BoolReactiveProperty();
private void Start()
{
boolRP.Subscribe(b => GetComponent<Toggle>().isOn = b);
GetComponent<Toggle>()
.OnValueChangedAsObservable()
.Subscribe(b => boolRP.SetValueAndForceNotify(b));
}
}
public class SubscribedToggle : MonoBehaviour
{
[SerializeField]
private SourceToggle sourceToggle;
private void Start()
{
sourceToggle.boolRP
.Subscribe(b => GetComponent<Toggle>().isOn = b);
GetComponent<Toggle>()
.OnValueChangedAsObservable()
.Subscribe(b => sourceToggle.boolRP.SetValueAndForceNotify(b));
}
}
我不完全明白你想阻止什么。我想你想阻止你的 SubscribedToggle
在你的 ReactiveProperty 上调用 SetValueAndForceNotify
?
然后在您的 SourceToggle
class.
IReadOnlyReactiveProperty
并管理一个私有 BoolReactiveProperty
public class SourceToggle : MonoBehaviour
{
private BoolReactiveProperty isOn;
private Toggle toggle;
public IReadOnlyReactiveProperty<bool> IsOn => isOn;
private void Awake()
{
toggle = GetComponent<Toggle>();
isOn = new BoolReactiveProperty(toggle.isOn);
toggle
.OnValueChangedAsObservable()
.Subscribe(b => isOn.SetValueAndForceNotify(b));
isOn.Subscribe(b => toggle.isOn = b);
}
}
public class SubscribedToggle : MonoBehaviour
{
[SerializeField]
private SourceToggle sourceToggle;
private void Start()
{
sourceToggle.IsOn
.Subscribe(b => GetComponent<Toggle>().isOn = b);
// GetComponent<Toggle>()
// .OnValueChangedAsObservable()
// .Subscribe(b => sourceToggle.IsOn.SetValueAndForceNotify(b)); // Impossible to call SetValueAndForceNotify
}
}
感谢@Hellium,我找到了解决方案
public class SourceToggle : MonoBehaviour
{
public ReadOnlyReactiveProperty<bool> IsOn { get; private set; }
private void Awake()
{
Toggle toggle = GetComponent<Toggle>();
IsOn = new ReadOnlyReactiveProperty<bool>(toggle.OnValueChangedAsObservable());
}
}
public class SubscribedToggle : MonoBehaviour
{
[SerializeField]
private SourceToggle sourceToggle;
private void Start()
{
Toggle toggle = GetComponent<Toggle>();
sourceToggle.IsOn
.Subscribe(b => toggle.isOn = b);
}
}