Skip to content

Instantly share code, notes, and snippets.

@eai04191
Last active August 13, 2024 17:07
Show Gist options
  • Save eai04191/5cb1e9a9d85cd8334ced48a2c18bb0b3 to your computer and use it in GitHub Desktop.
Save eai04191/5cb1e9a9d85cd8334ced48a2c18bb0b3 to your computer and use it in GitHub Desktop.

Objectを動かし続けるやつ

2024-08-14_01-41-12_Unity.mp4

インストール

Assets/Eai/Editor に ObjectMover.cs を置く

(Editorと付いているフォルダの中であればどこでもいいです)

使い方

Tools/Object Mover で起動

移動させたいオブジェクトと移動させたい速度と方向を指定して、Start Movingを押すと動き続ける

Sceneのカメラは追従しない。適当にカメラオブジェクトを作って、移動させたいオブジェクトの子として入れておくとGameビューが追従する

using UnityEditor;
using UnityEngine;
public class ObjectMoverWindow : EditorWindow
{
private GameObject targetObject;
private float moveSpeed = 2f;
private Vector3 moveDirection = Vector3.forward;
private Vector3 initialPosition;
private bool isMoving = false;
[MenuItem("Tools/Object Mover")]
public static void ShowWindow()
{
GetWindow<ObjectMoverWindow>("Object Mover");
}
private void OnGUI()
{
targetObject = (GameObject)
EditorGUILayout.ObjectField("Target Object", targetObject, typeof(GameObject), true);
EditorGUILayout.HelpBox(
"Before moving, make the camera object a child of target object and make it follow.",
MessageType.Info
);
moveSpeed = EditorGUILayout.FloatField("Move Speed", moveSpeed);
EditorGUILayout.HelpBox(
"In VRChat SDK, default walk speed is 2, run speed is 4",
MessageType.Info
);
moveDirection = EditorGUILayout.Vector3Field("Move Direction", moveDirection);
GUILayout.Box("", GUILayout.Height(1), GUILayout.ExpandWidth(true));
if (!EditorApplication.isPlaying)
{
EditorGUILayout.HelpBox("PhysBone is only works in play mode", MessageType.Warning);
}
if (GUILayout.Button("Start Moving"))
{
if (targetObject != null && !isMoving)
{
initialPosition = targetObject.transform.position; // 移動開始前に元の位置を保存
EditorApplication.update += MoveObject;
isMoving = true;
}
}
if (GUILayout.Button("Reset Position"))
{
if (isMoving && targetObject != null)
{
EditorApplication.update -= MoveObject;
targetObject.transform.position = initialPosition; // 元の位置にリセット
SceneView.RepaintAll(); // シーンビューを更新してリセットされた位置を反映
isMoving = false;
}
}
GUILayout.Box("", GUILayout.Height(1), GUILayout.ExpandWidth(true));
if (GUILayout.Button("Open Source Code"))
{
Application.OpenURL(
"https://gist.github.com/eai04191/5cb1e9a9d85cd8334ced48a2c18bb0b3"
);
}
}
private void MoveObject()
{
if (targetObject != null)
{
targetObject.transform.position +=
moveDirection.normalized * moveSpeed * Time.deltaTime;
SceneView.RepaintAll(); // シーンビューを更新して移動を視覚的に確認できるようにする
}
}
private void OnDisable()
{
// ウィンドウが閉じられたときに移動を停止し、元の位置にリセット
if (isMoving && targetObject != null)
{
targetObject.transform.position = initialPosition;
EditorApplication.update -= MoveObject;
isMoving = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment