CatLikeCoding Basic No.2
graph.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class graph : MonoBehaviour { [SerializeField] Transform PointPrefab; [SerializeField,Range(10,100)] int resolution = 10; Transform[] points; // 初始化位置 private void Awake() {
//数组所有点位进行初始化 points=new Transform[resolution]; float step = 2f / resolution; var scale = Vector3.one * step; var position=Vector3.zero; for (int i=0; i < points.Length;i++) {//transform是引用,所以更改point同样也会更改points[i]?存疑(暂时没有c#基础),不用point直接改points[i]也正确 Transform point = points[i] =Instantiate(PointPrefab); position.x = (i + 0.5f) * step - 1f; point.localPosition = position; point.localScale = scale; point.SetParent(transform,false); } } void Start() { } // Update is called once per frame void Update() { float time = Time.time; for (int i = 0; i < points.Length; i++) { Transform point = points[i]; Vector3 position = point.localPosition; position.y = Mathf.Sin(Mathf.PI * (position.x + time)); point.localPosition = position; } } }