EditorUtilities.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using UnityEditor;
  3. using UnityEditor.SceneManagement;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. using Object = UnityEngine.Object;
  7. #if FBX_EXPORTER
  8. using UnityEditor.Formats.Fbx.Exporter;
  9. #endif
  10. namespace sc.modeling.water.common.editor
  11. {
  12. public static class EditorUtilities
  13. {
  14. public static GameObject InstantiatePrefab(string GUID, string name)
  15. {
  16. string prefabPath = AssetDatabase.GUIDToAssetPath(GUID);
  17. if (prefabPath == string.Empty)
  18. {
  19. Debug.LogError($"The \"{name}\" prefab could not be found in the project, it was either deleted or not imported.");
  20. return null;
  21. }
  22. Object prefab = AssetDatabase.LoadAssetAtPath<Object>(prefabPath);
  23. return PrefabUtility.InstantiatePrefab(prefab, SceneManager.GetActiveScene()) as GameObject;
  24. }
  25. /// <summary>
  26. /// Position the object in view, select it and mark the scene as changed.
  27. /// </summary>
  28. /// <param name="gameObject"></param>
  29. public static void OnPostGameObjectCreation(GameObject gameObject)
  30. {
  31. if (Selection.activeGameObject) gameObject.transform.parent = Selection.activeGameObject.transform;
  32. PositionObjectInScene(gameObject);
  33. Selection.activeGameObject = gameObject;
  34. if(Application.isPlaying == false) EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
  35. }
  36. public static void PositionObjectInScene(GameObject gameObject)
  37. {
  38. //Position in view
  39. if (SceneView.lastActiveSceneView)
  40. {
  41. var transform = SceneView.lastActiveSceneView.camera.transform;
  42. float size = 50f;
  43. MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>();
  44. if(meshRenderer) size = (meshRenderer.bounds.min - meshRenderer.bounds.max).magnitude;
  45. Vector3 spawnPosition = transform.position + (transform.forward * size);
  46. if (Physics.Raycast(transform.position, transform.forward, out var hit, 2000, -1, QueryTriggerInteraction.Ignore))
  47. {
  48. spawnPosition = hit.point - (transform.forward * 1f);
  49. }
  50. gameObject.transform.position = spawnPosition;
  51. }
  52. }
  53. public static class FBX
  54. {
  55. /// <summary>
  56. /// Export any sort of meshes contained in the object, and load+reference them back to it
  57. /// </summary>
  58. /// <param name="gameObject"></param>
  59. /// <param name="filePath"></param>
  60. /// <exception cref="Exception">Empty file path, or missing the FBX extension</exception>
  61. public static void SaveToFileAndReference(GameObject gameObject, string filePath)
  62. {
  63. #if FBX_EXPORTER
  64. if (filePath == string.Empty)
  65. {
  66. throw new Exception("Failed to save mesh(es) to an FBX file, file path is empty");
  67. }
  68. if (filePath.EndsWith(".fbx") == false)
  69. {
  70. throw new Exception("Failed to save mesh(es) to an FBX file, file path must end with \".fbx\"");
  71. }
  72. //Export
  73. ModelExporter.ExportObject(filePath, gameObject);
  74. //Import
  75. AssetDatabase.ImportAsset(filePath, ImportAssetOptions.ForceSynchronousImport);
  76. //Loading meshes and assigning them to MeshFilters
  77. Object[] subAssets = AssetDatabase.LoadAllAssetsAtPath(filePath);
  78. LODGroup lodGroup = gameObject.GetComponent<LODGroup>();
  79. if (lodGroup)
  80. {
  81. MeshFilter[] meshFilters = gameObject.GetComponentsInChildren<MeshFilter>();
  82. for (int i = 1; i < meshFilters.Length; i++) //Skip the first, as it is the parent
  83. {
  84. if (subAssets[i] is Mesh) meshFilters[i].sharedMesh = subAssets[i] as Mesh;
  85. }
  86. }
  87. else
  88. {
  89. MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
  90. if (meshFilter)
  91. {
  92. for (int i = 1; i < subAssets.Length; i++) //Skip the first, as it is the parent
  93. {
  94. if (subAssets[i] is Mesh) meshFilter.sharedMesh = subAssets[i] as Mesh;
  95. }
  96. }
  97. }
  98. #endif
  99. }
  100. }
  101. }
  102. }