OnionController.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using UnityEngine;
  3. namespace OnionSkin
  4. {
  5. public class OnionController : MonoBehaviour
  6. {
  7. private void OnDisable()
  8. {
  9. this._disappearTime = 1f;
  10. this._disappearTimer = 0f;
  11. this._meshFilter = null;
  12. this.tint = this._startColor;
  13. }
  14. private void OnEnable()
  15. {
  16. if (this._meshFilter != null)
  17. {
  18. this._meshFilter.sharedMesh = null;
  19. }
  20. if (this._meshFilter == null)
  21. {
  22. this._meshFilter = base.GetComponent<MeshFilter>();
  23. }
  24. this._startColor = this.tint;
  25. }
  26. private void Update()
  27. {
  28. if (this._materials == null)
  29. {
  30. return;
  31. }
  32. if (this._curve == null)
  33. {
  34. this._curve = AnimationCurve.Linear(0f, 0f, 1f, 1f);
  35. this._disappearTime = 1f;
  36. }
  37. if (this._scaleCurve == null)
  38. {
  39. this._scaleCurve = AnimationCurve.Linear(0f, 1f, 1f, 1f);
  40. }
  41. this._disappearTimer += Time.deltaTime;
  42. float num = Mathf.Clamp01(this._curve.Evaluate(this._disappearTimer / this._disappearTime));
  43. this.tint.a = this.tint.a * num;
  44. float d = Mathf.Clamp01(this._scaleCurve.Evaluate(this._disappearTimer / this._disappearTime));
  45. Vector3 localScale = this._startScale * d;
  46. base.transform.localScale = localScale;
  47. this.Refresh();
  48. if (this._disappearTimer > this._disappearTime)
  49. {
  50. EffectController.TerminateEffect(base.gameObject);
  51. }
  52. }
  53. public void Clone(GameObject game)
  54. {
  55. this._startScale = base.transform.localScale;
  56. Mesh mesh = game.GetComponent<MeshFilter>().mesh;
  57. this._materials = game.GetComponent<Renderer>().materials;
  58. base.GetComponent<Renderer>().materials = this._materials;
  59. if (this._meshFilter == null)
  60. {
  61. this._meshFilter = base.GetComponent<MeshFilter>();
  62. }
  63. this._meshFilter.mesh = UnityEngine.Object.Instantiate<Mesh>(mesh);
  64. for (int i = 0; i < this._materials.Length; i++)
  65. {
  66. this._materials[i].shader = this.shader;
  67. this._materials[i].SetFloat("_EmissionStrength", this.EmissionStrength);
  68. this._materials[i].SetColor("_Color", this.tint);
  69. }
  70. }
  71. public void Refresh()
  72. {
  73. for (int i = 0; i < this._materials.Length; i++)
  74. {
  75. this._materials[i].SetColor("_Color", this.tint);
  76. }
  77. }
  78. public void SetDisappear(AnimationCurve disappearCurve, float disappearTime)
  79. {
  80. this._curve = ((this._curve == null) ? AnimationCurve.Linear(0f, 1f, 1f, 0f) : disappearCurve);
  81. this._disappearTime = disappearTime;
  82. }
  83. public Color tint = new Color(0f, 0f, 0.5f, 0.5f);
  84. public float EmissionStrength;
  85. private MeshFilter _meshFilter;
  86. public Shader shader;
  87. private Material[] _materials;
  88. private AnimationCurve _curve;
  89. private AnimationCurve _scaleCurve;
  90. private Vector3 _startScale;
  91. private float _disappearTime;
  92. private float _disappearTimer;
  93. private Color _startColor;
  94. }
  95. }