Class OptionHelper
Helper methods for Options.
Inheritance
Namespace: sttz.Trimmer
Assembly: Trimmer.dll
Syntax
public static class OptionHelper
Remarks
This static class contains methods to be used by Option subclasses that contain common design patterns or utilities.
Methods
| Improve this Doc View SourceGetAssetGUID(Object)
Helper method to get the GUID of an asset object.
Declaration
public static string GetAssetGUID(Object target)
Parameters
Type | Name | Description |
---|---|---|
UnityEngine.Object | target |
Returns
Type | Description |
---|---|
System.String | The GUID or null if the object has no GUID (is not an asset). |
GetBuildBasePath(String)
Build paths returned from Unity can point either to the executable (e.g. exe or app bundle) or the root directory of the export (e.g. XCode project). This method turns the first into the second and leaves the second unchanged, so you can get the base directory for all build targets.
Declaration
public static string GetBuildBasePath(string buildPath)
Parameters
Type | Name | Description |
---|---|---|
System.String | buildPath |
Returns
Type | Description |
---|---|
System.String |
GetSingleton<T>(Boolean)
Get a singleton script instance in the current scene. Intended for use in Options' Apply() methods.
Declaration
public static T GetSingleton<T>(bool create = true)
where T : Component
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | create | Wether to create the script if it does not exists. |
Returns
Type | Description |
---|---|
T | The script or null if |
Type Parameters
Name | Description |
---|---|
T |
Remarks
This method can be used to implement the feature injection design pattern
in Options (together with InjectFeature<T>(Scene, OptionInclusion)). In this case the
Option has an associated feature that isn't statically configured in
the project but instead injected on-demand. The feature must be implemented
as a MonoBehaviour
script that is not added to any scene but instead
exclusively injected and configured by the Option.
This method should be used in an Option's Apply() method to inject the feature when playing in the editor or when running a build with the Option included.
Example for a typical implementation:
protected bool Validate()
{
// Check if the Option is properly configured and/or enabled
return Value && !string.IsNullOrEmpty(GetChild<OptionChild>().Value);
}
public override void Apply()
{
base.Apply();
var enable = Validate();
var singleton = OptionHelper.GetSingleton<MyScript>(enable);
if (singleton != null) {
singleton.enabled = enable;
singleton.option = false;
singleton.otherOption = GetChild<OptionChild>().Value;
}
}
Here, the Option first checks if it is enabled and properly configured
in Validate
. This is then passed to GetSingleton, so that it doesn't
create the singleton if the feature is not enabled.
GetSingleton will always return the instance if it exists (even if the feature is disabled, so that it can be turned off when it was enabled before). Therefore always check if GetSingleton returns a non-null value and apply the configuration if it does.
Singletons created by this method will be stored on the "_Trimmer"
game object, which is marked DontDestroyOnLoad
, so that singletons
persist over scene loads.
Use InjectFeature<T>(Scene, OptionInclusion) in PostprocessScene(Scene, OptionInclusion) to inject the script into the build if the Option is not included.
InjectFeature<T>(Scene, OptionInclusion)
Inject a singleton script in a build. Intended for use in Options' PostprocessScene(Scene, OptionInclusion) methods.
Declaration
public static T InjectFeature<T>(Scene scene, OptionInclusion inclusion)
where T : Component
Parameters
Type | Name | Description |
---|---|---|
UnityEngine.SceneManagement.Scene | scene | Pass in the |
OptionInclusion | inclusion | Pass in the |
Returns
Type | Description |
---|---|
T | The script if it's injected or null |
Type Parameters
Name | Description |
---|---|
T |
Remarks
See GetSingleton<T>(Boolean) for a introductory explanation on how to implement the feature injection design pattern.
InjectFeature is used in conjunction with GetSingleton. It's needed in case a build includes an Option's associated feature but not the Option itself. If the Option is included, it can take care of injecting the feature at runtime in the build. But if only the feature is included, it needs to be injected at build-time.
This method should be used in an Option's PostprocessScene(Scene, OptionInclusion) to inject the feature into the build at build-time when required.
Example for a typical implementation:
protected bool Validate()
{
// Check if the Option is properly configured and/or enabled
return Value && !string.IsNullOrEmpty(GetChild<OptionChild>().Value);
}
#if UNITY_EDITOR
override public bool ShouldIncludeOnlyFeature()
{
// Removes the feature if it's improperly configured.
// Otherwise the feature will always be included even if misconfigured/disabled.
return Validate();
}
override public void PostprocessScene(Scene scene, OptionInclusion inclusion)
{
base.PostprocessScene(scene, inclusion);
var singleton = OptionHelper.InjectFeature<MyScript>(scene, inclusion);
if (singleton != null) {
singleton.option = false;
singleton.otherOption = GetChild<OptionChild>().Value;
}
}
#endif
Here overriding ShouldIncludeOnlyFeature takes care of checking if only the feature is included and removing it when it's not enabled/configured. InjectFeature then adds the singleton to the first scene so that it will be loaded in the build.
LoadAssetByGUID<T>(String)
Load an asset by its GUID.
Declaration
public static T LoadAssetByGUID<T>(string guid)
where T : Object
Parameters
Type | Name | Description |
---|---|---|
System.String | guid |
Returns
Type | Description |
---|---|
T | The object of given type in the asset with the given GUID or null if either no asset with this GUID exists or the asset does not contain an object of given type. |
Type Parameters
Name | Description |
---|---|
T |
RemovePluginFromBuild(String)
Declaration
public static void RemovePluginFromBuild(string guid)
Parameters
Type | Name | Description |
---|---|---|
System.String | guid |
RemovePluginsFromBuild(IEnumerable<String>)
Prevent plugins from being included in the build. This method should be called from an Option's PreprocessBuild(BuildTarget, String, OptionInclusion).
Declaration
public static void RemovePluginsFromBuild(IEnumerable<string> pluginGuids)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<System.String> | pluginGuids | The GUIDs of the plugins to exclude from the current build. |
Remarks
This method uses PluginImporter.SetIncludeInBuildDelegate
to prevent the plugins
from being included. This may interfere with other scripts using the same method.
RunScript(String, String)
Run an external process/script with given arguments and wait for it to exit.
Declaration
public static bool RunScript(string path, string arguments)
Parameters
Type | Name | Description |
---|---|---|
System.String | path | Path to the script (absolute, relative to project directory or on the PATH) |
System.String | arguments | Arguments to pass to the script |
Returns
Type | Description |
---|---|
System.Boolean |
|
RunScript(String, String, String, out String, out String)
Run an external process/script with given arguments, write the given input to its standard input, wait for it to exit and capture its output and/or errors.
Declaration
public static int RunScript(string path, string arguments, string input, out string output, out string error)
Parameters
Type | Name | Description |
---|---|---|
System.String | path | Path to the script (absolute, relative to project directory or on the PATH) |
System.String | arguments | Arguments to pass to the script |
System.String | input | Input written to the script's standard input (or null) |
System.String | output | The standard output of the script |
System.String | error | The standard error of the script |
Returns
Type | Description |
---|---|
System.Int32 | The exit code of the script. |
RunScript(String, String, out String)
Run an external process/script with given arguments, wait for it to exit and capture its output.
Declaration
public static bool RunScript(string path, string arguments, out string output)
Parameters
Type | Name | Description |
---|---|---|
System.String | path | Path to the script (absolute, relative to project directory or on the PATH) |
System.String | arguments | Arguments to pass to the script |
System.String | output | The standard output of the script |
Returns
Type | Description |
---|---|
System.Boolean |
|
RunScript(String, String, out String, out String)
Run an external process/script with given arguments, wait for it to exit and capture its output and/or errors.
Declaration
public static int RunScript(string path, string arguments, out string output, out string error)
Parameters
Type | Name | Description |
---|---|---|
System.String | path | Path to the script (absolute, relative to project directory or on the PATH) |
System.String | arguments | Arguments to pass to the script |
System.String | output | The standard output of the script |
System.String | error | The standard error of the script |
Returns
Type | Description |
---|---|
System.Int32 | The exit code of the script. |
RunScriptAsnyc(ProcessStartInfo, String, Action<String>, Action<String>, Action<Int32>)
Asynchronous RunScript method. Instead of blocking until the script is finished, this method takes three methods that are called with the output of the script as it's being read as well as one with the exit code once the script has finished.
Declaration
public static Action<bool> RunScriptAsnyc(ProcessStartInfo startInfo, string input, Action<string> onOutput, Action<string> onError, Action<int> onExit)
Parameters
Type | Name | Description |
---|---|---|
System.Diagnostics.ProcessStartInfo | startInfo | Process start configuration (note that UseShellExecute, RedirectStandardOutput and RedirectStandardError will be overwritten) |
System.String | input | Input written to the script's standard input (or null) |
System.Action<System.String> | onOutput | Method called with each output line |
System.Action<System.String> | onError | Method called with each error line |
System.Action<System.Int32> | onExit | Method called with the exit code |
Returns
Type | Description |
---|---|
System.Action<System.Boolean> | A callback that can be used to stop the script (parameter: false = terminate, true = kill) |
RunScriptAsnyc(String, String, String, Action<String>, Action<String>, Action<Int32>)
Asynchronous RunScript method. Instead of blocking until the script is finished, this method takes three methods that are called with the output of the script as it's being read as well as one with the exit code once the script has finished.
Declaration
public static Action<bool> RunScriptAsnyc(string path, string arguments, string input, Action<string> onOutput, Action<string> onError, Action<int> onExit)
Parameters
Type | Name | Description |
---|---|---|
System.String | path | Path to the script (absolute, relative to project directory or on the PATH) |
System.String | arguments | Arguments to pass to the script |
System.String | input | Input written to the script's standard input (or null) |
System.Action<System.String> | onOutput | Method called with each output line |
System.Action<System.String> | onError | Method called with each error line |
System.Action<System.Int32> | onExit | Method called with the exit code |
Returns
Type | Description |
---|---|
System.Action<System.Boolean> | A callback that can be used to stop the script (parameter: false = terminate, true = kill) |