EditorHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Traffic Simulation
  2. // https://github.com/mchrbn/unity-traffic-simulation
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace TrafficSimulation {
  6. public static class EditorHelper {
  7. public static void SetUndoGroup(string label) {
  8. //Create new Undo Group to collect all changes in one Undo
  9. Undo.SetCurrentGroupName(label);
  10. }
  11. public static void BeginUndoGroup(string undoName, TrafficSystem trafficSystem) {
  12. //Create new Undo Group to collect all changes in one Undo
  13. Undo.SetCurrentGroupName(undoName);
  14. //Register all TrafficSystem changes after this (string not relevant here)
  15. Undo.RegisterFullObjectHierarchyUndo(trafficSystem.gameObject, undoName);
  16. }
  17. public static GameObject CreateGameObject(string name, Transform parent = null) {
  18. GameObject newGameObject = new GameObject(name);
  19. //Register changes for Undo (string not relevant here)
  20. Undo.RegisterCreatedObjectUndo(newGameObject, "Spawn new GameObject");
  21. Undo.SetTransformParent(newGameObject.transform, parent, "Set parent");
  22. return newGameObject;
  23. }
  24. public static T AddComponent<T>(GameObject target) where T : Component {
  25. return Undo.AddComponent<T>(target);
  26. }
  27. //Determines if a ray hits a sphere
  28. public static bool SphereHit(Vector3 center, float radius, Ray r) {
  29. Vector3 oc = r.origin - center;
  30. float a = Vector3.Dot(r.direction, r.direction);
  31. float b = 2f * Vector3.Dot(oc, r.direction);
  32. float c = Vector3.Dot(oc, oc) - radius * radius;
  33. float discriminant = b * b - 4f * a * c;
  34. if (discriminant < 0f) {
  35. return false;
  36. }
  37. float sqrt = Mathf.Sqrt(discriminant);
  38. return -b - sqrt > 0f || -b + sqrt > 0f;
  39. }
  40. //From S_Darkwell: https://forum.unity.com/threads/adding-layer-by-script.41970/
  41. public static void CreateLayer(string name){
  42. if (string.IsNullOrEmpty(name))
  43. throw new System.ArgumentNullException("name", "New layer name string is either null or empty.");
  44. var tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
  45. var layerProps = tagManager.FindProperty("layers");
  46. var propCount = layerProps.arraySize;
  47. SerializedProperty firstEmptyProp = null;
  48. for (var i = 0; i < propCount; i++)
  49. {
  50. var layerProp = layerProps.GetArrayElementAtIndex(i);
  51. var stringValue = layerProp.stringValue;
  52. if (stringValue == name) return;
  53. if (i < 8 || stringValue != string.Empty) continue;
  54. if (firstEmptyProp == null)
  55. firstEmptyProp = layerProp;
  56. }
  57. if (firstEmptyProp == null)
  58. {
  59. UnityEngine.Debug.LogError("Maximum limit of " + propCount + " layers exceeded. Layer \"" + name + "\" not created.");
  60. return;
  61. }
  62. firstEmptyProp.stringValue = name;
  63. tagManager.ApplyModifiedProperties();
  64. }
  65. //From SkywardRoy: https://forum.unity.com/threads/change-gameobject-layer-at-run-time-wont-apply-to-child.10091/
  66. public static void SetLayer (this GameObject gameObject, int layer, bool includeChildren = false) {
  67. if (!includeChildren) {
  68. gameObject.layer = layer;
  69. return;
  70. }
  71. foreach (var child in gameObject.GetComponentsInChildren(typeof(Transform), true)) {
  72. child.gameObject.layer = layer;
  73. }
  74. }
  75. }
  76. }