ElectricLineController.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using UnityEngine;
  2. public class ElectricLineController : MonoBehaviour
  3. {
  4. public Transform pointA;
  5. public Transform pointB;
  6. public Material electricMaterial;
  7. public int columns = 2; // 帧列数
  8. public int rows = 8; // 帧行数
  9. public float frameRate = 0.05f;
  10. private LineRenderer lineRenderer;
  11. private int currentFrame = 0;
  12. private float timer = 0f;
  13. void Start()
  14. {
  15. lineRenderer = GetComponent<LineRenderer>();
  16. lineRenderer.positionCount = 2;
  17. Vector2 scale = new Vector2(1f / columns, 1f / rows);
  18. electricMaterial.SetTextureScale("_MainTex", scale);
  19. }
  20. void Update()
  21. {
  22. // 实时更新两点位置
  23. if (pointA && pointB)
  24. {
  25. lineRenderer.SetPosition(0, pointA.position);
  26. lineRenderer.SetPosition(1, pointB.position);
  27. }
  28. // 播放序列帧
  29. timer += Time.deltaTime;
  30. if (timer >= frameRate)
  31. {
  32. timer -= frameRate;
  33. currentFrame = (currentFrame + 1) % (columns * rows);
  34. int col = currentFrame % columns;
  35. int row = currentFrame / columns;
  36. //Y 要倒着来
  37. Vector2 offset = new Vector2((float)col / columns, 1f - ((float)(row + 1) / rows));
  38. electricMaterial.SetTextureOffset("_MainTex", offset);
  39. }
  40. }
  41. }