我不明白为什么TimeBetweenSpawns
属性没有价值。
也请让我知道,是否有更好的方法可以在按下后一段时间禁用按钮,然后再次启用它。
UnitSpawn.cs
public class UnitSpawn : MonoBehaviour
{
private GameObject position;
private Button button;
public void Awake()
{
button = GameObject.Find("Canvas/Button").GetComponent<Button>();
}
public void SpawnUnit(Unit unit)
{
position = GameObject.Find(unit.SpawnPosition);
Instantiate(unit.gameObject, position.transform.position,
position.transform.rotation);
button.enabled = false;
StartCoroutine(WaitTime(unit.TimeBetweenSpawns));
}
IEnumerator WaitTime(int timeBetweenSpawns)
{
yield return new WaitForSeconds(timeBetweenSpawns);
button.enabled = true;
}
}
Unit.cs
public abstract class Unit : MonoBehaviour
{
public abstract string SpawnPosition { get; set; }
public abstract int MovementSpeed { get; set; }
public abstract int TimeBetweenSpawns { get; set; }
public abstract int Health { get; set; }
public abstract int AttackSpeed { get; set; }
public abstract int Damage { get; set; }
public void Move()
{
var rb = GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(-MovementSpeed, rb.velocity.y);
}
}
Unit1.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Unit1 : Unit
{
public override string SpawnPosition { get; set; } =
"SpawnManager/SpawnPositionForUnit1";
public override int MovementSpeed { get; set; } = 3 ;
public override int TimeBetweenSpawns { get; set; } = 3;
public override int Health { get; set; }
public override int AttackSpeed { get; set; }
public override int Damage { get; set; }
public void Update()
{
Move();
}
}