예제 #1
0
        /// <summary>
        /// 根据单位id和所有者获得单位实例
        /// </summary>
        /// <param name="unitID"></param>
        /// <param name="owner"></param>
        /// <param name="damage"></param>
        /// <returns></returns>
        public GameObject GetGameUnit(string unitID, OwnerEnum owner, int damage = 0)
        {
            GameObject newUnit;

            //根据所有者选择不同的模板进行单位实例化
            switch (owner)
            {
            case (OwnerEnum.Enemy):
                newUnit = Instantiate(EnemyMould);
                break;

            case (OwnerEnum.Player):
                newUnit = Instantiate(FriendlyMould);
                break;

            default:
                newUnit = Instantiate(NeutralityMould);
                break;
            }

            //将生成的GameUnit传入数据库赋值初始化
            UnitDataBase.Instance().InitGameUnit(newUnit.GetComponent <GameUnit>(), unitID, owner, damage);

            return(newUnit);
        }
예제 #2
0
        /// <summary>
        /// 实现返回GameObject的函数,GetInst(string id)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="owner"></param>
        /// <param name="Damage"></param>
        /// <returns></returns>
        public GameObject GetInst(string unitId, OwnerEnum owner, int Damage = 0)
        {
            foreach (GameObject _unit in m_pool)
            {
                ///根据所有者不同选择对象,因为三种对象(友军,敌军,中立)身上的脚本可能不一样,这样可以节约一点写代码的时间
                ///注意这里其实比较危险,如果搜不到GameUnit的脚本是会报错中断的,不过我们可以保证他一定有这个脚本,所以在这里可以这么写
                if (_unit.GetComponent <GameUnit>().owner == owner)
                {
                    m_pool.Remove(_unit);
                    _unit.SetActive(true);
                    //从单位数据库将新单位初始化
                    UnitDataBase.Instance().InitGameUnit(_unit, unitId, owner, Damage);
                    return(_unit);
                }
            }

            //如果没有空余的对应类型的对象,就
            //从单位生成车间取得新单位实例
            return(GameUnitFactory.Instance().GetGameUnit(unitId, owner, Damage));
        }