TransparentWithDepth.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. using UnityEditor;
  6. namespace HighlightPlus {
  7. public class TransparentWithDepth {
  8. static Material bmDepthOnly;
  9. [MenuItem ("GameObject/Effects/Highlight Plus/Add Depth To Transparent Object", false, 100)]
  10. static void AddDepthOption () {
  11. Renderer renderer = GetRenderer ();
  12. if (renderer == null)
  13. return;
  14. if (!EditorUtility.DisplayDialog ("Add Depth To Transparent Object", "This option will force the transparent object to write to the depth buffer by adding a new special material to the renderer (existing materials are preserved) so it can occlude and allow See-Through effect.\nOnly use on transparent objects.\n\nProceed?", "Yes", "No")) {
  15. return;
  16. }
  17. Material[] materials = renderer.sharedMaterials;
  18. for (int k = 0; k < materials.Length; k++) {
  19. if (materials [k] == bmDepthOnly) {
  20. EditorUtility.DisplayDialog ("Depth Support", "Already set! Nothing to do.", "Ok");
  21. return;
  22. }
  23. }
  24. if (materials == null) {
  25. renderer.sharedMaterial = bmDepthOnly;
  26. } else {
  27. List<Material> newMaterials = new List<Material> (materials);
  28. newMaterials.Insert (0, bmDepthOnly);
  29. renderer.sharedMaterials = newMaterials.ToArray ();
  30. }
  31. }
  32. [MenuItem ("GameObject/Effects/Highlight Plus/Remove Depth Compatibility", false, 101)]
  33. static void RemoveDepthOption () {
  34. Renderer renderer = GetRenderer ();
  35. if (renderer == null)
  36. return;
  37. Material[] materials = renderer.sharedMaterials;
  38. for (int k = 0; k < materials.Length; k++) {
  39. if (materials [k] == bmDepthOnly) {
  40. List<Material> newMaterials = new List<Material> (renderer.sharedMaterials);
  41. newMaterials.RemoveAt (k);
  42. renderer.sharedMaterials = newMaterials.ToArray ();
  43. return;
  44. }
  45. }
  46. for (int k = 0; k < materials.Length; k++) {
  47. if (materials [k] == bmDepthOnly) {
  48. EditorUtility.DisplayDialog ("Depth Support", "This object was not previously modified! Nothing to do.", "Ok");
  49. return;
  50. }
  51. }
  52. }
  53. static Renderer GetRenderer () {
  54. if (Selection.activeGameObject == null) {
  55. EditorUtility.DisplayDialog ("Depth Support", "This option can only be used on GameObjects.", "Ok");
  56. return null;
  57. }
  58. Renderer renderer = Selection.activeGameObject.GetComponent<Renderer> ();
  59. if (renderer == null) {
  60. EditorUtility.DisplayDialog ("Depth Support", "This option can only be used on GameObjects with a Renderer component attached.", "Ok");
  61. return null;
  62. }
  63. if (bmDepthOnly == null) {
  64. bmDepthOnly = Resources.Load<Material> ("HighlightPlus/HighlightPlusDepthWrite");
  65. if (bmDepthOnly == null) {
  66. EditorUtility.DisplayDialog ("Depth Support", "HighlightPlusDepthWrite material not found!", "Ok");
  67. return null;
  68. }
  69. }
  70. return renderer;
  71. }
  72. }
  73. }