LoginSignup
1
2

UniRxでuGUIのドラッグ&ドロップの実装

Posted at

コード

色々な実装方法があったのですが,自分なりに一番簡単なものを備忘録として書き留めます

この三行だけ

trigger.OnDragAsObservable()
    .Subscribe(e => transform.position = e.pointerCurrentRaycast.worldPosition)
    .AddTo(this);

また,汎用性を上げるためにドラッグ&ドロップの中止ができるようにする

private bool isDraggable = true;

trigger.OnDragAsObservable()
    .TakeWhile(_ => isDraggable)
    .RepeatUntilDestroy(this)
    .Subscribe(e => transform.position = e.pointerCurrentRaycast.worldPosition)
    .AddTo(this);

テストとしてドラッグ開始から1秒後に停止してみる

trigger.OnBeginDragAsObservable()
    .SelectMany(_ => Observable.Timer(TimeSpan.FromSeconds(1)))
    .RepeatUntilDestroy(this)
    .Subscribe(_ => isDraggable = false)
    .AddTo(this);
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2