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

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

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

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

Q&A

解決済

2回答

310閲覧

ドラッグアンドドロップで情報を渡したい

HightGuel

総合スコア1

Unity

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

0グッド

0クリップ

投稿2024/03/19 13:42

編集2024/03/21 07:12

実現したいこと

画面下半分にbuttonを大量に並べて、そのbuttonからドラッグを始めて、画面上部のimageの上で指を離すことで、buttonに設定されてあるスクリプトの変数をimageに設定されてあるスクリプトに渡したいです。imageに直接でなく、ゲームマネージャーを仲介する方法だとありがたいです。

bottunの位置をドラッグで移動させる必要はありません。

発生している問題・分からないこと

いろいろと調べたのですが、なかなか適した方法が分からず困っています。

独学でプログラミングを学び始めたばかりで分からないことが多く申し訳ないのですが、どなたか解決に向けてお力を貸していただけないでしょうか。

ボタンはスクロールビューに配置していて、スクロールビューには縦にスワイプした時、横にスクロールしないようにプログラムしてあります。

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; public class FoodListController : ScrollRect { private bool moveChack = false; // Scroll Viewでマウスボタン押下時 public override void OnInitializePotentialDrag(PointerEventData eventData) { base.OnInitializePotentialDrag(eventData); } // マウスボタン押下中に初めてマウスを動かしたとき public override void OnBeginDrag(PointerEventData eventData) { base.OnBeginDrag(eventData); if(Mathf.Abs(eventData.delta.x)<Mathf.Abs(eventData.delta.y)) { moveChack = false; return; } moveChack = true; } // マウスボタン押下中にマウスを動かしている間 public override void OnDrag(PointerEventData eventData) { if (moveChack) { base.OnDrag(eventData); } } // マウスボタンを離したとき public override void OnEndDrag(PointerEventData eventData) { base.OnEndDrag(eventData); } }

buttonについているItemSourceDataクラスのitemSourceDataと、画面上半分に設置したimageについているRoomCharaDataクラスのroomCharaDataをゲームコントローラーのFoodJudge_メソッドに渡したいです。

button↓

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; public class ItemButton : MonoBehaviour ,IBeginDragHandler,IDragHandler IEndDragHandler { public ItemSourceData itemSourceData; public void Awake() { this.gameObject.GetComponent<Image>().sprite = itemSourceData.sprite; } public void OnClick() { Debug.Log("ボタンが押された"); } public void OnBeginDrag(PointerEventData eventData) { GetComponent<Image>().raycastTarget = false; Debug.Log("ドラッグ開始"); } public void OnEndDrag(PointerEventData eventData) { GetComponent<Image>().raycastTarget = true; Debug.Log("ドラッグ終わり"); } }

image↓

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class FoodDrop : MonoBehaviour,IDropHandler { public GameObject gameController; public void OnDrop(PointerEventData eventData) { if (eventData.pointerDrag != null) { ItemSourceData itemSourceData= eventData.pointerDrag.gameObject.GetComponent<ItemButton>().itemSourceData; RoomCharaData roomCharaData = gameController.GetComponent<FoodJudge>().roomCharaData; gameController.GetComponent<FoodJudge>().FoodJudge_(roomCharaData, itemSourceData); } } }

ゲームコントローラー↓

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using DG.Tweening; public class FoodJudge : MonoBehaviour { public Image PlayerIllust; public RoomCharaData roomCharaData; private void Awake() { CharaChange(roomCharaData.WELCOME_SPRITE); } public void CharaChange(Sprite welcome) { PlayerIllust.GetComponent<Image>().sprite = welcome; } public void FoodJudge_(RoomCharaData roomCharaData, ItemSourceData itemSourceData) { Debug.Log(roomCharaData + " " + itemSourceData); }

試したこと・調べたこと

  • teratailやGoogle等で検索した
  • ソースコードを自分なりに変更した
  • 知人に聞いた
  • その他
上記の詳細・結果

buttonにIDragHandlerが継承されていないのでOnBeginDrag呼び出されないのだと思いますが、継承するとスクロールビューが全く動かなくなってしまいました。Rayを使う方法が1番適していると思うのですが、いまいち仕組みが理解できません。

補足

特になし

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

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

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

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

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

Refrain

2024/03/19 15:32

まずはスクリーンショットやソースコードを記載することから始めてみてはいかがでしょうか。 相談事にしても、現状が分からないと意見が出せなくなると思います。
HightGuel

2024/03/19 16:35

失礼いたしました、明日補足させていただきます。
guest

回答2

0

ベストアンサー

ボタンのスクリプトが ItemButton 1 種類ならば
ゲームマネージャーの Update に
次のような記述を追記するだけで OK と思われます。

MyImageComponent の箇所は
画像のクラスに置き換えて下さい。

Update

1private RaycastHit2D beforeHit2D; //フィールド 2 3void Update() 4{ 5 if(Input.GetMouseDown(0)) 6 { 7 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 8 beforeHit2D = Physics2D.Raycast((Vector2)ray.origin, (Vector2)ray.direction); 9 } 10 else if(Input.GetMouseUp(0)) 11 { 12 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 13 RaycastHit2D afterHit2D = Physics2D.Raycast((Vector2)ray.origin, (Vector2)ray.direction); 14 15 if(beforeHit2D && afterHit2D) 16 { 17 MyImageComponent myImageComponent = afterHit2D.GetComponent<MyImageComponent>(); 18 ItemButton hitItemButton = beforeHit2D.GetComponent<ItemButton>(); 19 if(myImageComponent && hitItemButton) 20 { 21 CharaChange(hitItemButton.itemSourceData.sprite); 22 } 23 } 24 } 25}
public class ItemButton : MonoBehaviour ,IBeginDragHandler,IDragHandler IEndDragHandler { public ItemSourceData itemSourceData; public ItemButton(Sprite sprite) { this.gameObject.GetComponent<Image>().sprite = sprite; } // public void Awake() // { // this.gameObject.GetComponent<Image>().sprite = itemSourceData.sprite; // } public void OnClick() { Debug.Log("ボタンが押された"); } public void OnBeginDrag(PointerEventData eventData) { GetComponent<Image>().raycastTarget = false; Debug.Log("ドラッグ開始"); } public void OnEndDrag(PointerEventData eventData) { GetComponent<Image>().raycastTarget = true; Debug.Log("ドラッグ終わり"); } } public class FoodJudge : MonoBehaviour { public Image PlayerIllust; public RoomCharaData roomCharaData; private RaycastHit2D beforeHit2D; private void Awake() { Sprite[] sprites = Resources.LoadAll<Sprite>(fileName); for(int i = 0; i < sprites.Length; i++) { GameObject buttonObject = new("ButtonObject" + i); buttonObject.AddComponent<Image>(); buttonObject.AddComponent<ItemButton>(); //ここで buttonObject の位置を調整する } CharaChange(roomCharaData.WELCOME_SPRITE); } void Update() { if(Input.GetMouseDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); beforeHit2D = Physics2D.Raycast((Vector2)ray.origin, (Vector2)ray.direction); } else if(Input.GetMouseUp(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit2D afterHit2D = Physics2D.Raycast((Vector2)ray.origin, (Vector2)ray.direction); if(beforeHit2D && afterHit2D) { MyImageComponent myImageComponent = afterHit2D.GetComponent<MyImageComponent>(); ItemButton hitItemButton = beforeHit2D.GetComponent<ItemButton>(); if(myImageComponent && hitItemButton) { CharaChange(hitItemButton.itemSourceData.sprite); } } } } public void CharaChange(Sprite welcome) { PlayerIllust.GetComponent<Image>().sprite = welcome; } public void FoodJudge_(RoomCharaData roomCharaData, ItemSourceData itemSourceData) { Debug.Log(roomCharaData + " " + itemSourceData); } }

投稿2024/03/22 01:36

編集2024/03/22 01:45
uni2

総合スコア256

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

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

HightGuel

2024/03/22 14:25

回答ありがとうございます。 UIにも適用できるようにスクリプトを弄ったら解決しました。 ベストアンサーに選ばせていただきました。
guest

0

少し肥大化するかもしれませんが
ゲームマネージャーの Update に
次のような記述を行うのが良いと思われます。

マウス操作のコードを添付します。

申し訳ございませんがメモ帳で打ったため
デバックはしておりません。

継承を使えるならば
インターフェイスを活用することで
for 文で回して肥大化を抑えることもできます。

Update

1private RaycastHit2D beforeHit2D; //フィールド 2 3void Update() 4{ 5 if(Input.GetMouseDown(0)) 6 { 7 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 8 beforeHit2D = Physics2D.Raycast((Vector2)ray.origin, (Vector2)ray.direction); 9 } 10 else if(Input.GetMouseUp(0)) 11 { 12 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 13 RaycastHit2D afterHit2D = Physics2D.Raycast((Vector2)ray.origin, (Vector2)ray.direction); 14 15 if(beforeHit2D && afterHit2D) 16 { 17 MyImageComponent myImageComponent = afterHit2D.GetComponent<MyImageComponent>(); 18 if(myImageComponent) 19 { 20 MyButtonComponent1 myButtonComponent1 = beforeHit2D.GetComponent<MyButtonComponent1>(); 21 if(myButtonComponent1) 22 { 23 myImageComponent.MyFunction1(myButtonComponent1.GetNum()); //myButtonComponent1.GetNum() は MyButtonComponent1 にあるゲッター 24 } 25 MyButtonComponent2 myButtonComponent2 = beforeHit2D.GetComponent<MyButtonComponent2>(); 26 if(myButtonComponent2) 27 { 28 myImageComponent.MyFunction1(myButtonComponent2.GetNum()); 29 } 30 MyButtonComponent3 myButtonComponent3 = beforeHit2D.GetComponent<MyButtonComponent3>(); 31 if(myButtonComponent3) 32 { 33 myImageComponent.MyFunction1(myButtonComponent3.GetNum()); 34 } 35 MyButtonComponent4 myButtonComponent4 = beforeHit2D.GetComponent<MyButtonComponent4>(); 36 if(myButtonComponent4) 37 { 38 myImageComponent.MyFunction1(myButtonComponent4.GetNum()); 39 } 40 } 41 } 42 } 43}

投稿2024/03/21 03:56

編集2024/03/21 04:06
uni2

総合スコア256

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

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

HightGuel

2024/03/21 07:16

ご回答ありがとうございます。ループ構文の使い方はよくわからず、ボタンは約200個ありそうなのでどうしようかと悩んでおります。スクリプトの追加が遅れてしまい申し訳ございません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問