Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Save this class inside a folder called "Editor" (without quotation you know)
- // and also the file MUST be called PhysicsSimulationWindow.cs
- using UnityEngine;
- using UnityEditor;
- using System.Collections.Generic;
- using UnityEditor.SceneManagement;
- public class PhysicsSimulationWindow : EditorWindow
- {
- private bool isSimulating = false;
- private float simulationTime = 0f;
- private float simulationDuration = 5f;
- private SimulationMode originalSimulationMode;
- private List<GameObject> selectedObjects = new List<GameObject>();
- private List<Rigidbody> addedRigidbodies = new List<Rigidbody>();
- private Vector2 scrollPosition;
- [MenuItem("Tools/Physics Simulation")]
- public static void ShowWindow()
- {
- GetWindow<PhysicsSimulationWindow>("Physics Simulation");
- }
- private void OnEnable()
- {
- Selection.selectionChanged += Repaint;
- }
- private void OnDisable()
- {
- Selection.selectionChanged -= Repaint;
- }
- private void OnGUI()
- {
- UpdateSelectedObjects();
- GUILayout.Label("Selected Objects", EditorStyles.boldLabel);
- if (IsPrefabMode())
- {
- EditorGUILayout.HelpBox("Physics simulation is not available in the prefab editor. Please return to the main scene.", MessageType.Warning);
- return;
- }
- EditorGUI.BeginDisabledGroup(isSimulating);
- simulationDuration = EditorGUILayout.FloatField("Simulation Time:", simulationDuration);
- EditorGUI.EndDisabledGroup();
- if (GUILayout.Button(isSimulating ? "Stop Simulation" : "Start Simulation"))
- {
- if (isSimulating)
- {
- StopSimulation();
- }
- else
- {
- StartSimulation();
- }
- }
- if (isSimulating)
- {
- EditorGUILayout.LabelField($"Simulation Time: {simulationTime:F2}s / {simulationDuration:F2}s");
- }
- GUILayout.Space(10);
- GUILayout.Label("Selected Objects:", EditorStyles.boldLabel);
- scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.Height(150));
- foreach (var obj in selectedObjects)
- {
- EditorGUILayout.ObjectField(obj, typeof(GameObject), true);
- }
- EditorGUILayout.EndScrollView();
- }
- private void UpdateSelectedObjects()
- {
- selectedObjects.Clear();
- selectedObjects.AddRange(Selection.gameObjects);
- }
- private bool IsPrefabMode()
- {
- return PrefabStageUtility.GetCurrentPrefabStage() != null;
- }
- private void StartSimulation()
- {
- if (isSimulating || IsPrefabMode()) return;
- selectedObjects.Clear();
- addedRigidbodies.Clear();
- selectedObjects.AddRange(Selection.gameObjects);
- if (selectedObjects.Count == 0)
- {
- EditorUtility.DisplayDialog("No objects selected", "Select at least one object in the scene.", "OK");
- return;
- }
- foreach (var obj in selectedObjects)
- {
- Rigidbody rb = obj.GetComponent<Rigidbody>();
- if (rb == null)
- {
- rb = Undo.AddComponent<Rigidbody>(obj);
- addedRigidbodies.Add(rb);
- }
- }
- isSimulating = true;
- simulationTime = 0f;
- originalSimulationMode = Physics.simulationMode;
- EditorApplication.update += SimulatePhysics;
- }
- private void StopSimulation()
- {
- if (!isSimulating) return;
- isSimulating = false;
- EditorApplication.update -= SimulatePhysics;
- Physics.simulationMode = originalSimulationMode;
- foreach (var rb in addedRigidbodies)
- {
- if (rb != null)
- {
- Undo.DestroyObjectImmediate(rb);
- }
- }
- addedRigidbodies.Clear();
- SceneView.RepaintAll();
- }
- private void SimulatePhysics()
- {
- if (!isSimulating) return;
- Physics.simulationMode = SimulationMode.Script;
- Physics.Simulate(Time.fixedDeltaTime);
- simulationTime += Time.fixedDeltaTime;
- SceneView.RepaintAll();
- if (simulationTime >= simulationDuration)
- {
- StopSimulation();
- }
- Repaint();
- }
- private void OnDestroy()
- {
- if (isSimulating)
- {
- StopSimulation();
- }
- }
- }
Advertisement
Comments
-
- Save this class inside a folder called "Editor" (without quotation you know) and also the file MUST be called PhysicsSimulationWindow.cs
Add Comment
Please, Sign In to add comment