loadAllUDSInDirectory.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using udSDK;
  6. /*
  7. * Loads all UDS files in a local path and places them in the world space relative to the first
  8. * Intended for multi part uds files
  9. */
  10. public class loadAllUDSInDirectory : MonoBehaviour
  11. {
  12. public string path = "";
  13. public bool reload = false;
  14. void Start()
  15. {
  16. string[] files = Directory.GetFiles(path);
  17. double[] rootBaseOffset = new double[3];
  18. Vector3 rootBaseTranslation = Vector3.zero;
  19. int baseInd = 0; //index in the list to which all models will be placed relative to
  20. for (int i = 0; i < files.Length; ++i)
  21. {
  22. string file = files[i];
  23. //skip non uds files
  24. if (!file.Substring(file.Length - 4).Equals(".uds"))
  25. {
  26. if (i == baseInd)
  27. ++baseInd;
  28. continue;
  29. }
  30. GameObject modelGameObject = new GameObject(file);
  31. modelGameObject.transform.SetParent(this.transform);
  32. modelGameObject.AddComponent<UDSModel>();
  33. UDSModel model = modelGameObject.GetComponent<UDSModel>();
  34. model.path = file;
  35. try
  36. {
  37. model.LoadModel();
  38. }
  39. catch
  40. {
  41. Debug.LogError("load model failed: " + file);
  42. if (i == baseInd)
  43. ++baseInd;
  44. continue;
  45. }
  46. if (i == baseInd) //reference all models to the first
  47. {
  48. rootBaseOffset = model.header.baseOffset;
  49. }
  50. model.geolocationOffset =
  51. UDUtilities.UDtoGL * -new Vector3(
  52. (float)rootBaseOffset[0],
  53. (float)rootBaseOffset[1],
  54. (float)rootBaseOffset[2]
  55. );
  56. modelGameObject.tag = "UDSModel";
  57. model.geolocate = true;
  58. }
  59. }
  60. private void Update()
  61. {
  62. if (this.reload)
  63. {
  64. foreach (Transform child in transform) {
  65. GameObject.Destroy(child.gameObject);
  66. }
  67. Start();
  68. reload = false;
  69. }
  70. }
  71. }