OutputPath.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using UnityEngine;
  4. namespace UnityEditor.Recorder
  5. {
  6. /// <summary>
  7. /// Class that allows building file paths relative.
  8. /// </summary>
  9. [Serializable]
  10. public class OutputPath
  11. {
  12. /// <summary>
  13. /// Options specifying which root location the output path is relative to (or if the path is absolute).
  14. /// </summary>
  15. public enum Root
  16. {
  17. /// <summary>
  18. /// Relative path to Project file (parent of Assets).
  19. /// </summary>
  20. Project,
  21. /// <summary>
  22. /// Relative path to Assets.
  23. /// </summary>
  24. AssetsFolder,
  25. /// <summary>
  26. /// Relative path to StreamingAssets.
  27. /// </summary>
  28. StreamingAssets,
  29. /// <summary>
  30. /// Relative path to PersistentData.
  31. /// </summary>
  32. PersistentData,
  33. /// <summary>
  34. /// Relative path to Temporary Cache.
  35. /// </summary>
  36. TemporaryCache,
  37. /// <summary>
  38. /// Absolute path.
  39. /// </summary>
  40. Absolute
  41. }
  42. [SerializeField] Root m_Root;
  43. [SerializeField] string m_Leaf;
  44. [SerializeField] bool m_ForceAssetFolder;
  45. internal Root root
  46. {
  47. get { return m_Root; }
  48. set { m_Root = value; }
  49. }
  50. internal string leaf
  51. {
  52. get { return m_Leaf; }
  53. set { m_Leaf = value; }
  54. }
  55. internal bool forceAssetsFolder
  56. {
  57. get { return m_ForceAssetFolder;}
  58. set
  59. {
  60. m_ForceAssetFolder = value;
  61. if (m_ForceAssetFolder)
  62. m_Root = Root.AssetsFolder;
  63. }
  64. }
  65. internal static OutputPath FromPath(string path)
  66. {
  67. var result = new OutputPath();
  68. if (path.Contains(Application.streamingAssetsPath))
  69. {
  70. result.m_Root = Root.StreamingAssets;
  71. result.m_Leaf = path.Replace(Application.streamingAssetsPath, string.Empty);
  72. }
  73. else if (path.Contains(Application.dataPath))
  74. {
  75. result.m_Root = Root.AssetsFolder;
  76. result.m_Leaf = path.Replace(Application.dataPath, string.Empty);
  77. }
  78. else if (path.Contains(Application.persistentDataPath))
  79. {
  80. result.m_Root = Root.PersistentData;
  81. result.m_Leaf = path.Replace(Application.persistentDataPath, string.Empty);
  82. }
  83. else if (path.Contains(Application.temporaryCachePath))
  84. {
  85. result.m_Root = Root.TemporaryCache;
  86. result.m_Leaf = path.Replace(Application.temporaryCachePath, string.Empty);
  87. }
  88. else if (path.Contains(ProjectPath()))
  89. {
  90. result.m_Root = Root.Project;
  91. result.m_Leaf = path.Replace(ProjectPath(), string.Empty);
  92. }
  93. else
  94. {
  95. result.m_Root = Root.Absolute;
  96. result.m_Leaf = path;
  97. }
  98. return result;
  99. }
  100. internal static string GetFullPath(Root root, string leaf)
  101. {
  102. var ret = string.Empty;
  103. switch (root)
  104. {
  105. case Root.PersistentData:
  106. ret = Application.persistentDataPath;
  107. break;
  108. case Root.StreamingAssets:
  109. ret = Application.streamingAssetsPath;
  110. break;
  111. case Root.TemporaryCache:
  112. ret = Application.temporaryCachePath;
  113. break;
  114. case Root.AssetsFolder:
  115. ret = Application.dataPath;
  116. break;
  117. case Root.Project:
  118. ret = ProjectPath();
  119. break;
  120. }
  121. if (root != Root.Absolute && !leaf.StartsWith("/"))
  122. {
  123. ret += "/";
  124. }
  125. ret += leaf;
  126. return ret;
  127. }
  128. internal string GetFullPath()
  129. {
  130. return GetFullPath(m_Root, m_Leaf);
  131. }
  132. static string ProjectPath()
  133. {
  134. return Regex.Replace(Application.dataPath, "/Assets$", string.Empty);
  135. }
  136. }
  137. }