Swarm Object Manager这个插件,是用来管理群对象物体的,对于像是子弹或者是粒子系统这样重复利用率很高的东西十分有效
先看看Main函数,首先定义了一个静态的实例,方便后面的各种物体创建对象的时候使用
private static Main _instance = null;
public static Main Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType(typeof(Main)) as Main;
if (_instance == null && Application.isEditor)
Debug.LogError("Could not locate a Main object. You have to have exactly one Main in the scene.");
}
return _instance;
}
}
void Awake()
{
if (_instance != null)
{
Debug.LogError("You can only have one instance of the Main singleton object in existence.");
}
else
_instance = this;
}
然后确定场景中的人物和 子弹管理者,粒子系统管理者,确保init和 updata 注意这里是通过主函数的update对子物体的framupdate来调用
void Start()
{
ship.Initialize();
asteroidManager.Initialize();
bulletManager.Initialize();
explosionManager.Initialize();
}
void Update()
{
ship.FrameUpdate();
asteroidManager.FrameUpdate();
bulletManager.FrameUpdate();
explosionManager.FrameUpdate();
}
这样看 这个main函数的思路已经十分清晰了obj
- -我想过一些缺陷的地方,就是事件的调用可能会比较混乱,但是如果事件不多只有鼠标的position的话那就不是问题了
//还有一个ship 可以看做是玩家的化身,用来发出动作
public class Ship : MonoBehaviour
{
private Transform _thisTransform;
public float turnSpeed;
public float bulletSpeed;
public void Initialize()
{
_thisTransform = this.transform;
}
public void FrameUpdate() //处理动作
{
if (Input.GetKey(KeyCode.LeftArrow))
{
_thisTransform.RotateAround(Vector3.back, -turnSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.RightArrow))
{
_thisTransform.RotateAround(Vector3.back, turnSpeed * Time.deltaTime);
}
if (Input.GetKeyDown(KeyCode.LeftControl) || Input.GetKeyDown(KeyCode.RightControl))
{
Bullet bullet = (Bullet)Main.Instance.bulletManager.ActivateItem();
if (bullet != null)
bullet.Fire(_thisTransform.up * bulletSpeed);
}
}
}