Scripting
Objects and highlights
Highlight the object under the mouse
This usually needs no code: a Highlight Trigger in Pointer Hover mode does it. For a cursor of your own, switch the Highlight on and off yourself.
using PassForge.Effects;
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
// Highlights the object under the mouse, for a cursor of your own. The objects carry a Pass Forge Highlight that
// starts switched off, and a collider.
public class HoverHighlight : MonoBehaviour
{
[SerializeField] Camera m_Camera;
[SerializeField] float m_Distance = 200f;
Collider m_Over;
PassForgeHighlight m_Hovered;
void Update()
{
Collider over = null;
if (TryGetPointer(out Vector2 pointer)
&& Physics.Raycast(m_Camera.ScreenPointToRay(pointer), out RaycastHit hit, m_Distance))
over = hit.collider;
// Looks the highlight up only when the pointer moves onto another collider.
if (over == m_Over)
return;
m_Over = over;
var next = over != null ? over.GetComponentInParent<PassForgeHighlight>() : null;
if (next == m_Hovered)
return;
if (m_Hovered != null)
m_Hovered.enabled = false; // a highlight shows while it is enabled
m_Hovered = next;
if (m_Hovered != null)
m_Hovered.enabled = true;
}
// UnityEngine.Input throws in a project set to the Input System package only, so this reads either one.
static bool TryGetPointer(out Vector2 position)
{
#if ENABLE_INPUT_SYSTEM
var mouse = Mouse.current;
position = mouse != null ? mouse.position.ReadValue() : Vector2.zero;
return mouse != null;
#else
position = Input.mousePosition;
return Input.mousePresent;
#endif
}
}
Select and deselect from code
A Highlight Trigger in Pointer Click mode selects on a click; Select, Deselect and PassForgeHighlightTrigger.DeselectAll do it from code, and onClicked tells you about the click.
using PassForge.Effects;
using UnityEngine;
// Selection through Highlight Triggers in Pointer Click mode, from code.
public class UnitSelection : MonoBehaviour
{
[SerializeField] PassForgeHighlightTrigger m_Hero;
void OnEnable() => m_Hero.onClicked.AddListener(OnHeroClicked);
void OnDisable() => m_Hero.onClicked.RemoveListener(OnHeroClicked);
// The hero starts selected. With Only One Selected, selecting deselects the others.
void Start() => m_Hero.Select();
public void Deselect(PassForgeHighlightTrigger unit) => unit.Deselect();
// Such as on Escape or a click on empty ground.
public void ClearSelection() => PassForgeHighlightTrigger.DeselectAll();
void OnHeroClicked()
{
if (m_Hero.isSelected)
Debug.Log("Open the hero's panel here.");
}
}
Flash an enemy on hit
Flash(color, seconds) works whether the Highlight is on or off, so a switched off Highlight serves as a flash only.
using PassForge.Effects;
using UnityEngine;
// On an enemy with a Pass Forge Highlight. Switched off, the highlight serves as a flash only.
public class HitFlash : MonoBehaviour
{
[SerializeField] PassForgeHighlight m_Highlight;
// Flashing again restarts the flash. Alpha sets how strongly it covers the object; HDR colors feed bloom.
public void OnHit() => m_Highlight.Flash(Color.red, 0.15f);
}
Fill an object as it is captured
fillAmount grows the profile's Fill over this object only. The profile's Fill must be switched on.
using PassForge.Effects;
using UnityEngine;
// A capture point that fills while the player stands in its trigger. Its highlight profile has Fill switched on.
public class CapturePoint : MonoBehaviour
{
[SerializeField] PassForgeHighlight m_Highlight;
[SerializeField] float m_Seconds = 5f;
bool m_Inside;
float m_Progress;
void OnTriggerEnter(Collider other) { if (other.CompareTag("Player")) m_Inside = true; }
void OnTriggerExit(Collider other) { if (other.CompareTag("Player")) m_Inside = false; }
void Update()
{
if (!m_Inside || m_Progress >= 1f)
return;
m_Progress = Mathf.Min(1f, m_Progress + Time.deltaTime / m_Seconds);
// Changes this object only: the first time, it gets its own copy of the profile.
m_Highlight.fillAmount = m_Progress;
}
}
An enemy's death
A kill moment, the outline fading, a dissolve, then the object switched off once the Dissolve's onFinished says it is done.
using PassForge.Effects;
using UnityEngine;
// An enemy's death: a kill moment, the outline fading, a dissolve, then the object switched off for a pool.
public class EnemyDeath : MonoBehaviour
{
[SerializeField] PassForgeMoment m_Kill; // such as a white flash and a shockwave
[SerializeField] PassForgeHighlight m_Outline;
[SerializeField] PassForgeDissolve m_Dissolve;
bool m_Dead;
void OnEnable() => m_Dissolve.onFinished.AddListener(OnDissolved);
void OnDisable() => m_Dissolve.onFinished.RemoveListener(OnDissolved);
public void Die()
{
if (m_Dead)
return;
m_Dead = true;
m_Kill.Play();
m_Outline.FadeOut(0.25f); // so the outline does not trace a body that is dissolving
m_Dissolve.Dissolve(0.8f);
}
void OnDissolved()
{
if (m_Dead)
gameObject.SetActive(false); // which also takes it off the radar
}
// Back from the pool.
public void Respawn()
{
gameObject.SetActive(true);
m_Dead = false;
m_Dissolve.amount = 0f;
m_Outline.FadeIn(0f);
}
}
Move the Reveal Lens
A Reveal Lens with Placement set to Mouse Pointer follows the mouse with no code. With Screen Point, a script moves it, such as with a gamepad stick.
using PassForge.Effects;
using UnityEngine;
// Under the mouse needs no code: a Reveal Lens with Placement set to Mouse Pointer follows the mouse or the first
// touch. This moves the lens with a gamepad stick instead, and goes back to the mouse.
public class LensCursor : MonoBehaviour
{
[SerializeField] PassForgeRevealLens m_Lens;
[SerializeField] float m_Speed = 0.6f; // screens a second
Vector2 m_Stick;
public void UseMouse() => m_Lens.placement = RevealLensPlacement.MousePointer;
// Call it from your input code with the stick's value.
public void Move(Vector2 stick)
{
m_Stick = stick;
if (stick != Vector2.zero)
m_Lens.placement = RevealLensPlacement.ScreenPoint;
}
void Update()
{
if (m_Lens.placement != RevealLensPlacement.ScreenPoint || m_Stick == Vector2.zero)
return;
// The screen point runs from 0 to 1 across the screen. Real time, so it moves in a pause menu too.
var point = m_Lens.screenPoint + m_Stick * (m_Speed * Time.unscaledDeltaTime);
m_Lens.screenPoint = new Vector2(Mathf.Clamp01(point.x), Mathf.Clamp01(point.y));
}
}