MonoBehaviour的多态
MonoBehaviour 可以进行多态的处理
如下武器逻辑系统的多态处理
public class EquipLogic : MonoBehaviour { virtual public void execLogic() { } } /// <summary> /// @brief // add hp by 10% atk and add 150 atk /// </summary> public class EquipLogic1 : EquipLogic { // Use this for initialization void Start() { PlayerData.ins.addAtk(150); } // Update is called once per frame void Update() { } public override void execLogic() { // PlayerController.ins.addHp(100); PlayerController.ins.addHp((int)(PlayerData.ins.getAtk() * 0.1f)); } } /// <summary> /// @brief add 80 atk /// </summary> public class EquipLogic2 : EquipLogic { void Start() { PlayerData.ins.addAtk(80); } } /// <summary> /// @brief // add 20 atk /// </summary> public class EquipLogic3 : EquipLogic { void Start() { PlayerData.ins.addAtk(20); } }
虽然在一个文件内,但是不影响规则,
Destroy(PlayerData.ins.player.GetComponent<EquipLogic>()); PlayerData.ins.player.AddComponent<EquipLogic3>();