即时通讯位于塔防中-使用C#在Unity中开发样式。因此,我有一个抽象的塔类,它具有范围,射速等方面的整数。然后,我有一些针对不同类型塔的子类。我怀疑在塔类中是否具有GameObject类型的属性,该属性包含塔类游戏对象本身(用于生成弹丸,必要时移动塔)或具有带有该属性的类,然后具有控制该类的控制器。塔类用于分配和控制游戏对象本身(何时需要移动它,是否将其设置为活动状态等)。
我希望我能表明自己的意思!谢谢!
编辑
我不确定选择哪个选项会更好。
选项1:
public abstract class Tower{
private int health;
...
private GameObject object;
public HealthDown(){
if(health >1){ health -= 1;}
else { object.SetActive(false);}
}
}
选项2:
public abstract class Tower{
private int health;
...
public HealthDown(){
if(health >1){ health -= 1;}
}
}
然后是控制塔楼的脚本:
public class Controller : MonoBehaviour {
Tower tower;
...
void Update(){
if(tower.health <= 1){this.gameObject.SetActive(false);}
}
}
我希望现在更加清楚!
谢谢!
我假设您为每座塔使用不同的模型/材料,因此我将通过附加到每个预制件的-MonoBehavior
派生脚本(TowerController.cs
)参数化这些不同的攻击率等:
Tower1 prefab:
Model = tower
Material = tower1
Script = TowerController.cs
attackRate = 10
projectilePrefab = projectile1
Tower2 prefab:
Model = tower
Material = tower2
Script = TowerController.cs
attackRate = 20
projectilePrefab = projectile2
这遵循正常的Unity工作流程,并允许通过UI进行编辑,并在需要时被实例覆盖。
因此,选项1和选项2都不存在,而是更接近选项2而没有类继承,并且更像Unity常规的。
这是TowerController
允许设置attackRate
和预制件预制件的示例实现(因此,可以通过UI为每种塔类型设置不同的版本):
using UnityEngine;
using System.Collections;
public class TowerController: MonoBehaviour
{
public int health = 50;
public int attackRate = 10;
public GameObject projectilePrefab;
public void TakeDamage(int damage)
{
health -= damage;
if (health <= 0) {
gameObject.SetActive(false);
}
}
private void FireProjectile()
{
GameObject projectile = Instantiate(projectilePrefab, transform.position, transform.rotation) as GameObject;
projectile.transform.SetParent(transform.parent, true);
// Add force etc.
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句