1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?