コード例 #1
0
    void Awake()
    {
        // Create empty object to contain all spawned enemies
        _enemyEmptyParent = new GameObject("enemies");
        _enemyEmptyParent.transform.parent = this.transform;

        // Auto-find closest zone
        if (_getClosestZone)
        {
            SpawnZone[] zones   = FindObjectsOfType <SpawnZone>();
            SpawnZone   nearest = null;

            // Find closest zone
            if (zones.Length > 0)
            {
                float smallestDistance = float.MaxValue;
                foreach (SpawnZone z in zones)
                {
                    // Get distance to the zone
                    float dist = Vector3.Distance(z.transform.position, transform.position);

                    if (dist < smallestDistance)
                    {
                        smallestDistance = dist;
                        nearest          = z;
                    }
                }
            }

            if (nearest == null)
            {
                Debug.LogError("No Spawn Zones were found. Right click in the scene heirarchy to create a new zone.");
            }

            _zone = nearest;
        }

        // Register this script with the zone, or remove this script if there is no zone specified
        if (_zone)
        {
            _zone.AddNode(this);
        }
        else
        {
            Debug.LogError("SpawnNode is not assigned to a SpawnZone. The node will be destroyed.", this);
            Destroy(this);
        }
    }