ゲーム開発者向けのリアクティブプログラミング:はじめに

こんにちは、Habr。「UnityGameDeveloper。Professional」コースの将来の学生を招待してシューティングゲームの敵の高度な人工知能」というトピックに関する公開ウェビナーに参加してください



興味深い記事の翻訳も提供しています。










ゲーム開発者で、リアクティブプログラミングについてまだ聞いたことがない場合は、ビジネスをやめてこの記事を読んでください。冗談ではありません。





子猫に気を取られてはいけません。リアクティブプログラミングについて読んでください!





だから私はあなたの注意を引いたのですか?





優秀な!彼をなくさないように頑張ります。





リアクティブプログラミングとは何ですか?

リアクティブプログラミングとは何か、それがどのように発生したのか、そしてなぜそれが私たち一人一人にとって非常に重要なのかについての高レベルの説明で、あなたを邪魔するつもりはありません。もっと実用的なものから始めます。





, Unity, — . , — . , .





?





, Player!





, ?





Rocket!





, ?





. Level.





? , , . , , .





Glue Code ( ) — …





, , , . , , , (UI). ?





, ? , Player (label) UI UI , ?





. , ?





? Level UI , .





, , .





, Player, ?





, ? , , , .





, … …









!

, . , .





, Unity C# . , . .





: , - , , .





, C# — . , , . , , , .





— , Unity C# (Reactive Extensions) Unity (UniRx).





!





. !





Player



, :





 using UnityEngine;
 using UnityEngine.UI;
 
 public class Player : MonoBehaviour {
   public int health = 100;
   public Text scoreLabel;
 
   public void ApplyDamage(int damage) {
     health -= damage;
     scoreLabel.text = health.ToString();
   }
 }
      
      



- , , «» (glue) , (  “sticky”).





using UnityEngine;
 using UnityEngine.UI;
 
 public class Player : MonoBehaviour {
   public int health = 100;
   public Text scoreLabel;
 
   public void ApplyDamage(int damage) {
     health -= damage;
     scoreLabel.text = health.ToString();
   }
 }
      
      



! .





, :





 using UnityEngine;
 using UnityEngine.UI;
 using UniRx;
 
 public class Player : MonoBehaviour {
   public ReactiveProperty<int> health 
     = new ReactiveProperty<int>(100);
 
   public void ApplyDamage(int damage) {
     health.Value -= damage;
   }
 }
      
      



? - . ReactiveProperty



.





, , , ( , ). , , , .





ReactiveProperty

, UiPresenter



:





using UnityEngine;
 using UnityEngine.UI;
 using UniRx;
 
 public class UiPresenter : MonoBehaviour {
   public Player player;
 
   public Text scoreLabel;
 
   void Start() {
     player.health.SubscribeToText(scoreLabel);
   }
 }
      
      



scoreLabel



. -, ?





, .





, , - , , ? ?





! , , , . , .





, , , ( ). ReactiveProperty



. , , ( , — , ?).





? ReactiveProperty? , .





ReactiveProperty — Observable



( “), , Reactive Properties



. Observable



.





これは驚くべきことのように見えますが、残念ながら、そのような価値の流れについては話していません。
, , ,

— , ? , , , - .





, , , - , :





? !





, ?





, , Observable



!





6 Observable



!





, , Observable



, . , , — -Observable



«» (emit) . . X :





, observable



— . ?





, , observable



, , .





: , , . — — , . - :





, , , :





all_balls_in_order = Observable.Merge(h1, h2, h3, h4, h5, h6);
      
      



, . . , - . , , , . .





: .





!

, . , .





. , , , , .





all_balls_delayed = Observable.Merge(
  h1.Delay(
    TimeSpan.FromSeconds(1)
  ), 
  h2.Delay(
    TimeSpan.FromSeconds(2)
  ), 
  h3.Delay(
    TimeSpan.FromSeconds(3)
  ), 
  h4.Delay(
    TimeSpan.FromSeconds(3)
  ), 
  h5.Delay(
    TimeSpan.FromSeconds(2)
  ), 
  h6.Delay(
    TimeSpan.FromSeconds(1)
  )
);
      
      



, ?





?





rxmarbles.com , .





UI

, UI, - ? !





. , observable



, , . , UiPresenter



:





using UnityEngine;
 using UnityEngine.UI;
 using UniRx;
 
 public class UiPresenter : MonoBehaviour {
   public Player player;
 
   public Text scoreLabel;
 
   void Start() {
     player.health
       .Select(health => string.Format("Health: {0}", health))
       .Subscribe(text => scoreLabel.text = text);
   }
 }
      
      



Select



( rxmarbles



map



). . ( ) .





, , — . , , ?





Subscribe



, . , . SubscribeToText



, , — , .





, , . :





using UnityEngine;
 using UnityEngine.UI;
 using UniRx;
 
 public class UiPresenter : MonoBehaviour {
   public Player player;
 
   public Text scoreLabel;
   public Animator animator;
 
   void Start() {
     player.health
       .Select(health => string.Format("Health: {0}", health))
       .Do(x => animator.Play("Flash"))
       .Subscribe(text => scoreLabel.text = text);
   }
 }
      
      



, ? , , , Do



, .





, , — () :





using UnityEngine;
 using UnityEngine.UI;
 using UniRx;
 
 public class UiPresenter : MonoBehaviour {
   public ReactiveProperty<Player> player 
     = new ReactiveProperty<Player>();
 
   public Text scoreLabel;
   public Animator animator;
 
   void Start() {
     var playerHealth = this.player
       .Where(player => player != null)
       .Select(player => player.health);
 
     playerHealth
       .Select(health => string.Format("Health: {0}", health))
       .Do(x => animator.Play("Flash"))
       .Subscribe(text => scoreLabel.text = text);
   }
 }
      
      



? ReactiveProperty



, . , , .





, , , .





,





, , , . , , , . Unity UniRx.





rxmarbles.com, , . , UniRx (map



, , Select



UniRx).





« , » — .





!






"Unity Game Developer. Professional".



" ".












All Articles