daily pastebin goal
30%
SHARE
TWEET

Untitled

a guest Feb 17th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5.  
  6. public class ObjectFader : MonoBehaviour
  7. {
  8.  
  9.     Transform myTransform;
  10.     [SerializeField] Transform Player;
  11.  
  12.     [SerializeField] LayerMask ignoredLayer;
  13.  
  14.     class RendererProperties
  15.     {
  16.         public RendererProperties()
  17.         { }
  18.  
  19.         public RendererProperties(Renderer r)
  20.         {
  21.             renderer = r;
  22.             justAdded = true;
  23.         }
  24.  
  25.         public Renderer renderer;
  26.         public bool justAdded;
  27.     }
  28.  
  29.     List<RendererProperties> faded_objects = new List<RendererProperties>();
  30.  
  31.     // Start is called before the first frame update
  32.     void Start()
  33.     {
  34.         myTransform = transform;
  35.     }
  36.  
  37.     private void FixedUpdate()
  38.     {
  39.         Vector3 dir = Player.position - myTransform.position;
  40.         float distance = dir.magnitude;
  41.         RaycastHit[] hits = Physics.RaycastAll(myTransform.position, dir.normalized, distance, ~ignoredLayer);
  42.  
  43.         foreach (var i in faded_objects)
  44.         {
  45.             i.justAdded = false;
  46.         }
  47.  
  48.         if (hits.Length > 0)
  49.         {
  50.             FadeObjects(hits);
  51.         }
  52.  
  53.         RestoreObjects();
  54.  
  55.         Debug.DrawRay(myTransform.position, dir, Color.red);
  56.     }
  57.  
  58.  
  59.     void FadeObjects(RaycastHit[] hits)
  60.     {
  61.         foreach (var hit in hits)
  62.         {
  63.             Renderer r = hit.transform.GetComponent<Renderer>();
  64.             if (r == null) continue; // questo oggetto non e' renderizzato quindi non serve processarlo
  65.  
  66.             bool found = false;
  67.             foreach (var rp in faded_objects)
  68.             {
  69.                 if (rp.renderer == r)
  70.                 {
  71.                     rp.justAdded = true;
  72.                     found = true;
  73.                     break;
  74.                 }
  75.             }
  76.             if (!found)
  77.             {
  78.                 faded_objects.Add(new RendererProperties(r));
  79.                 r.enabled = false;
  80.                 Debug.Log("Added " + r.transform.name);
  81.             }
  82.            
  83.         }
  84.     }
  85.  
  86.     void RestoreObjects()
  87.     {
  88.         List<RendererProperties> remove = new List<RendererProperties>();
  89.  
  90.         foreach (var i in faded_objects)
  91.         {
  92.             if (!i.justAdded)
  93.             {
  94.                 remove.Add(i);
  95.                 i.renderer.enabled = true;
  96.             }
  97.         }
  98.  
  99.         for (int i = 0; i < remove.Count; i++)
  100.         {
  101.             faded_objects.Remove(remove[i]);
  102.             Debug.Log("Removed " + remove[i].renderer.transform.name);
  103.         }
  104.     }
  105.    
  106. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top