UniRx学习之路(二)——UI应用实践

1.Subject使用技巧

 1 public Button btn0;
 2 public Button btn1;
 3 public Button btn2;
 4 public Button btn3;
 5 public Button btn4;
 6 private Subject<int> _currentBtn = new Subject<int>();
 7 
 8 void InitializeItem(int index){
 9     OnShowItem(index);  
10 }
11 
12 void OnShowItem(int index){
13     _currentBtn.OnNext(index);
14 }
15 
16 public IObservable<int> ShowInfoItem(){
17   return _currentBtn.onClickAsObservable();        
18 }
1 private InfoListView view = new InfoListView();
2 
3 public void Initialize(){
4     view.ShowInfoItem().Subscribe(onNext: x=>{
5         print(x);
6     }
7 
8 }

 Subject同时充当了Observer和Observable的角色

2.将数据绑定到Text显示

 1 public Text text;
 2 public Slider slider;
 3 public Button btn;
 4 public ScrollRect scroll;
 5 
 6 void Start(){
 7   slider.OnValueChangedAsObservable().Where(x => x > 0.5f).
 8       SubscribeToText(ReturnText(),x =>((int)(slider.value * 100))
 9       .ToString());
10       
11   btn.OnClickAsObservable().SubscribeToText(ReturnText(),
12       x => btn.gameObject.transform.GetChild(0).
13       GetComponent<Text>().text);
14     
15   scroll.OnScrollAsObservable().SubscribeToText(ReturnText(),
16       x  => x.ToString());
17 }    
18 
19 Text ReturnText(){
20   return text;  
21 }

 3.Interactable相互影响作用

1 public Toggle toggle;
2 public Button btn;
3 
4 void Start(){
5   toggle.OnValueChangedAsObservable().SubscribeToInteractable(btn);  
6 }

 

 toggle是一个勾选选项,当toggle被勾选时,btn可以使用;当toggle不被勾选时,btn不起作用

 

posted on 2017-02-03 17:20  Fantasia丶叶子  阅读(1401)  评论(0编辑  收藏  举报

导航