Stardog

Unity 5 Modular Detector

Mar 29th, 2015
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class DetectorS : MonoBehaviour {
  6.  
  7.     [Header("Info")]
  8.     public List<GameObject> detected;
  9.  
  10.     [Header("Options")]
  11.     public List<string> tagsToDetect;
  12.  
  13.     // COMPONENTS
  14.     private SphereCollider sc;
  15.    
  16.     // EVENTS
  17.     public delegate void EventHandler(DetectorS detectorS, GameObject detectedGO);
  18.     public event EventHandler DetectedSomething, UndetectedSomething;
  19.  
  20.     void OnTriggerEnter(Collider collider)
  21.     {
  22.         if (!tagsToDetect.Contains(collider.tag)) return;
  23.  
  24.         Detect(collider.gameObject);
  25.     }
  26.  
  27.     void OnTriggerExit(Collider collider)
  28.     {
  29.         if (!tagsToDetect.Contains(collider.tag)) return;
  30.  
  31.         Undetect(collider.gameObject);
  32.     }
  33.  
  34.     void Detect(GameObject go)
  35.     {
  36.         if (detected.Contains(go)) return;
  37.  
  38.         // ADD
  39.         detected.Add(go);
  40.  
  41.         // EVENT
  42.         if (DetectedSomething != null) DetectedSomething(this, go);
  43.     }
  44.  
  45.     void Undetect(GameObject go)
  46.     {
  47.         if (!detected.Contains(go)) return;
  48.  
  49.         // REMOVE
  50.         detected.Remove(go);
  51.  
  52.         // EVENT
  53.         if (UndetectedSomething != null) UndetectedSomething(this, go);
  54.     }
  55.  
  56. }
Add Comment
Please, Sign In to add comment