// OnTriggerExit is called when the Collider other has stopped touching the trigger public void OnTriggerExit(Collider other) { // Retrieve all the renderers Renderer[] rendererWeHit = other.gameObject.GetComponentsInChildren <Renderer>(); List <int> renderersToRestore = new List <int>(); for (int idx = 0; idx < rendererWeHit.Length; idx++) { int key = rendererWeHit[idx].GetInstanceID(); if (modifiedShaders.ContainsKey(key)) { renderersToRestore.Add(key); } } for (int i = 0; i < renderersToRestore.Count; i++) { ShaderData sd = modifiedShaders[renderersToRestore[i]]; modifiedShaders.Remove(renderersToRestore[i]); for (int j = 0; j < sd.renderer.materials.Length; j++) { sd.renderer.materials[j].shader = sd.shader[j]; if (sd.renderer.materials[j].HasProperty("_Color")) { sd.renderer.materials[j].color = sd.color[j]; } } } }
// OnTriggerEnter is called when the Collider other enters the trigger public void OnTriggerEnter(Collider other) { int objLayer = 1 << other.gameObject.layer; if (other.isTrigger && ignoreTriggers || other.CompareTag(playerTag) || (layersToFade & objLayer) != objLayer) return; // Retrieve all the renderers Renderer[] rendererWeHit = other.gameObject.GetComponentsInChildren<Renderer>(); // Loop through the renderers for (int idx = 0; idx < rendererWeHit.Length; idx++) { if (rendererWeHit[idx] != null) // just to be on the safe side :) { // If we changed this already we skip it, otherwise we proceed with the change if (!modifiedShaders.ContainsKey(rendererWeHit[idx].GetInstanceID())) { ShaderData shaderData = new ShaderData(); shaderData.renderer = rendererWeHit[idx]; shaderData.shader = new Shader[rendererWeHit[idx].materials.Length]; shaderData.color = new Color[rendererWeHit[idx].materials.Length]; for (int j = 0; j < rendererWeHit[idx].materials.Length; j++) { shaderData.shader[j] = rendererWeHit[idx].materials[j].shader; if (rendererWeHit[idx].materials[j].HasProperty("_Color")) shaderData.color[j] = rendererWeHit[idx].materials[j].color; rendererWeHit[idx].materials[j].shader = transparentShader; rendererWeHit[idx].materials[j].color = fadingColorToUse; } // Add the shader to the list of those that have been changed modifiedShaders.Add(rendererWeHit[idx].GetInstanceID(), shaderData); } } } }
// OnTriggerEnter is called when the Collider other enters the trigger public void OnTriggerEnter(Collider other) { int objLayer = 1 << other.gameObject.layer; if (other.isTrigger && ignoreTriggers || other.CompareTag(playerTag) || (layersToFade & objLayer) != objLayer) { return; } // Retrieve all the renderers Renderer[] rendererWeHit = other.gameObject.GetComponentsInChildren <Renderer>(); // Loop through the renderers for (int idx = 0; idx < rendererWeHit.Length; idx++) { if (rendererWeHit[idx] != null) // just to be on the safe side :) { // If we changed this already we skip it, otherwise we proceed with the change if (!modifiedShaders.ContainsKey(rendererWeHit[idx].GetInstanceID())) { ShaderData shaderData = new ShaderData(); FadingManager fade = rendererWeHit[idx].gameObject.GetComponent <FadingManager>(); if (fade != null) { fade.GoAway(); } shaderData.renderer = rendererWeHit[idx]; shaderData.materials = rendererWeHit[idx].materials; Material[] tmpMats = rendererWeHit[idx].materials; shaderData.color = new Color[rendererWeHit[idx].materials.Length]; for (int j = 0; j < tmpMats.Length; j++) { shaderData.color[j] = tmpMats[j].color; tmpMats[j] = new Material(transparentMaterial); tmpMats[j].color = fadingColorToUse; if (replicateTexture) { tmpMats[j].mainTexture = rendererWeHit[idx].materials[j].mainTexture; } else { tmpMats[j].mainTexture = null; } } rendererWeHit[idx].materials = tmpMats; // Add the shader to the list of those that have been changed modifiedShaders.Add(rendererWeHit[idx].GetInstanceID(), shaderData); fade = rendererWeHit[idx].gameObject.AddComponent <FadingManager>(); fade.fadingTime = fadingTime; fade.fadingAmount = transparenceValue; } } } }
// OnTriggerEnter is called when the Collider other enters the trigger public void OnTriggerEnter(Collider other) { int objLayer = 1 << other.gameObject.layer; if (other.isTrigger && ignoreTriggers || other.CompareTag(playerTag) || (layersToFade & objLayer) != objLayer) { return; } // Retrieve all the renderers Renderer[] rendererWeHit = other.gameObject.GetComponentsInChildren <Renderer>(); // Loop through the renderers for (int idx = 0; idx < rendererWeHit.Length; idx++) { if (rendererWeHit[idx] != null) // just to be on the safe side :) { // If we changed this already we skip it, otherwise we proceed with the change if (!modifiedShaders.ContainsKey(rendererWeHit[idx].GetInstanceID())) { ShaderData shaderData = new ShaderData(); shaderData.renderer = rendererWeHit[idx]; shaderData.shader = new Shader[rendererWeHit[idx].materials.Length]; shaderData.color = new Color[rendererWeHit[idx].materials.Length]; for (int j = 0; j < rendererWeHit[idx].materials.Length; j++) { shaderData.shader[j] = rendererWeHit[idx].materials[j].shader; if (rendererWeHit[idx].materials[j].HasProperty("_Color")) { shaderData.color[j] = rendererWeHit[idx].materials[j].color; } rendererWeHit[idx].materials[j].shader = transparentShader; rendererWeHit[idx].materials[j].color = fadingColorToUse; } // Add the shader to the list of those that have been changed modifiedShaders.Add(rendererWeHit[idx].GetInstanceID(), shaderData); } } } }
// OnTriggerExit is called when the Collider other has stopped touching the trigger public void OnTriggerExit(Collider other) { // Retrieve all the renderers Renderer[] rendererWeHit = other.gameObject.GetComponentsInChildren <Renderer>(); List <int> renderersToRestore = new List <int>(); for (int idx = 0; idx < rendererWeHit.Length; idx++) { int key = rendererWeHit[idx].GetInstanceID(); if (modifiedShaders.ContainsKey(key)) { renderersToRestore.Add(key); } } for (int i = 0; i < renderersToRestore.Count; i++) { ShaderData sd = modifiedShaders[renderersToRestore[i]]; modifiedShaders.Remove(renderersToRestore[i]); for (int m = 0; m < sd.materials.Length; m++) { FadingManager fade = sd.renderer.gameObject.GetComponent <FadingManager>(); if (fade != null) { fade.GoAway(); } fade = sd.renderer.gameObject.AddComponent <FadingManager>(); fade.fadingTime = fadingTime; fade.fadingAmount = transparenceValue; fade.fadeOut = false; fade.matIdx = m; fade.oldMat = sd.materials[m]; fade.oldColor = sd.color[m]; } } Resources.UnloadUnusedAssets(); }
// This function is called every fixed framerate frame, if the MonoBehaviour is enabled private void FixedUpdate() { if (playerTransform == null) // Do nothing if we have no target return; // Let's retrieve all the objects in the way of the camera #if UNITY_5 RaycastHit[] hit = Physics.RaycastAll(myTransform.position, myTransform.forward, (playerTransform.position - myTransform.position).magnitude + offset, layersToFade, ignoreTriggers ? QueryTriggerInteraction.Ignore : QueryTriggerInteraction.Collide); #else RaycastHit[] hit = Physics.RaycastAll(myTransform.position, myTransform.forward, (playerTransform.position - myTransform.position).magnitude + offset, layersToFade); #endif Debug.DrawLine(myTransform.position, playerTransform.position + (myTransform.forward * offset), fadingColorToUse, Time.fixedDeltaTime); List<int> renderersIdsHitInThisFrame = new List<int>(); if (hit != null) { // Parse all objects we hit for (int i = 0; i < hit.Length; i++) { #if !UNITY_5 if (hit[i].collider.isTrigger && ignoreTriggers) continue; #endif // Ignore the player :) if (!hit[i].collider.CompareTag(playerTag)) { // Retrieve all the renderers Renderer[] rendererWeHit = hit[i].collider.gameObject.GetComponentsInChildren<Renderer>(); // Loop through the renderers for (int idx = 0; idx < rendererWeHit.Length; idx++) { if (rendererWeHit[idx] != null) // just to be on the safe side :) { // Store the render's unique Id among those hit during the current frame renderersIdsHitInThisFrame.Add(rendererWeHit[idx].GetInstanceID()); // If we changed this alreadu we skip it, otherwise we proceed with // the change if (!modifiedShaders.ContainsKey(rendererWeHit[idx].GetInstanceID())) { ShaderData shaderData = new ShaderData(); shaderData.renderer = rendererWeHit[idx]; shaderData.shader = new Shader[rendererWeHit[idx].materials.Length]; shaderData.color = new Color[rendererWeHit[idx].materials.Length]; for (int j = 0; j < rendererWeHit[idx].materials.Length; j++) { shaderData.shader[j] = rendererWeHit[idx].materials[j].shader; if (rendererWeHit[idx].materials[j].HasProperty("_Color")) shaderData.color[j] = rendererWeHit[idx].materials[j].color; rendererWeHit[idx].materials[j].shader = transparentShader; rendererWeHit[idx].materials[j].color = fadingColorToUse; } // Add the shader to the list of those that have been changed modifiedShaders.Add(rendererWeHit[idx].GetInstanceID(), shaderData); } } } } } } // Now let's restore those shaders that we changed but now they are no longer in the way List<int> renderersToRestore = new List<int>(); foreach (KeyValuePair<int, ShaderData> elemento in modifiedShaders) { if (!renderersIdsHitInThisFrame.Contains(elemento.Key)) renderersToRestore.Add(elemento.Key); } for (int i = 0; i < renderersToRestore.Count; i++) { ShaderData sd = modifiedShaders[renderersToRestore[i]]; modifiedShaders.Remove(renderersToRestore[i]); for (int j = 0; j < sd.renderer.materials.Length; j++) { sd.renderer.materials[j].shader = sd.shader[j]; if (sd.renderer.materials[j].HasProperty("_Color")) sd.renderer.materials[j].color = sd.color[j]; } } }
// This function is called every fixed framerate frame, if the MonoBehaviour is enabled private void FixedUpdate() { if (playerTransform == null) // Do nothing if we have no target { return; } // Let's retrieve all the objects in the way of the camera #if UNITY_5 RaycastHit[] hit = Physics.RaycastAll(myTransform.position, myTransform.forward, (playerTransform.position - myTransform.position).magnitude + offset, layersToFade, ignoreTriggers ? QueryTriggerInteraction.Ignore : QueryTriggerInteraction.Collide); #else RaycastHit[] hit = Physics.RaycastAll(myTransform.position, myTransform.forward, (playerTransform.position - myTransform.position).magnitude + offset, layersToFade); #endif Debug.DrawLine(myTransform.position, playerTransform.position + (myTransform.forward * offset), fadingColorToUse, Time.fixedDeltaTime); List <int> renderersIdsHitInThisFrame = new List <int>(); if (hit != null) { // Parse all objects we hit for (int i = 0; i < hit.Length; i++) { #if !UNITY_5 if (hit[i].collider.isTrigger && ignoreTriggers) { continue; } #endif // Ignore the player :) if (!hit[i].collider.CompareTag(playerTag)) { // Retrieve all the renderers Renderer[] rendererWeHit = hit[i].collider.gameObject.GetComponentsInChildren <Renderer>(); // Loop through the renderers for (int idx = 0; idx < rendererWeHit.Length; idx++) { if (rendererWeHit[idx] != null) // just to be on the safe side :) { // Store the render's unique Id among those hit during the current frame renderersIdsHitInThisFrame.Add(rendererWeHit[idx].GetInstanceID()); // If we changed this alreadu we skip it, otherwise we proceed with // the change if (!modifiedShaders.ContainsKey(rendererWeHit[idx].GetInstanceID())) { ShaderData shaderData = new ShaderData(); shaderData.renderer = rendererWeHit[idx]; shaderData.shader = new Shader[rendererWeHit[idx].materials.Length]; shaderData.color = new Color[rendererWeHit[idx].materials.Length]; for (int j = 0; j < rendererWeHit[idx].materials.Length; j++) { shaderData.shader[j] = rendererWeHit[idx].materials[j].shader; if (rendererWeHit[idx].materials[j].HasProperty("_Color")) { shaderData.color[j] = rendererWeHit[idx].materials[j].color; } rendererWeHit[idx].materials[j].shader = transparentShader; rendererWeHit[idx].materials[j].color = fadingColorToUse; } // Add the shader to the list of those that have been changed modifiedShaders.Add(rendererWeHit[idx].GetInstanceID(), shaderData); } } } } } } // Now let's restore those shaders that we changed but now they are no longer in the way List <int> renderersToRestore = new List <int>(); foreach (KeyValuePair <int, ShaderData> elemento in modifiedShaders) { if (!renderersIdsHitInThisFrame.Contains(elemento.Key)) { renderersToRestore.Add(elemento.Key); } } for (int i = 0; i < renderersToRestore.Count; i++) { ShaderData sd = modifiedShaders[renderersToRestore[i]]; modifiedShaders.Remove(renderersToRestore[i]); for (int j = 0; j < sd.renderer.materials.Length; j++) { sd.renderer.materials[j].shader = sd.shader[j]; if (sd.renderer.materials[j].HasProperty("_Color")) { sd.renderer.materials[j].color = sd.color[j]; } } } }
// This function is called every fixed framerate frame, if the MonoBehaviour is enabled private void FixedUpdate() { if (playerTransform == null) // Do nothing if we have no target { return; } // Let's retrieve all the objects in the way of the camera #if UNITY_5_0 || UNITY_5_1 if (!useSpherecast) { hit = Physics.RaycastAll(myTransform.position, myTransform.forward, (playerTransform.position - myTransform.position).magnitude + offset, layersToFade); } else { hit = Physics.SphereCastAll(myTransform.position, spherecastRadius, myTransform.forward, (playerTransform.position - myTransform.position).magnitude + offset, layersToFade); } #else if (!useSpherecast) { hit = Physics.RaycastAll(myTransform.position, myTransform.forward, (playerTransform.position - myTransform.position).magnitude + offset, layersToFade, ignoreTriggers ? QueryTriggerInteraction.Ignore : QueryTriggerInteraction.Collide); } else { hit = Physics.SphereCastAll(myTransform.position, spherecastRadius, myTransform.forward, (playerTransform.position - myTransform.position).magnitude + offset, layersToFade, ignoreTriggers ? QueryTriggerInteraction.Ignore : QueryTriggerInteraction.Collide); } #endif Debug.DrawLine(myTransform.position, playerTransform.position + (myTransform.forward * offset), fadingColorToUse, Time.fixedDeltaTime); List <int> renderersIdsHitInThisFrame = new List <int>(); if (hit != null) { // Parse all objects we hit for (int i = 0; i < hit.Length; i++) { if (hit[i].collider.isTrigger && ignoreTriggers) { continue; } // Ignore the player :) if (!(hit[i].collider.CompareTag(playerTag) || hit[i].collider.CompareTag("NoFade"))) { // Retrieve all the renderers Renderer[] rendererWeHit = hit[i].collider.gameObject.GetComponentsInChildren <Renderer>(); // Loop through the renderers for (int idx = 0; idx < rendererWeHit.Length; idx++) { if (rendererWeHit[idx] != null) // just to be on the safe side :) { // Store the render's unique Id among those hit during the current frame renderersIdsHitInThisFrame.Add(rendererWeHit[idx].GetInstanceID()); // If we changed this already we skip it, otherwise we proceed with // the change if (!modifiedShaders.ContainsKey(rendererWeHit[idx].GetInstanceID())) { ShaderData shaderData = new ShaderData(); FadingManager fade = rendererWeHit[idx].gameObject.GetComponent <FadingManager>(); if (fade != null) { fade.GoAway(); } shaderData.renderer = rendererWeHit[idx]; shaderData.materials = rendererWeHit[idx].materials; Material[] tmpMats = rendererWeHit[idx].materials; shaderData.color = new Color[rendererWeHit[idx].materials.Length]; for (int j = 0; j < tmpMats.Length; j++) { shaderData.color[j] = tmpMats[j].color; tmpMats[j] = transparentMaterial; tmpMats[j].color = fadingColorToUse; if (replicateTexture) { tmpMats[j].mainTexture = rendererWeHit[idx].materials[j].mainTexture; } else { tmpMats[j].mainTexture = null; } } rendererWeHit[idx].materials = tmpMats; // Add the shader to the list of those that have been changed modifiedShaders.Add(rendererWeHit[idx].GetInstanceID(), shaderData); fade = rendererWeHit[idx].gameObject.AddComponent <FadingManager>(); fade.fadingTime = fadingTime; fade.fadingAmount = transparenceValue; } } } } } } // Now let's restore those shaders that we changed but now they are no longer in the way List <int> renderersToRestore = new List <int>(); foreach (KeyValuePair <int, ShaderData> elemento in modifiedShaders) { if (!renderersIdsHitInThisFrame.Contains(elemento.Key)) { renderersToRestore.Add(elemento.Key); } } for (int i = 0; i < renderersToRestore.Count; i++) { ShaderData sd = modifiedShaders[renderersToRestore[i]]; modifiedShaders.Remove(renderersToRestore[i]); for (int m = 0; m < sd.materials.Length; m++) { FadingManager fade = sd.renderer.gameObject.GetComponent <FadingManager>(); if (fade != null) { fade.GoAway(); } fade = sd.renderer.gameObject.AddComponent <FadingManager>(); fade.fadingTime = fadingTime; fade.fadingAmount = transparenceValue; fade.fadeOut = false; fade.matIdx = m; fade.oldMat = sd.materials[m]; fade.oldColor = sd.color[m]; } } Resources.UnloadUnusedAssets(); }