TC_AnimateTransform.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using UnityEngine;
  2. using System.Collections;
  3. [ExecuteInEditMode]
  4. public class TC_AnimateTransform : MonoBehaviour {
  5. public bool animate = true;
  6. public float rotSpeed;
  7. public Vector3 moveSpeed;
  8. public float scaleSpeed, scaleAmplitude, scaleOffset;
  9. Vector3 posOld;
  10. float time;
  11. #if UNITY_EDITOR
  12. void OnEnable()
  13. {
  14. UnityEditor.EditorApplication.update += MyUpdate;
  15. }
  16. void OnDisable()
  17. {
  18. UnityEditor.EditorApplication.update -= MyUpdate;
  19. }
  20. #endif
  21. void Update()
  22. {
  23. MyUpdate();
  24. }
  25. void MyUpdate ()
  26. {
  27. // if (Input.GetKey(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.A)) animate = !animate;
  28. if (!animate) return;
  29. float deltaTime = Time.realtimeSinceStartup - time;
  30. transform.Rotate(0, rotSpeed * deltaTime, 0);
  31. transform.Translate(moveSpeed * deltaTime * 90);
  32. float sp = (Mathf.Sin(Time.realtimeSinceStartup * scaleSpeed) * scaleAmplitude) + scaleOffset;
  33. transform.localScale = new Vector3(sp, sp, sp);
  34. time = Time.realtimeSinceStartup;
  35. }
  36. void OnDrawGizmos()
  37. {
  38. // Gizmos.Lab
  39. Event eventCurrent = Event.current;
  40. // if (eventCurrent.keyCode == KeyCode.Space) Debug.Log(eventCurrent);
  41. if (eventCurrent.shift && eventCurrent.type == EventType.KeyUp) animate = !animate;
  42. }
  43. }