iTweenAlphaTo.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Module Name: iTweenAlphaTo.cs
  2. // Project: iTween Editor br Vortex Game Studios
  3. // Version: 1.00.00
  4. // Developed by: Alexandre Ribeiro de Sá (@themonkeytail)
  5. // Copyright(c) Vortex Game Studios LTDA ME.
  6. // http://www.vortexstudios.com
  7. //
  8. // iTween Alpha To component
  9. // Use this component to create alpha tween from your component.
  10. // 1.00.00 - First build
  11. //
  12. // Check every tools and plugins we made for Unity at
  13. // https://www.assetstore.unity3d.com/en/publisher/4888
  14. //
  15. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  16. // EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  17. // WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  18. using UnityEngine;
  19. using System.Collections;
  20. using UnityEngine.Events;
  21. using System;
  22. using UnityEngine.UI;
  23. public class iTweenAlphaTo : iTweenEditor {
  24. public float valueFrom = 0.0f;
  25. public float valueTo = 1.0f;
  26. [System.Serializable]
  27. public class OnStart : UnityEvent { };
  28. public OnStart onStart;
  29. [System.Serializable]
  30. public class OnComplete : UnityEvent { };
  31. public OnComplete onComplete;
  32. private SpriteRenderer _spriteRenderer = null;
  33. private Image _uiImage = null;
  34. private RawImage _uiRawImage = null;
  35. private CanvasGroup _uiCanvasGroup = null;
  36. // Use this for initialization
  37. void Awake() {
  38. _spriteRenderer = GetComponent<SpriteRenderer>();
  39. _uiImage = GetComponent<Image>();
  40. _uiRawImage = GetComponent<RawImage>();
  41. _uiCanvasGroup = GetComponent<CanvasGroup>();
  42. if ( this.autoPlay )
  43. this.iTweenPlay();
  44. }
  45. public override void iTweenPlay() {
  46. Hashtable ht = new Hashtable();
  47. ht.Add( "from", this.valueFrom );
  48. ht.Add( "to", this.valueTo );
  49. ht.Add( "time", this.tweenTime );
  50. ht.Add( "delay", this.waitTime );
  51. ht.Add( "looptype", this.loopType );
  52. ht.Add( "easetype", this.easeType );
  53. ht.Add( "onstart", (Action<object>)( newVal => {
  54. _onUpdate( this.valueFrom );
  55. if ( onStart != null ) {
  56. onStart.Invoke();
  57. }
  58. } ) );
  59. ht.Add( "onupdate", (Action<object>)( newVal => {
  60. _onUpdate( (float)newVal );
  61. } ) );
  62. ht.Add( "oncomplete", (Action<object>)( newVal => {
  63. if ( onComplete != null ) {
  64. onComplete.Invoke();
  65. }
  66. } ) );
  67. ht.Add( "ignoretimescale", ignoreTimescale );
  68. iTween.ValueTo( this.gameObject, ht );
  69. }
  70. private void _onUpdate( float value ) {
  71. if ( _spriteRenderer != null ) {
  72. _spriteRenderer.color = new Color( _spriteRenderer.color.r, _spriteRenderer.color.g, _spriteRenderer.color.b, value );
  73. }
  74. if ( _uiImage != null ) {
  75. _uiImage.color = new Color( _uiImage.color.r, _uiImage.color.g, _uiImage.color.b, value );
  76. }
  77. if ( _uiRawImage != null ) {
  78. _uiRawImage.color = new Color( _uiRawImage.color.r, _uiRawImage.color.g, _uiRawImage.color.b, value );
  79. }
  80. if ( _uiCanvasGroup != null ) {
  81. _uiCanvasGroup.alpha = value;
  82. }
  83. }
  84. }