微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Unity,为什么要生成这么多预制件?

如何解决Unity,为什么要生成这么多预制件?

代码在这里

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnManager : MonoBehavIoUr
{
    public GameObject[] animalprefabs;
    public float spawninterval = 2.5f;
    public float spawnDelay = 2.0f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {

         InvokeRepeating("spawnRandomAnimal",spawnDelay,spawninterval);
        
    }

    void spawnRandomAnimal()
    {
        int animalIndex = Random.Range(0,animalprefabs.Length);
        int SpawnIndex = 20;
        Vector3 spawnPosition = new Vector3(Random.Range(-SpawnIndex,SpawnIndex),25);
        Instantiate(animalprefabs[animalIndex],spawnPosition,animalprefabs[animalIndex].transform.rotation);
    }
}

我想在某个时间间隔后随机实例化预制件,但不知何故生成了大量预制件。我希望在时间间隔后在随机位置实例化一个预制件......有人请帮忙

解决方法

每次调用 # Rakefile task :check_localhost do pid = system("lsof -i tcp:3000 -t") fail unless pid # or you can use `abort('message')` end task :test => :check_localhost do puts "****** THIS IS TEST ******" end 时,您都在重新调用 invokeRepeating。每次它被调用时,它都会添加另一个要重复的任务。

移动它以开始解决您的问题。

Update()
,

您在 update() 中使用了 invokerepeating() 函数。这就是生成太多预制件的原因。从 update() 中删除 invokerepeating() 并在 start() 或 onenable() 中尝试。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。