64 lines
1.6 KiB
C#
64 lines
1.6 KiB
C#
|
using UnityEngine;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
|
||
|
public class OutlineEffectController : MonoBehaviour
|
||
|
{
|
||
|
public float radius = 5f;
|
||
|
public float effectDuration = 3f;
|
||
|
public LayerMask outlineLayer;
|
||
|
|
||
|
private List<Renderer> affectedRenderers = new List<Renderer>();
|
||
|
|
||
|
private bool isEffectActive = false;
|
||
|
|
||
|
void Update()
|
||
|
{
|
||
|
if (Input.GetKeyDown(KeyCode.C))
|
||
|
{
|
||
|
if (!isEffectActive)
|
||
|
{
|
||
|
StartCoroutine(ActivateOutlineEffect());
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
IEnumerator ActivateOutlineEffect()
|
||
|
{
|
||
|
isEffectActive = true;
|
||
|
ApplyOutline();
|
||
|
yield return new WaitForSeconds(effectDuration);
|
||
|
RemoveOutline();
|
||
|
isEffectActive = false;
|
||
|
}
|
||
|
|
||
|
void ApplyOutline()
|
||
|
{
|
||
|
Collider[] hitColliders = Physics.OverlapSphere(transform.position, radius);
|
||
|
foreach (Collider col in hitColliders)
|
||
|
{
|
||
|
if (((1 << col.gameObject.layer) & outlineLayer.value) != 0)
|
||
|
{
|
||
|
Renderer renderer = col.GetComponent<Renderer>();
|
||
|
if (renderer != null && !affectedRenderers.Contains(renderer))
|
||
|
{
|
||
|
affectedRenderers.Add(renderer);
|
||
|
renderer.enabled = true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void RemoveOutline()
|
||
|
{
|
||
|
foreach (Renderer renderer in affectedRenderers)
|
||
|
{
|
||
|
if (renderer != null)
|
||
|
{
|
||
|
renderer.enabled = false;
|
||
|
}
|
||
|
}
|
||
|
affectedRenderers.Clear();
|
||
|
}
|
||
|
}
|