Advertisement
NovusX

list_custom_editor

May 25th, 2021
3,426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | None | 0 0
  1. //Public arracy class to add to a game object in the scee
  2. using UnityEngine;
  3.  
  4. public class PhysicalSimulatorList : MonoBehaviour
  5. {
  6.     [HideInInspector]
  7.     public GameObject[] objectsToSimulate = new GameObject[] {};
  8.  
  9.     //public getter method
  10.     public GameObject[] GetList()
  11.     {
  12.         return objectsToSimulate;
  13.     }
  14. }
  15.  
  16. //Editor Window Class
  17.  
  18. using UnityEngine;
  19. using UnityEditor;
  20. using UnityEditorInternal;
  21.  
  22. public class PhysicalSimulator : EditorWindow
  23. {
  24.     private const string _helpText = "Cannot find 'Physical Simulation List' component on any GameObject in the scene!";
  25.  
  26.     private static Vector2 _windowsMinSize = Vector2.one * 500f;
  27.     private static Rect _helpRect = new Rect(0f, 0f, 400f, 100f);
  28.     private static Rect _listRect = new Rect(Vector2.zero, _windowsMinSize);
  29.  
  30.     private bool _isActive;
  31.  
  32.     SerializedObject _objectSO = null;
  33.     ReorderableList _listRE = null;
  34.  
  35.     PhysicalSimulatorList _simulatorList;
  36.  
  37.     [MenuItem("Automation/Physical Simulator")]
  38.     public static void OpenSimulatorWindow()
  39.     {
  40.         GetWindow<PhysicalSimulator>(true,"Physical Simulator");
  41.     }
  42.  
  43.     private void OnEnable()
  44.     {
  45.        
  46.         _simulatorList = FindObjectOfType<PhysicalSimulatorList>();
  47.  
  48.         if (_simulatorList)
  49.         {
  50.             _objectSO = new SerializedObject(_simulatorList);
  51.  
  52.             //init list
  53.             _listRE = new ReorderableList(_objectSO, _objectSO.FindProperty("objectsToSimulate"), true,
  54.                 true, true, true);
  55.  
  56.             //handle drawing
  57.             _listRE.drawHeaderCallback = (rect) => EditorGUI.LabelField(rect, "Game Objects");
  58.             _listRE.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
  59.             {
  60.                 rect.y += 2f;
  61.                 rect.height = EditorGUIUtility.singleLineHeight;
  62.                 GUIContent objectLabel = new GUIContent($"GameObject {index}");
  63.                 //the index will help numerate the serialized fields
  64.                 EditorGUI.PropertyField(rect, _listRE.serializedProperty.GetArrayElementAtIndex(index), objectLabel);
  65.             };
  66.         }
  67.     }
  68.  
  69.     private void OnInspectorUpdate()
  70.     {
  71.         Repaint();
  72.     }
  73.  
  74.     private void OnGUI()
  75.     {
  76.         if (_objectSO == null)
  77.         {
  78.             EditorGUI.HelpBox(_helpRect, _helpText, MessageType.Warning);
  79.             return;
  80.         }
  81.         else if (_objectSO != null)
  82.         {
  83.             _objectSO.Update();
  84.             _listRE.DoList(_listRect);
  85.             _objectSO.ApplyModifiedProperties();
  86.         }
  87.  
  88.         GUILayout.Space(_listRE.GetHeight() + 30f);
  89.         GUILayout.Label("Please select Game Objects to simulate");
  90.         GUILayout.Space(10f);
  91.  
  92.         EditorGUILayout.BeginHorizontal();
  93.        
  94.  
  95.         GUILayout.Space(30f);
  96.  
  97.         if (GUILayout.Button("Run Simulation"))
  98.         {
  99.             RunSimulation();
  100.         }
  101.  
  102.         GUILayout.Space(30f);
  103.  
  104.         if (GUILayout.Button("Stop Simulation"))
  105.         {
  106.             StopSimulation();
  107.         }
  108.  
  109.         GUILayout.Space(30f);
  110.  
  111.         GUILayout.Label(_isActive ? "Simulation Activated!" : "Simulation Deactivated!", EditorStyles.boldLabel);
  112.  
  113.         EditorGUILayout.EndHorizontal();
  114.     }
  115.  
  116.     private void RunSimulation()
  117.     {
  118.         _isActive = true;
  119.  
  120.         foreach (var obj in _simulatorList.GetList())
  121.         {
  122.             if (obj.GetComponent<Rigidbody>() == null)
  123.                 obj.AddComponent<Rigidbody>();
  124.  
  125.             if (obj.GetComponent<MeshRenderer>() == null)
  126.                 obj.AddComponent<MeshRenderer>();
  127.         }
  128.     }
  129.  
  130.     private void StopSimulation()
  131.     {
  132.         _isActive = false;
  133.  
  134.         foreach (var obj in _simulatorList.GetList())
  135.         {
  136.             var rb = obj.GetComponent<Rigidbody>();
  137.             var mesh = obj.GetComponent<MeshRenderer>();
  138.  
  139.             if (rb != null)
  140.                 DestroyImmediate(rb);
  141.  
  142.             if (mesh != null)
  143.                 DestroyImmediate(mesh);
  144.            
  145.         }
  146.     }
  147.  
  148. }//class
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement