2025-07-11 21:00:00 +08:00
#if PRIME_TWEEN_INSTALLED
using JetBrains.Annotations ;
using PrimeTween ;
using UnityEngine ;
namespace PrimeTweenDemo {
public class HighlightedElementController : MonoBehaviour {
[SerializeField] Camera mainCamera ;
[SerializeField] CameraProjectionMatrixAnimation cameraProjectionMatrixAnimation ;
[CanBeNull] public HighlightableElement current { get ; private set ; }
2026-02-28 17:09:15 +08:00
#if UNITY_2019_1_OR_NEWER & & ! PHYSICS_MODULE_INSTALLED
2025-07-11 21:00:00 +08:00
void Awake ( ) {
2026-02-28 17:09:15 +08:00
Debug . LogError ( "Please install the package needed for Physics.Raycast(): 'Package Manager/Packages/Built-in/Physics' (com.unity.modules.physics)." ) ;
2025-07-11 21:00:00 +08:00
}
2026-02-28 17:09:15 +08:00
#endif
2025-07-11 21:00:00 +08:00
void Update ( ) {
if ( cameraProjectionMatrixAnimation . IsAnimating ) {
return ;
}
2026-02-28 17:09:15 +08:00
if ( Application . isMobilePlatform & & InputController . touchSupported & & ! InputController . Get ( ) ) {
2025-07-11 21:00:00 +08:00
SetCurrentHighlighted ( null ) ;
return ;
}
2026-02-28 17:09:15 +08:00
var screenPosition = InputController . screenPosition ;
if ( ! new Rect ( 0f , 0f , Screen . width , Screen . height ) . Contains ( screenPosition ) ) {
return ;
}
var ray = mainCamera . ScreenPointToRay ( screenPosition ) ;
2025-07-11 21:00:00 +08:00
var highlightableElement = RaycastHighlightableElement ( ray ) ;
SetCurrentHighlighted ( highlightableElement ) ;
if ( current ! = null & & InputController . GetDown ( ) ) {
current . GetComponent < Animatable > ( ) . OnClick ( ) ;
}
}
[CanBeNull]
static HighlightableElement RaycastHighlightableElement ( Ray ray ) {
#if ! UNITY_2019_1_OR_NEWER | | PHYSICS_MODULE_INSTALLED
// If you're seeing a compilation error on the next line, please install the package needed for Physics.Raycast(): 'Package Manager/Packages/Built-in/Physics' (com.unity.modules.physics).
return Physics . Raycast ( ray , out var hit ) ? hit . collider . GetComponentInParent < HighlightableElement > ( ) : null ;
#else
return null ;
#endif
}
void SetCurrentHighlighted ( [ CanBeNull ] HighlightableElement newHighlighted ) {
if ( newHighlighted ! = current ) {
if ( current ! = null ) {
AnimateHighlightedElement ( current , false ) ;
}
current = newHighlighted ;
if ( newHighlighted ! = null ) {
AnimateHighlightedElement ( newHighlighted , true ) ;
}
}
}
static readonly int emissionColorPropId = Shader . PropertyToID ( "_EmissionColor" ) ;
static void AnimateHighlightedElement ( [ NotNull ] HighlightableElement highlightable , bool isHighlighted ) {
Tween . LocalPositionZ ( highlightable . highlightAnchor , isHighlighted ? 0.08f : 0 , 0.3f ) ;
foreach ( var model in highlightable . models ) {
2026-02-28 17:09:15 +08:00
Tween . MaterialColor ( model . sharedMaterial , emissionColorPropId , isHighlighted ? Color . white * 0.25f : Color . black , 0.2f , Ease . OutQuad ) ;
2025-07-11 21:00:00 +08:00
}
}
}
}
#endif