| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- 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);
- /*electricMaterial.SetInt("_ZWrite", 0); // 不写入深度
- electricMaterial.SetInt("_ZTest", (int)UnityEngine.Rendering.CompareFunction.Always); // 永远显示
- electricMaterial.renderQueue = 5000; // Overlay*/
- }
- 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);
- }
- }
- }
|