| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- using UnityEngine;
- public class ElectricLineController : MonoBehaviour
- {
- public Transform pointA;
- public Transform pointB;
- public Material electricMaterial;
- public int columns = 2; // 帧列数
- public int rows = 8; // 帧行数
- public float frameRate = 0.05f;
- private LineRenderer lineRenderer;
- private int currentFrame = 0;
- private float timer = 0f;
- void Start()
- {
- lineRenderer = GetComponent<LineRenderer>();
- lineRenderer.positionCount = 2;
- Vector2 scale = new Vector2(1f / columns, 1f / rows);
- electricMaterial.SetTextureScale("_MainTex", scale);
- }
- void Update()
- {
- // 实时更新两点位置
- if (pointA && pointB)
- {
- lineRenderer.SetPosition(0, pointA.position);
- lineRenderer.SetPosition(1, pointB.position);
- }
- // 播放序列帧
- timer += Time.deltaTime;
- if (timer >= frameRate)
- {
- timer -= frameRate;
- currentFrame = (currentFrame + 1) % (columns * rows);
- int col = currentFrame % columns;
- int row = currentFrame / columns;
- //Y 要倒着来
- Vector2 offset = new Vector2((float)col / columns, 1f - ((float)(row + 1) / rows));
- electricMaterial.SetTextureOffset("_MainTex", offset);
- }
- }
- }
|