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

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

新規登録して質問してみよう
ただいま回答率
85.49%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

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

Q&A

解決済

1回答

2454閲覧

【Unity】UIのサイズ変更後に判定がズレてしまう

Y0241-N

総合スコア1066

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

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

0グッド

0クリップ

投稿2021/12/09 04:53

編集2021/12/09 05:11

前提・実現したいこと

スクリプトでUIのサイズを動的に変更出来るようにしようとしています。
一回目は見た目通りの位置に判定があり、ドラッグすることでUIのサイズを変更できます。
しかし、サイズ変更後は見た目通りの位置に判定が無く、ずれた場所に判定があります。

サイズ変更後になぜズレた位置に判定があるのか分からず困っています。

該当のソースコード

サイズ変更のスクリプト

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5using UnityEngine.EventSystems; 6 7 8public class Change_WindowSize : MonoBehaviour, IDragHandler,IPointerDownHandler 9{ 10 private Texture2D[] CursorImage; 11 private PointerEventData pointer; 12 private GameObject Window; 13 14 private Vector2 MouseDownPos; 15 private Vector2 MouseDragPos; 16 private Vector2 MoveVector; 17 private string clickAreaName; 18 19 void Start() 20 { 21 Window = gameObject; 22 pointer = new PointerEventData(EventSystem.current); 23 } 24 25 void CheckArea() 26 { 27 List<RaycastResult> results = new List<RaycastResult>(); 28 29 pointer.position = Input.mousePosition; 30 EventSystem.current.RaycastAll(pointer, results); 31 32 foreach (RaycastResult target in results) 33 { 34 Debug.Log("UP" +target.gameObject.name); 35 } 36 37 if(results.Count > 0) 38 { 39 clickAreaName = results?[0].gameObject.tag; 40 } 41 else 42 { 43 clickAreaName = ""; 44 } 45 } 46 47 void ChangeWindowSize(string clickAreaTag, RectTransform windowRect) 48 { 49 var resizeRect = windowRect; 50 51 switch(clickAreaTag) 52 { 53 case "upper": 54 Cursor.SetCursor(CursorImage[0], Vector2.zero, CursorMode.Auto); 55 resizeRect.SetTop(MoveVector.y); 56 break; 57 58 case "under": 59 Cursor.SetCursor(CursorImage[0], Vector2.zero, CursorMode.Auto); 60 resizeRect.SetBottom(MoveVector.y); 61 break; 62 63 case "left": 64 Cursor.SetCursor(CursorImage[1], Vector2.zero, CursorMode.Auto); 65 resizeRect.SetLeft(MoveVector.x); 66 break; 67 68 case "right": 69 Cursor.SetCursor(CursorImage[1], Vector2.zero, CursorMode.Auto); 70 resizeRect.SetRight(MoveVector.x); 71 break; 72 } 73 74 windowRect = resizeRect; 75 } 76 public void OnPointerDown(PointerEventData e) 77 { 78 MouseDownPos = Input.mousePosition; 79 CheckArea(); 80 } 81 82 public virtual void OnDrag(PointerEventData e) 83 { 84 MouseDragPos = Input.mousePosition; 85 var WindowRt = Window.GetComponent<RectTransform>(); 86 87 var c1 = WindowRt.TransformPoint(new Vector2(WindowRt.rect.x, WindowRt.rect.y)); 88 var c2 = WindowRt.TransformPoint(new Vector2(WindowRt.rect.xMax, WindowRt.rect.yMax)); 89 90 MoveVector = MouseDragPos - MouseDownPos; 91 MouseDownPos = Input.mousePosition; 92 93 ChangeWindowSize(clickAreaName,WindowRt); 94 } 95}

Set○○(float)の中身

C#

1 using UnityEngine; 2 public static class RectTransformExtensions 3 { 4 public static void SetLeft(this RectTransform rt, float left) 5 { 6 Vector2 rect = rt.offsetMin; 7 rect.x += left; 8 rt.offsetMin = rect; 9 } 10 11 public static void SetRight(this RectTransform rt, float right) 12 { 13 Vector2 rect = rt.offsetMax; 14 rect.x += right; 15 rt.offsetMax = rect; 16 } 17 18 public static void SetTop(this RectTransform rt, float top) 19 { 20 Vector2 rect = rt.offsetMax; 21 rect.y += top; 22 rt.offsetMax = rect; 23 } 24 25 public static void SetBottom(this RectTransform rt, float bottom) 26 { 27 Vector2 rect = rt.offsetMin; 28 rect.y += bottom; 29 rt.offsetMin = rect; 30 } 31 }

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

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

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

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

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

guest

回答1

0

自己解決

マウスポインタ-を変更するさいに変更する画像の左上を選択基準位置としてため、
判定位置がずれていたのではなく、マウスの選択範囲位置が左上に限定されていたのが原因でした。

マウス画像を元に戻す、もしくは変更する画像の縦横の長さの半分の値を
Cursor.SetCursor(CursorImage[0], Vector2.zero, CursorMode.Auto);
する際のVector.zero部分に入力すると正しく中央に選択範囲が来ます。

投稿2021/12/09 08:06

Y0241-N

総合スコア1066

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問