質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

1469閲覧

[Unity]縦にスワイプできるページを作りたい。

yonsan

総合スコア2

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/08/20 14:08

編集2020/08/20 14:30

前提・実現したいこと

unityで、縦にスワイプできるパネルを作成中です。
下記にあります、該当のソースコードでは、パネルを横にスワイプできるようになっております。
あらかじめページ数をpublicで宣言し、そのページ数をオーバーしたスワイプはできないようにしております。
このソースコードを、横ではなく縦スワイプできるようにしたいのですが、実現できません。
ご教授の程、お願い致します。
また、足りない情報等ありましたら、お知らせ願います。

※容量を小さくしたGif画像を、何故か本投稿へ貼り付けできなかったため、横スワイプの実行結果確認いただく際は、以下URLよりご確認願います。
https://d.kuku.lu/823499bbf5

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.EventSystems; 5 6public class PageSwiper : MonoBehaviour, IDragHandler, IEndDragHandler 7{ 8 private Vector3 panelLocation; 9 public float percentThreshold = 0.2f; 10 public float easing = 0.5f; 11 public float totalPages = 1; 12 private int currentPage = 1; 13 14 // Start is called before the first frame update 15 void Start() 16 { 17 panelLocation = transform.position; 18 } 19 public void OnDrag(PointerEventData data) 20 { 21 float difference = data.pressPosition.x - data.position.x; 22 transform.position = panelLocation - new Vector3(difference, 0, 0); 23 } 24 public void OnEndDrag(PointerEventData data) 25 { 26 float percentage = (data.pressPosition.x - data.position.x) / Screen.width; 27 if (Mathf.Abs(percentage) >= percentThreshold) 28 { 29 Vector3 newLocation = panelLocation; 30 if (percentage > 0 && currentPage < totalPages) 31 { 32 currentPage++; 33 newLocation += new Vector3(-Screen.width, 0, 0); 34 } 35 else if (percentage < 0 && currentPage > 1) 36 { 37 currentPage--; 38 newLocation += new Vector3(Screen.width, 0, 0); 39 } 40 StartCoroutine(SmoothMove(transform.position, newLocation, easing)); 41 panelLocation = newLocation; 42 } 43 else 44 { 45 StartCoroutine(SmoothMove(transform.position, panelLocation, easing)); 46 } 47 } 48 49 IEnumerator SmoothMove(Vector3 startpos, Vector3 endpos, float seconds) 50 { 51 float t = 0f; 52 while (t <= 1.0) 53 { 54 t += Time.deltaTime / seconds; 55 transform.position = Vector3.Lerp(startpos, endpos, Mathf.SmoothStep(0f, 1f, t)); 56 yield return null; 57 } 58 } 59} 60

試したこと

コード内のposition.xをyにしたり、Vector3()内の値をxとyで入れ替えたりと、色々試しましたが実現できませんでした。

補足情報(FW/ツールのバージョンなど)

環境:Unity 2020.1.2f1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

利用したコードは、以下参考サイトのものとして回答させて頂きます。
※スクリプトの使い方として認識齟齬がないか、確認のためにリンク貼りました。
Swiping Pages in Unity(YouTube)
Swiping Pages in Unity(SourceCode)

大きく以下の点を修正したら、縦に動作しませんでしょうか。
※もし不具合あったら、回答自体スルーして頂いて大丈夫です。

  • position.x となっている様な箇所を、 position.y に変更
  • 「new Vector3」 を行なっている箇所では、 Y軸に対して "difference"値 を設定する
  • Screen.width となっている様な箇所を、 Screen.height に変更
  • 縦に(Y軸に)動かすため、newPosition計算部分の符号を "+" から "-"に変更

Diff

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.EventSystems; 5 6public class PageSwiper : MonoBehaviour, IDragHandler, IEndDragHandler{ 7 private Vector3 panelLocation; 8 public float percentThreshold = 0.2f; 9 public float easing = 0.5f; 10 public int totalPages = 1; 11 private int currentPage = 1; 12 13 // Start is called before the first frame update 14 void Start(){ 15 panelLocation = transform.position; 16 } 17 public void OnDrag(PointerEventData data){ 18- float difference = data.pressPosition.x - data.position.x; 19- transform.position = panelLocation - new Vector3(difference, 0, 0); 20+ float difference = data.pressPosition.y - data.position.y; 21+ transform.position = panelLocation - new Vector3(0, difference, 0); 22 } 23 public void OnEndDrag(PointerEventData data){ 24- float percentage = (data.pressPosition.x - data.position.x) / Screen.width; 25+ float percentage = (data.pressPosition.y - data.position.y) / -Screen.height; 26 27 if(Mathf.Abs(percentage) >= percentThreshold){ 28 Vector3 newLocation = panelLocation; 29 if(percentage > 0 && currentPage < totalPages){ 30 currentPage++; 31- newLocation += new Vector3(-Screen.width, 0, 0); 32+ newLocation -= new Vector3(0, -Screen.height, 0); 33 }else if(percentage < 0 && currentPage > 1){ 34 currentPage--; 35- newLocation += new Vector3(Screen.width, 0, 0); 36+ newLocation -= new Vector3(0, Screen.height, 0); 37 } 38 StartCoroutine(SmoothMove(transform.position, newLocation, easing)); 39 panelLocation = newLocation; 40 }else{ 41 StartCoroutine(SmoothMove(transform.position, panelLocation, easing)); 42 } 43 } 44 IEnumerator SmoothMove(Vector3 startpos, Vector3 endpos, float seconds){ 45 float t = 0f; 46 while(t <= 1.0){ 47 t += Time.deltaTime / seconds; 48 transform.position = Vector3.Lerp(startpos, endpos, Mathf.SmoothStep(0f, 1f, t)); 49 yield return null; 50 } 51 } 52} 53

↓動作結果
イメージ説明

投稿2020/08/20 17:14

tsuki01

総合スコア1751

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yonsan

2020/08/21 01:07

ご回答いただいた通り修正致しましたところ、狙い通りの動作を確認できました。 Screnn.width→Screen.heightの変更と、newposition計算を+からーに変更することが自分にはできていなかったと理解しました。 ありがとうございました。勉強になります。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問