Skip to content

ScriptUtility

Namespace: SideXP.Core.EditorOnly

public static class ScriptUtility

Miscellaneous functions for working with scripts in the project.

Fields

AssemblyDefinitionExtension

public const string AssemblyDefinitionExtension = ".asmdef"

The extension of Assembly Definition assets (with "." prefix).

CSExtension

public const string CSExtension = ".cs"

The extension of C# script assets (with "." prefix).

NamespaceDeclarationPattern

public static readonly Regex NamespaceDeclarationPattern

Pattern used to extract the first namespace declared in a script.

ClassDeclarationPattern

public static readonly Regex ClassDeclarationPattern

Pattern used to extract the first type declared in a script, and its base type if applicable.

Remarks

Captures the type name in the class group, its generic parameter list (if any) in the generics group, and its base type in the base group. Requires at least one modifier before the type keyword (an access modifier such as public/private, or another modifier such as static/partial), which keeps bare class Foo occurrences in comments from matching. Supports class/struct/interface/enum and record (including record class/record struct) declarations, in any modifier order.

Methods

GetNamespaceFromPath(string)

public static string GetNamespaceFromPath(string path)

Gets the namespace that should be used for a file at a given path.

Parameters

  • path: The path to the file of which to get the namespace to use. This path must be relative to the project's directory.

Returns

Returns the namespace defined in the first assembly definition file found in the hierarchy, or the root namespace defined in the editor settings.

TryGetDeclaredType(TextAsset, Type)

public static bool TryGetDeclaredType(TextAsset asset, out Type type)

Tries to extract the main type declared in a given script content.

Parameters

  • asset: The asset from which to extract the declared type.
  • type: Outputs the extracted type.

Returns

Returns true if a type has been extracted successfully.

TryGetDeclaredTypeFromScriptAt(string, Type)

public static bool TryGetDeclaredTypeFromScriptAt(string path, out Type type)

Tries to extract the main type declared in a script at the given path.

Parameters

  • path: The path to the script asset.
  • type: Outputs the extracted type.

Returns

Returns true if a type has been extracted successfully.

TryGetDeclaredType(string, Type)

public static bool TryGetDeclaredType(string content, out Type type)

Tries to extract the main type declared in a given script content.

Parameters

  • content: The content of the script from which to extract the main type.
  • type: Outputs the extracted type.

Returns

Returns true if a type has been extracted successfully.

GetScriptRef(string)

public static ScriptRef GetScriptRef(string scriptPath)

Gets a ScriptRef instance that represents the script at a given path.

Parameters

  • scriptPath: The path to the script asset.

Returns

Returns the found ScriptRef.

GetScriptRef(string, ScriptRef)

public static bool GetScriptRef(string scriptPath, out ScriptRef scriptRef)

Gets a ScriptRef instance that represents the script at a given path.

Parameters

  • scriptRef: Outputs the found or created ScriptRef.
  • scriptPath: The path to the script asset.

Returns

Returns true if a ScriptRef has been found or created for the script at the given path.

GetScriptRef(MonoScript)

public static ScriptRef GetScriptRef(MonoScript scriptAsset)

Gets (or creates) a ScriptRef instance that represents a given script asset.

Parameters

  • scriptAsset: The asset that declares the script.

Returns

Returns the found ScriptRef.

GetScriptRef(MonoScript, ScriptRef)

public static bool GetScriptRef(MonoScript scriptAsset, out ScriptRef scriptRef)

Gets (or creates) a ScriptRef instance that represents a given script asset.

Parameters

  • scriptAsset: The asset that declares the script.

GetScriptRef(Type)

public static ScriptRef GetScriptRef(Type type)

Gets (or creates) a ScriptRef instance that represents the script that declares a given type.

Parameters

  • type: The type from which to get the script ref.

Returns

Returns the found ScriptRef.

GetScriptRef(Type, ScriptRef)

public static bool GetScriptRef(Type type, out ScriptRef scriptRef)

Parameters

  • scriptRef: Outputs the found or created ScriptRef.

Returns

Returns true if a ScriptRef has been found or created for the script at the given path.

GetScriptType(string)

public static Type GetScriptType(string scriptPath)

Gets the declared type in a script file.

Returns

Returns the extracted type.

Remarks

Unity API has a GetClass function, but it doesn't work with structs and interfaces. This function uses both regular expressions and reflection, and so is able to get the declared type whatever its nature. This also applies to text, which logs an error that can't be handled as an exception if the asset is about to be deleted. We prefer using a custom implementation to read the script, making this function actually usable in an OnWillDelete() message.

GetScriptType(string, Type)

public static bool GetScriptType(string scriptPath, out Type type)

Returns

Returns true if this script's type has been extracted successfully.

GetScriptType(MonoScript)

public static Type GetScriptType(MonoScript scriptAsset)

Gets the declared type in a script file.

Returns

Returns the extracted type.

Remarks

Unity API has a GetClass function, but it doesn't work with structs and interfaces. This function uses both regular expressions and reflection, and so is able to get the declared type whatever its nature. This also applies to text, which logs an error that can't be handled as an exception if the asset is about to be deleted. We prefer using a custom implementation to read the script, making this function actually usable in an OnWillDelete() message.

GetScriptType(MonoScript, Type)

public static bool GetScriptType(MonoScript scriptAsset, out Type type)

Returns

Returns true if this script's type has been extracted successfully.

GetScriptPath(Type)

public static string GetScriptPath(Type type)

Gets the path to the script that declares a given Type.

Parameters

  • type: The type of the script to get.

Returns

Returns the found script's path that declares the given Type.

GetScriptPath(Type, string)

public static bool GetScriptPath(Type type, out string scriptPath)

Parameters

  • scriptPath: Outputs the found script's path that declares the given Type.

GetScriptContent(string)

public static string GetScriptContent(string scriptPath)

Gets the content of the script at a given path as plain text.

Parameters

  • scriptPath: The path to the script asset.

Returns

Returns the content of the script at the given path as plain text.

GetScriptContent(string, string)

public static bool GetScriptContent(string scriptPath, out string scriptContent)

Gets the content of the script at a given path as plain text.

Parameters

  • scriptContent: Outputs the content of the script at the given path.

Returns

Returns the content of the script at the given path as plain text.

GetProjectTypes()

public static Type[] GetProjectTypes()

Gets all the types from scripts in the project.

Returns

Returns all the main types declared in MonoScript assets.