首页 > 学院 > 开发设计 > 正文

Unity BuffSystem

2019-11-14 09:26:27
字体:
来源:转载
供稿:网友
1. 写好Unity的BuffSystem的单例主要接口using UnityEngine;using System.Collections;using Police.PaoKu;using DG.Tweening;public class BuffSystem : Singleton<BuffSystem> { public BUFFType buffChoose; public Buff[] _buffSystem; public void InvokeCallBack(BUFFType _buffType , float delayTime = 0) { DOVirtual.DelayedCall(delayTime,()=> { foreach (var o in _buffSystem) { if (o.buffType == _buffType) { o.callBack.GetComponent<BaseBuff>().Begin(new string[1] { _buffType.ToString() }); break; } } }).SetUpdate(false); } public bool ReleaseBuff(BUFFType _buffType) { foreach (var o in _buffSystem) { if (o.buffType == _buffType) { o.callBack.GetComponent<BaseBuff>().Exit(); return true; } } return false; }}public enum BUFFType{ 普通, 助推器, 盾, 免疫}[System.Serializable]public class Buff{ public BUFFType buffType; public GameObject callBack;}

2.写好Unity 每个Buff系统类的每一种Buff效果基础类;

using UnityEngine;using System.Collections;using System;public class BaseBuff : MonoBehaviour ,iperiod{ public float lifeTime; public float lastTime; public bool isDestroy = false; PRotected bool isBegin; public virtual void Begin(string[] _str) { isBegin = true; lastTime = lifeTime; } public virtual void Exit() { isBegin = false; lifeTime = lastTime; if (isDestroy) DestroyImmediate(this); } protected virtual void Update() { if(isBegin) lifeTime -= Time.deltaTime; if(lifeTime < 0) { Exit(); } }}public interface IPeriod{ void Begin(string[] _str); void Exit();}

3.然后举例写一个buff类型例子

using UnityEngine;using System.Collections;using Police.PaoKu;public class SubDefend : BaseBuff{ public override void Begin(string[] _str) { base.Begin(_str); ParkourPlayerController.Instance.Player.gameObject.AddComponent<Defend>(); } public override void Exit() { base.Exit(); Destroy(ParkourPlayerController.Instance.Player.GetComponent<Defend>()); }}

4.每个buff类型给予空物体,然后赋值给(1)的配置表 这里写图片描述

5.这样的优势是容易扩展,可以理解成工厂模式,而且改变每个buff代码互不影响。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表