NRProjectConfigHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System;
  12. using System.IO;
  13. using System.Collections.Generic;
  14. using UnityEditor;
  15. using UnityEngine;
  16. using LitJson;
  17. #if UNITY_EDITOR
  18. [UnityEditor.InitializeOnLoad]
  19. #endif
  20. public static class NRProjectConfigHelper
  21. {
  22. static NRProjectConfigHelper()
  23. {
  24. EditorApplication.update += OnceUpdate;
  25. }
  26. static void OnceUpdate()
  27. {
  28. var projectConfig = GetProjectConfig();
  29. // Avoid to import asset while beginning to run application in editor.
  30. if (!Application.isPlaying)
  31. ApplyProjectConfigToSessionConfig(projectConfig);
  32. EditorApplication.update -= OnceUpdate;
  33. }
  34. // Load projectConfig from default path, if it doesnot exist, create the asset.
  35. public static NRProjectConfig GetProjectConfig()
  36. {
  37. NRProjectConfig projectConfig = null;
  38. string projectConfigAssetPath = GetProjectConfigAssetPath();
  39. try
  40. {
  41. projectConfig = AssetDatabase.LoadAssetAtPath(projectConfigAssetPath, typeof(NRProjectConfig)) as NRProjectConfig;
  42. }
  43. catch (System.Exception e)
  44. {
  45. NRDebugger.Warning("Unable to load NRProjectConfig from {0}, error {1}", projectConfigAssetPath, e.Message);
  46. }
  47. // BuildPipeline.isBuildingPlayer cannot be called in static constructor
  48. if (projectConfig == null && !BuildPipeline.isBuildingPlayer)
  49. {
  50. projectConfig = ScriptableObject.CreateInstance<NRProjectConfig>();
  51. projectConfig.targetDeviceTypes = new List<NRDeviceType>();
  52. projectConfig.targetDeviceTypes.Add(NRDeviceType.NrealLight);
  53. projectConfig.targetDeviceTypes.Add(NRDeviceType.NrealAir);
  54. AssetDatabase.CreateAsset(projectConfig, projectConfigAssetPath);
  55. }
  56. return projectConfig;
  57. }
  58. public static void CommitProjectConfig(NRProjectConfig projectConfig)
  59. {
  60. string projectConfigAssetPath = GetProjectConfigAssetPath();
  61. string customConfigAssetPath = AssetDatabase.GetAssetPath(projectConfig);
  62. if (customConfigAssetPath != projectConfigAssetPath)
  63. {
  64. NRDebugger.Warning("The asset path of NRProjectConfig is legal only for: {0}, error path: {1}", projectConfigAssetPath, customConfigAssetPath);
  65. }
  66. EditorUtility.SetDirty(projectConfig);
  67. }
  68. private static string GetProjectConfigAssetPath()
  69. {
  70. var so = ScriptableObject.CreateInstance(typeof(NRStubHelper));
  71. var script = MonoScript.FromScriptableObject(so);
  72. string assetPath = AssetDatabase.GetAssetPath(script);
  73. string editorDir = Directory.GetParent(assetPath).FullName;
  74. string nrsdkDir = Directory.GetParent(editorDir).FullName;
  75. if (NRStubHelper.IsInsideUnityPackage())
  76. {
  77. nrsdkDir = Path.GetFullPath(Path.Combine(Application.dataPath, "NRSDK"));
  78. if (!Directory.Exists(nrsdkDir))
  79. {
  80. Directory.CreateDirectory(nrsdkDir);
  81. }
  82. }
  83. string configAssetPath = Path.GetFullPath(Path.Combine(nrsdkDir, "NRProjectConfig.asset"));
  84. Uri configUri = new Uri(configAssetPath);
  85. Uri projectUri = new Uri(Application.dataPath);
  86. Uri relativeUri = projectUri.MakeRelativeUri(configUri);
  87. return relativeUri.ToString();
  88. }
  89. public static void ApplyProjectConfigToSessionConfig(NRProjectConfig projectConfig)
  90. {
  91. var sessionConfigGuids = AssetDatabase.FindAssets("t:NRSessionConfig");
  92. foreach (var item in sessionConfigGuids)
  93. {
  94. var sessionConfig = AssetDatabase.LoadAssetAtPath<NRSessionConfig>(
  95. AssetDatabase.GUIDToAssetPath(item));
  96. sessionConfig.SetProjectConfig(projectConfig);
  97. EditorUtility.SetDirty(sessionConfig);
  98. }
  99. AssetDatabase.SaveAssets();
  100. AssetDatabase.Refresh();
  101. }
  102. }
  103. }