ScriptUtility¶
Namespace:
SideXP.Core.EditorOnly
Miscellaneous functions for working with scripts in the project.
Fields¶
AssemblyDefinitionExtension¶
The extension of Assembly Definition assets (with "." prefix).
CSExtension¶
The extension of C# script assets (with "." prefix).
NamespaceDeclarationPattern¶
Pattern used to extract the first namespace declared in a script.
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)¶
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)¶
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)¶
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)¶
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)¶
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)¶
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)¶
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)¶
Gets (or creates) a ScriptRef instance that represents a given script asset.
Parameters
scriptAsset: The asset that declares the script.
GetScriptRef(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)¶
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)¶
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)¶
Returns
Returns true if this script's type has been extracted successfully.
GetScriptType(MonoScript)¶
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)¶
Returns
Returns true if this script's type has been extracted successfully.
GetScriptPath(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)¶
Parameters
scriptPath: Outputs the found script's path that declares the givenType.
GetScriptContent(string)¶
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)¶
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()¶
Gets all the types from scripts in the project.
Returns
Returns all the main types declared in MonoScript assets.