-
Notifications
You must be signed in to change notification settings - Fork 1
Extensions
Namespace: WIDVE.Utilities
Location: WIDVE Unity Scripts/Utilities/Extensions
The Extensions folder contains various scripts with extensions methods for existing Unity scripts and components. When the WIDVE.Utilities
namespace is included in another script, all extension methods in the Extensions scripts will be available for use.
For example, to use the IsSelected()
method in GameObjectExtensions
, simply call gameObject.IsSelected()
on an existing game object. See the C# Extension Methods page for full details.
The SetDirty()
method can be used with the Component
, GameObject
, and UnityEngine.Object
classes. These classes cover the majority of objects that will exist in a scene. The SetDirty()
method is shorthand for calling EditorUtility.SetDirty()
on the given object. This is necessary when using a script to modify an object's data during edit mode. If SetDirty()
is not called, the editor won't know that the object has been modified, and any changes made through script will not be saved.
The MarkSceneDirty()
method can be used with the Component
and GameObject
classes. It is similar to SetDirty()
, but marks the scene containing the object as dirty rather than the object itself. If the object is part of a prefab asset rather than a scene, the prefab asset is marked as dirty. MarkSceneDirty()
should be used in many of the same circumstances as SetDirty()
. It can take some trial and error to figure out which method to use to force Unity to save any modifications that have been made, but it is perfectly fine to call both methods.
The InstantiatePrefab()
method can be used with the Transform
and GameObject
classes. It serves as a shorthand for spawning prefabs with a few extra options. It can be called in both edit mode and play mode. Prefabs will be spawned as children of the calling Transform
or GameObject
.
Argument | Description |
---|---|
keepPrefabConnection |
If true , the spawned object keeps a connection to the base prefab (it will show up blue in the editor). If false , or when this function is called outside the editor, the object will have no prefab connection. |
keepPrefabTransform |
If true , the spawned object will have its local transform keep the same values present in the base prefab. If false , the object's local transform may be changed based on the spawning parent object. |
Returns true
if the game object is part of the current selection, false
otherwise.
Returns true
if the game object is the currently selected object (visible in the inspector), false
otherwise.
Returns true
if the game object is part of a scene. Returns false
when the game object is part of a prefab asset or part of a temporary Unity import step. This method is useful when running logic during OnValidate()
. OnValidate()
runs when an object is selected in the editor, but also whenever prefabs are saved and in many other instances that are not fully documented. If complex logic has to be run during OnValidate()
, it should only run if ExistsInScene()
is true
; otherwise, it will run at many other times and greatly slow down the project.