ReactiveProperty:反應(yīng)式屬性,即可以支持Rx編程方式的屬性語(yǔ)法。
我們一般創(chuàng)建的屬性如:

private int age;
private string name;
private float score;這些游戲?qū)傩酝ǔP枰ㄖ兓覀兛梢允褂檬录卣{(diào),但是太麻煩了,好在UniRx為我們提供了ReactiveProperty,一個(gè)輕量級(jí)的屬性代理。
示例我們先聲明一個(gè)數(shù)據(jù)模型類,它定義了一個(gè)Enemy類,里面由Hp血量,和IsDead是否死亡屬性,因?yàn)閮烧呤怯幸欢P(guān)系的,所以可以定義IsDead為ReadOnlyReactiveProperty,然后和血量 x<=0 綁定起來(lái)。
// 反應(yīng)式通知 model數(shù)據(jù)類
public class Enemy
{public IReactivePropertyCurrentHp {get; private set; }
public IReadOnlyReactivePropertyIsDead {get; private set; }
public Enemy(int initialHp)
{// 聲明屬性
CurrentHp = new ReactiveProperty(initialHp);
IsDead = CurrentHp.Select(x =>x<= 0).ToReactiveProperty();
}
} 接下來(lái)我們還可以讓UI和Model數(shù)據(jù)進(jìn)行綁定,來(lái)達(dá)到MVP或者M(jìn)VVM框架的目的
public class Sample12_ReactiveProperty : MonoBehaviour
{// Open Sample12Scene. Set from canvas
public Button MyButton;
public Toggle MyToggle;
public InputField MyInput;
public Text MyText;
public Slider MySlider;
// ReactiveProperty屬性可以序列化在Inspector面板, 我們可以通過(guò)序列化方式修改它
public IntReactiveProperty IntRxProp = new IntReactiveProperty();
//定義Enemy數(shù)據(jù)類
Enemy enemy = new Enemy(1000);
void Start()
{// UnityEvent 轉(zhuǎn) Observable
// (快捷方式, MyButton.OnClickAsObservable())
// 點(diǎn)擊按鈕Hp-=99
MyButton.onClick.AsObservable().Subscribe(_ =>enemy.CurrentHp.Value -= 99);
// Toggle, 輸出參數(shù)給Observable(OnValueChangedAsObservable可以廣播一個(gè)bool類型value
// SubscribeToInteractable 是 UniRx.UI擴(kuò)展方法,相當(dāng)于 .interactable = x)
MyToggle.OnValueChangedAsObservable().SubscribeToInteractable(MyButton);
// input 延遲1s 展示在myText上
MyInput.OnValueChangeAsObservable()
.Where(x =>x != null)
.Delay(TimeSpan.FromSeconds(1))
.SubscribeToText(MyText); // SubscribeToText 是UniRx.UI 擴(kuò)展方法
// 轉(zhuǎn)化為可視化數(shù)據(jù)
MySlider.OnValueChangedAsObservable()
.SubscribeToText(MyText, x =>Math.Round(x, 2).ToString());
// 基于RxProp的屬性 CurrentHp的變化(Button Click)是可觀察的
enemy.CurrentHp.SubscribeToText(MyText);
// 當(dāng)enemy血量<0后,廣播事件,讓myButton和MyToggle設(shè)置為不可見(jiàn)
enemy.IsDead.Where(isDead =>isDead == true)
.Subscribe(_ =>{MyToggle.interactable = MyButton.interactable = false;
});
// 用IntRxProp的值初始化 text:)
IntRxProp.SubscribeToText(MyText);
}
}總結(jié)通過(guò)這個(gè)例子,我們可以看到一些UI框架的縮影。
1.戰(zhàn)斗時(shí)角色持續(xù)掉血,且實(shí)時(shí)刷新UI的場(chǎng)景。
2.設(shè)置頁(yè)面開關(guān)控制全局音效,音量的功能。
3.某些特殊技能導(dǎo)致的掉血,延遲表現(xiàn)到人物血條上或者飄字上。
4.角色死亡引起不同界面,不同實(shí)體的不同變化。
我們慶幸,現(xiàn)在有了UniRx,我們可以省去了大量的事件回調(diào)的編寫。省去了大量的MVVM高耦合代碼邏輯。可以很輕量化的編寫一個(gè)MVP(MVRP)架構(gòu)的UI框架了。
我們直到Unity沒(méi)有提供UI綁定機(jī)制,如果使用MVVM模式,我們需要實(shí)現(xiàn)復(fù)雜的綁定層,這可能會(huì)影響性能。所以我們需要使用Presenter持有視圖并更新視圖,雖然不是真正的綁定,但是Observables啟用了對(duì)通知的訂閱,這個(gè)模式我們成為ReactivePresenter模式。
在Unity的Hierarchy中,試圖就是一個(gè)場(chǎng)景,試圖初始化時(shí)和Presenters關(guān)聯(lián),xxxAsObservable方法使得創(chuàng)建事件信號(hào)變得簡(jiǎn)單,沒(méi)有任何開銷,再搭配UniRx.UI.Extension中的一些簡(jiǎn)單工具函數(shù),可以實(shí)現(xiàn)一個(gè)高性能和簡(jiǎn)潔的體系結(jié)構(gòu)。
這些屬性均派生自InspecetableReactiveProperty,可以在Inspector面板序列化,當(dāng)值發(fā)生變化時(shí)可以通知,在Inspector中更改值時(shí)也會(huì)發(fā)出通知。
//數(shù)值型
private IntReactiveProperty a1;
private LongReactiveProperty a2;
private ByteReactiveProperty a3;
//浮點(diǎn)型
private FloatReactiveProperty b1;
private DoubleReactiveProperty b2;
//bool型
private BoolReactiveProperty c1;
//string型
private StringReactiveProperty c2;
//向量,四元數(shù)
private Vector2ReactiveProperty v1;
private Vector3ReactiveProperty v2;
private Vector4ReactiveProperty v3;
private QuaternionReactiveProperty q1;
//顏色
private ColorReactiveProperty color;
//其他
private RectReactiveProperty rect;
private BoundsReactiveProperty bound;
private AnimationCurveReactiveProperty anim;
//集合型
private ReactiveCollectionlist;
private ReactiveDictionarydict; 序列化功能由 InspectorDisplayDrawer提供,通過(guò)繼承你可以應(yīng)用你自己自定義的ReactiveProperty:
public enum Fruit
{Apple, Grape
}
[Serializable]
public class FruitReactiveProperty : ReactiveProperty{public FruitReactiveProperty()
{}
public FruitReactiveProperty(Fruit initialValue)
:base(initialValue)
{}
}
[UnityEditor.CustomPropertyDrawer(typeof(FruitReactiveProperty))]
[UnityEditor.CustomPropertyDrawer(typeof(YourSpecializedReactiveProperty2))] // and others...
public class ExtendInspectorDisplayDrawer : InspectorDisplayDrawer
{}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)頁(yè)名稱:UniRx之ReactiveProperty實(shí)現(xiàn)MVP-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://chinadenli.net/article8/diisop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站策劃、電子商務(wù)、App設(shè)計(jì)、網(wǎng)站收錄、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容