-
Notifications
You must be signed in to change notification settings - Fork 0
Utility Functions
See Assets/Objects/Util for utility function definitions.
You can derive SingletonBehaviour<MyClass>
instead of MonoBehaviour
if you want a singleton, accessible statically through SingleTonBehaviour<T>.Instance
C# extensions allow you to write "custom member functions" (Visual Studio has a slightly different icon for them, but they are used all the time in Unity APIs). These are useful for general-purpose extensions to existing classes and is better than using static functions (syntactically, Util.ProjectXZ(vector)
is uglier than vector.ProjectXZ()
).
Right now, we have extensions for checking whether a LayerMask
contains a layer (LayerMask.Contains(int layer)
), and an extension for projecting a vector to the XZ plane (Vector3.ProjectXZ()
). Please add any general-purpose extensions to this file.
This is a really cool set of functions that lets you find Objects in the Editor. The main reason you want to find Objects in the editor is so that when you add a component/create a ScriptableObject, its values default to something custom using the void Reset()
function. Basically, any time you have a serialized field for a component and you know (or could guess) where that component is, use Reset
to try to find it automatically. For example, if you have a custom text box with a TMP_Text tmp
component that is in one of the children, you can do
void Reset() {
tmp = GetComponentInChildren<TMP_Text>();
}
to automatically try to find that tmp
so you don't have to assign it manually in the editor. This gets very useful when you have a lot of serialized fields (see FishingRod).
However, for more complex things like if there are two TMP_Text
components in the children and you want to select the one whose GameObject includes the word "label" (case-insensitive), this gets a bit trickier.
FindUtil.Query<Component>(MonoBehaviour thisComponent)
makes this easier, which is mainly a simple API to GetComponent*
functions. (Note, I think you can also use the search API to do this but whatever). So, to do what I said above, it would be:
void Reset(){
tmp = FindUtil.Query<TMP_Text>(this)
.InChildren
.NameContains("label", insensitive: true)
.Execute();
}
However, it also has other utilities. To find a singleton asset (like GlobalParametersSO
), you can do:
void Reset(){
parameters = FindUtil.Asset<GlobalParametersSO>();
}
And to do a more complex search for an asset (eg. FishSO finding Prefabs in the same directory with a specific name):
void Reset()
{
InHandPrefab = FindUtil.QueryAsset<GameObject>()
.SiblingTo(this)
.NameContains("hand", insensitive: true)
.Execute();
}