Unityでマップ状に情報を管理するソフトを作っているのですが、マウス座標を中心としたオブジェクトの拡大縮小がなかなかできません。
オブジェクトの構造はこのようになっています。ScrollViewの中のElementsが拡大縮小し、拡大縮小可能なScrollViewとして機能する予定です。
Unityでは、Pivotを変更することで拡大の中心などを変えられると思い、
強引に数値を変換しようとしたのですが、とても誤差が大きく使い物になりませんでした。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using System; 5 6public class ElementView : MonoBehaviourCustom 7{ 8 RectTransform thisRT; 9 // Start is called before the first frame update 10 void Start() 11 { 12 thisRT = GetComponent<RectTransform>(); 13 StartCoroutine(DelayFlameEnd(() => { UpdateSize(); })); 14 } 15 16 // Update is called once per frame 17 void Update() 18 { 19 Debug.Log(Input.GetAxis("Mouse ScrollWheel")); 20 Vector2 anchor = ((Vector2)Input.mousePosition + thisRT.anchoredPosition + new Vector2(0, 30)) / thisRT.sizeDelta; 21 Debug.Log(anchor); 22 thisRT.pivot = anchor; 23 if (Input.GetAxis("Mouse ScrollWheel") == 0.1f) 24 { 25 thisRT.localScale *= 0.9f; 26 } 27 if (Input.GetAxis("Mouse ScrollWheel") == -0.1f) 28 { 29 thisRT.localScale /= 0.9f; 30 } 31 if (Input.GetMouseButtonUp(0)) 32 { 33 UpdateSize(); 34 } 35 thisRT.pivot = new Vector2(0, 1); 36 } 37 38 void UpdateSize() 39 { 40 float maxX = 0; 41 float minY = 0; 42 foreach (Transform childTransform in transform) 43 { 44 RectTransform childRT = childTransform.gameObject.GetComponent<RectTransform>(); 45 float rtX = childRT.anchoredPosition.x + childRT.sizeDelta.x; 46 float rtY = -childRT.anchoredPosition.y + childRT.sizeDelta.y; 47 if (maxX < rtX) 48 { 49 maxX = rtX; 50 } 51 if (minY < rtY) 52 { 53 minY = rtY; 54 } 55 } 56 thisRT.sizeDelta = new Vector2(maxX+100, minY+100); 57 } 58} 59
###分からない点
マウス座標を中心として拡大する方法。
Transformを使って似たようなことをしているサイトはあったが、RectTransformではできないのか。
試したこと
Pivotを計算し、そのまま拡大縮小をした。
補足情報(FW/ツールのバージョンなど)
Unity2019 1.4f1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。