Unity-Technologies/EntityComponentSystemSamples

JobsTutorial Step1_NoJobs is a terrible example

yp0403130231 opened this issue · 0 comments

I just read JobsTutorial found Step1_NoJobs is a terrible example.
I hope it should be like this.

FindNearest.cs:

using UnityEngine;

namespace Step1
{
    public class FindNearest : MonoBehaviour
    {
        public Vector3[] TargetPositions;
        public Vector3[] SeekerPositions;
        public Vector3[] NearestTargetPositions; 
        
        public void Awake()
        {
            Spawner spawner = Object.FindObjectOfType<Spawner>();
            TargetPositions = new Vector3[spawner.NumTargets];
            SeekerPositions = new Vector3[spawner.NumSeekers];
            NearestTargetPositions = new Vector3[spawner.NumSeekers];
        }

        public void Update()
        {
            for (int i = 0; i < TargetPositions.Length; i++)
            {
                TargetPositions[i] = Spawner.TargetTransforms[i].localPosition;
            }

            for (int i = 0; i < SeekerPositions.Length; i++)
            {
                SeekerPositions[i] = Spawner.SeekerTransforms[i].localPosition;
            }

            float temp1, temp2;
            for (int i = 0; i < SeekerPositions.Length; i++)
            {
                Vector3 seekerPos = SeekerPositions[i];
                float nearestDistSq = float.MaxValue;
                for (int j = 0; j < TargetPositions.Length; j++)
                {
                    Vector3 targetPos = TargetPositions[j];
                    temp1 = seekerPos.x - targetPos.x;
                    temp2 = seekerPos.z - targetPos.z;
                    float distSq = temp1 * temp1 + temp2 * temp2;
                    if (distSq < nearestDistSq)
                    {
                        nearestDistSq = distSq;
                        NearestTargetPositions[i] = targetPos;
                    }
                }
            }

            // Draw a debug line from each seeker to its nearest target.
            for (int i = 0; i < SeekerPositions.Length; i++)
            {
                // float3 is implicitly converted to Vector3
                Debug.DrawLine(SeekerPositions[i], NearestTargetPositions[i]);
            }
        }
    }
}

If you read this code , you will Understand my thoughts, The frame rate of this code will be around 60.