Skip to content

PathUtility

Namespace: SideXP.Core

public static class PathUtility

Miscellaneous functions for working with path strings.

Fields

DefaultDirectorySeparator

public const char DefaultDirectorySeparator = '/'

The character used by default as path directory separator.

AssetsDirectory

public const string AssetsDirectory = "Assets"

Name of the /Assets directory.

ResourcesDirectory

public const string ResourcesDirectory = "Resources"

Name of the /Resources directory.

BaseResourcesPath

public const string BaseResourcesPath = "Assets/Resources"

The base relative path to the /Resources directory at the root of the /Assets directory.

ProjectPath

public static readonly string ProjectPath

The absolute path to this project. In the editor, this is the path to the project's folder (the root folder, not /Assets). In build, it's the path to the persistent data path directory (same as PersistentDataPath). This value is meant to be used by custom editors or in-game features that require to store persistent data.

DataPath

public static readonly string DataPath

The absolute path to the game content directory. In the editor, this is the path to the project's /Assets folder. In build, it's the path to the folder that contains the game executable. Note that in build, that directory is readonly, so you must use PersistentDataPath to store persistent data.

See also

-

PersistentDataPath

public static readonly string PersistentDataPath

The absolute path to the persistent data directory. This path varies from plaforms, but is guaranteed to be writable, so you can use it to store persistent data.

See also

-

TemporaryDataPath

public static readonly string TemporaryDataPath

The absolute path to the temporary data directory.

See also

-

Methods

ToPath(string, bool, char)

public static string ToPath(string str, bool normalize = false, char separator = '/')

Converts the given string into a path, by removing forbidden characters and using a single character as directory separator.

Parameters

  • str: The string to convert.
  • normalize: If enabled, the path is resolved to its full path (in the editor, this also resolves Packages/... virtual paths to their actual folder).
  • separator: The character to use as directory separator.

Returns

Returns the converted string.

Example

PathUtility.ToPath("Assets\\Foo\\Bar");                // "Assets/Foo/Bar"
PathUtility.ToPath("Assets/Foo/Bar", separator: '\\'); // "Assets\Foo\Bar"

ToPath(string, bool, char)

public static void ToPath(ref string str, bool normalize = false, char separator = '/')

Converts the given string into a path, by removing forbidden characters and using a single character as directory separator.

Parameters

  • str: The string to convert.
  • normalize: If enabled, the path is resolved to its full path (in the editor, this also resolves Packages/... virtual paths to their actual folder).
  • separator: The character to use as directory separator.

Returns

Returns the converted string.

Example

PathUtility.ToPath("Assets\\Foo\\Bar");                // "Assets/Foo/Bar"
PathUtility.ToPath("Assets/Foo/Bar", separator: '\\'); // "Assets\Foo\Bar"

ToPaths(IEnumerable<string>, bool, char)

public static string[] ToPaths(IEnumerable<string> strs, bool normalize = false, char separator = '/')

Converts the given strings into paths, by removing forbidden characters and use the same character as directory separator.

Parameters

  • strs: The collection of strings to convert.

ToAbsolutePath(string)

public static string ToAbsolutePath(string path)

If the given string represents a relative path, it's combined with ProjectPath to make it absolute.

Parameters

  • path: The path string to convert. If that path is already absolute, this function returns it as is. If it's null or empty, this function returns the value of ProjectPath.

Returns

Returns the absolute path string.

Example

// Assuming the project is at "C:/dev/MyProject":
PathUtility.ToAbsolutePath("Assets/Foo"); // "C:/dev/MyProject/Assets/Foo"
PathUtility.ToAbsolutePath("");           // the project path

ToAbsolutePath(string)

public static void ToAbsolutePath(ref string path)

If the given string represents a relative path, it's combined with ProjectPath to make it absolute.

Parameters

  • path: The path string to convert. If that path is already absolute, this function returns it as is. If it's null or empty, this function returns the value of ProjectPath.

Returns

Returns the absolute path string.

Example

// Assuming the project is at "C:/dev/MyProject":
PathUtility.ToAbsolutePath("Assets/Foo"); // "C:/dev/MyProject/Assets/Foo"
PathUtility.ToAbsolutePath("");           // the project path

ToRelativePath(string, bool)

public static string ToRelativePath(string path, bool normalize = false)

If the given string represents an absolute path leading to this project, this function makes it relative to ProjectPath.

Parameters

  • path: The path string to convert. If that path is already relative, this function returns it as is. If it's null or empty, this function returns an empty string.

Returns

Returns the relative path string.

Example

// Assuming the project is at "C:/dev/MyProject":
PathUtility.ToRelativePath("C:/dev/MyProject/Assets/Foo"); // "Assets/Foo"
PathUtility.ToRelativePath("Assets/Foo");                  // "Assets/Foo" (already relative)

ToRelativePath(string, bool)

public static void ToRelativePath(ref string path, bool normalize = false)

If the given string represents an absolute path leading to this project, this function makes it relative to ProjectPath.

Parameters

  • path: The path string to convert. If that path is already relative, this function returns it as is. If it's null or empty, this function returns an empty string.

Returns

Returns the relative path string.

Example

// Assuming the project is at "C:/dev/MyProject":
PathUtility.ToRelativePath("C:/dev/MyProject/Assets/Foo"); // "Assets/Foo"
PathUtility.ToRelativePath("Assets/Foo");                  // "Assets/Foo" (already relative)

IsProjectPath(string)

public static bool IsProjectPath(string path)

Checks if the given path leads to this project.

Parameters

  • path: The path string to check. If that path is relative this function returns true.

Returns

Returns true if the given path leads to this project.

Example

PathUtility.IsProjectPath("Assets/Foo");                  // true (relative paths are project paths)
PathUtility.IsProjectPath("C:/dev/MyProject/Assets/Foo"); // true
PathUtility.IsProjectPath("C:/Windows/System32");         // false

IsProjectPath(string)

public static bool IsProjectPath(ref string path)

Checks if the given path leads to this project.

Parameters

  • path: The path string to check. If that path is relative this function returns true.

Returns

Returns true if the given path leads to this project.

Example

PathUtility.IsProjectPath("Assets/Foo");                  // true (relative paths are project paths)
PathUtility.IsProjectPath("C:/dev/MyProject/Assets/Foo"); // true
PathUtility.IsProjectPath("C:/Windows/System32");         // false