// Update is called once per frame void Update() { timer -= Time.deltaTime; if (timer <= 0) { // 重新计时 timer = 2.0f; // 如果鱼的数量达到最大数量则返回 if (cur_count >= max_count) { return; } // 随机1、2、3产生不同的鱼 int index = 1 + (int)(Random.value * 3.0f); if (index > 3) { index = 3; } // 更新鱼的数量 cur_count++; // 读取鱼的prefab GameObject fishprefab = (GameObject)Resources.Load("Prefabs/fish" + index); float cameraz = Camera.main.transform.position.z; // 鱼的初始随机位置 Vector3 randpos = new Vector3(Random.value, Random.value, -cameraz); randpos = Camera.main.ViewportToWorldPoint(randpos); // 鱼的随机初始方向 Fish.Target target = Random.value > 0.5f ? Fish.Target.Right : Fish.Target.Left; Fish f = Fish.Create(fishprefab, target, randpos); // 注册鱼的死亡回调 f.OnDeath += OnDeath; } }
// Update is called once per frame void Update() { timer = timer - Time.deltaTime; if (timer <= 0) { timer = 2.0f; if (fish_count >= max_fish) { return; } int index = 1 + (int)(Random.value * 3.0f); if (index > 3) { index = 3; //最大为3,防止溢出 } fish_count++; GameObject fishprefab = (GameObject)Resources.Load("fish" + index); //读取鱼的prefab float cameraz = Camera.main.transform.position.z; Vector3 randpos = new Vector3(Random.value, Random.value, -cameraz); //鱼的初始位置 randpos = Camera.main.ViewportToWorldPoint(randpos); Fish.Target target = Random.value > 0.5f ? Fish.Target.Right : Fish.Target.Left; //鱼的初始方向 Fish f = Fish.Create(fishprefab, target, randpos); f.OnDeath = f.OnDeath + OnDeath; //处理鱼的死亡信息 } }