うにてぃブログ

主にUnityとC#に関する記事を書いていきます

Transform

【Unity】ドラッグ操作によるオブジェクトの回転:Space.Worldを考慮した正確な方法

オブジェクトをドラッグした方向に回転させるには、Transform.Rotateを使用します。 以下の例では、ドラッグの方向にオブジェクトを回転させる方法を示しています。 using UnityEngine; using UnityEngine.EventSystems; public class RotationTest : MonoBe…

【Unity】オブジェクトを今向いている方向に前後左右移動させる

transform.rotation に移動ベクトルを乗算すると今向いている向きから前後左右に移動させることができる using UnityEngine; public class PlayerController : MonoBehaviour { [SerializeField] private float _speed = 1f; private void Update() { if (In…

【Unity】Transform の子供一覧を取得する

Transform の子供一覧を取得する方法は2つあり 一つは childCount を取得して GetChild で取得する方法 for (var i = 0; i < transform.childCount; i++) { var child = transform.GetChild(i); Debug.Log(child.name); } もう一つは transform を foreach …

【Unity】Parent of RectTransform is being set with parent property. Consider using the SetParent method instead, with the worldPositionStays argument set to false. This will retain local orientation and scale rather than world orientation and scale, which c

Parent of RectTransform is being set with parent property. Consider using the SetParent method instead, with the worldPositionStays argument set to false. This will retain local orientation and scale rather than world orientation and scale…

【Unity】対象の Transform を親に持つかどうかを調べる拡張メソッド

public static class TransformExtension { public static bool HasInParent(this Transform self, Transform target) { var current = self; while (current.parent != null) { if (current == target) return true; current = current.parent; } return fa…

【Unity】GameObject の Hierarchy 上での移動コスト

何度も計測しているわけではないのと階層の違いで速度は違うと思います parent に直接値を入れる以外は何度も利用するわけじゃなければ 使っても問題なさそう Transform.parent Transform.SetParent Transform.SetSiblingIndex Transform.SetAsFirstSibling …

【Unity】オブジェクトを回転させつづける

using UnityEngine; public class RotateObject : MonoBehaviour { [SerializeField] private Vector3 speed; private void LateUpdate() { transform.Rotate(speed, Space.Self); } }

【Unity】Hierarchy 上の Transform の順番を操作する

Transform.GetSiblingIndex 現在の順番を取得できる using UnityEngine; public class TransformGetSiblingIndex : MonoBehaviour { private void Awake() { Debug.Log(transform.GetSiblingIndex()); } } Transform.SetSiblingIndex 指定した順番に変更する…

【Unity】Transform の Global Scale (lossyScale) を 指定の値に変更する

Transform の Global Scale (lossyScale) は Read Only のため値の変更が出来ない そのため lossyScale を指定した値にする拡張を作成した /// <summary> /// <para>The global scale of the object (Read Only).</para> /// </summary> public Vector3 lossyScale { [NativeMethod("GetWorld…

【Unity】続 Unity デフォルト型の Inspector拡張 ~Transform~

【Unity】Unity デフォルト型の Inspector 拡張 - うにてぃブログ 上記記事で CanEditMultipleObjects を設定時に複数の要素の更新ができない問題があったため、いっそ TransformInspector の実装部分をまるっと実装してしまうことにする 実装 Position と S…