Unity Components Extensions
Camera
GetSizeAtDistance
Returns the camera viewport for a distance (orthographic or perspective).
GetDistanceFromFrustumHeight
GetFieldOfView
RaycastPosition
Returns a point on a plane. (for example, projects the mouse position on a plane)
Vector3? mousePosition = camera.RaycastPosition(eventMousePosition, new Plane(Vector3.up, new Vector3(0f, 0f, 0f)));
// an horizontal grid
for (int x = 0; x < xsize; ++x) {
for (int z = 0; z < zsize; ++z) {
var hover = mousePosition.HasValue && new Rect(x, z, 1f, 1f).Contains(new Vector2(mousePosition.Value.x, mousePosition.Value.z));
...
}
}
TakeScreenshot
// take a screenshot
var texture = await GetComponent<Camera>().TakeScreenshot();
// compose a screenshot with multiple cameras.
var texture = await GetComponent<Camera>().TakeScreenshot(camera2, camera3, ...);
Color
HSBColor class
Common includes the HSBColor from unity wiki (http://wiki.unity3d.com/index.php?title=HSBColor)
ToHSB
var color = new Color();
var hsb = color.ToHSB();
GetHue
Alias of color.ToHSB().h
var hue = color.GetHue();
GetSaturation
Optimized if you want the saturation only
var sat = color.GetSaturation();
GetBrightness
Optimized if you want the brightness only
var bright = color.GetBrightness();
LayerMask
Contains
Test if a LayerMask contains a layer by id or name.
mylayerMask.Contains("default");
Animation
GetClip
Gets a clip by name. Returns the AnimationClip or null.
GetComponent<Animator>().GetClip("MyAnimationName");
GetClipDuration
alias of GetClip("MyAnimationName").length, returns the duration or 0
GetComponent<Animator>().GetClipDuration("MyAnimationName");
AnimatorRuntimeInfos
This component is required to call States methods.
Add an
AnimatorRuntimeInfos
in your GameObject.
HasState
var anim = GetComponent<Animator>();
if (anim.HasState("Jump")) {
anim.Play("Jump");
}
anim.HasStates("Jump"[, names]);
GetStateInfos
Returns informations of a state stored by AnimatorRuntimeInfos
component.
var anim = GetComponent<Animator>();
var jump = anim.GetStateInfos("Jump");
Debug.Log(jump.LayerId);
Debug.Log(jump.Id);
Debug.Log(jump.Name);
Debug.Log(jump.AnimationName);
anim.GetStatesInfos("Jump"[, names]);
GetStateDuration
Returns the motion duration in a state.
var anim = GetComponent<Animator>();
anim.GetStateDuration("Jump");
anim.GetStatesDuration("Jump"[, names]);
HasParameter
Uses AnimatorRuntimeInfos component to find parameter by name
var anim = GetComponent<Animator>();
anim.HasParameter("Jump");
anim.HasParameters("Jump"[, names]);