?????個(gè)人主頁(yè):@元宇宙-秩沅
????? hallo 歡迎 點(diǎn)贊?? 收藏? 留言?? 加關(guān)注?!
????? 本文由 秩沅 原創(chuàng)
????? 收錄于專欄:unityUI專題篇
?通用API實(shí)現(xiàn)抽象行為封裝【五】?
??前言
???
??(A)常用關(guān)鍵API
??(B)需求分析
??(C)行為實(shí)現(xiàn)——武器拾取變成技能拾取
??????:步驟
- 1.添加道具預(yù)制體(隨機(jī)生成武器)
- 2.封裝道具基類
- 3.封裝武器道具邏輯
1.封裝道具基類
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________項(xiàng)目: ______________
//___________功能: 道具基類,提取道具相同點(diǎn)
//___________創(chuàng)建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class propsFather : MonoBehaviour
{
public float RotateSpeed = 100;
public GameObject Effect; //吃掉道具的特效
private void Update()
{
transform.Rotate(Vector3.up * RotateSpeed * Time.deltaTime);
}
public virtual void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
GainEffect();
}
}
//生成特效
public void GainEffect()
{
GameObject effect = Instantiate(Effect, transform.position, transform.rotation); //實(shí)例化吃掉特效
AudioSource effectAudio = effect.GetComponent<AudioSource>();
effectAudio.enabled = MusicContol.SingleMusicContol.nowMusicData.soundSwitch;
effectAudio.volume = MusicContol.SingleMusicContol.nowMusicData.soundRoll;
gameObject.SetActive(false); //吃掉后讓其先失活
Destroy(effect, 3); //特效持續(xù)三秒后消失
}
}
封裝武器道具邏輯
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________項(xiàng)目: ______________
//___________功能: 武器道具的邏輯封裝
//___________創(chuàng)建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class props_Weapon : propsFather
{
public GameObject [] weapons;
private int index;
private void Awake()
{
weapons = Resources.LoadAll<GameObject>(@"Prefabs/Weapon") ; //加載武器資源
}
public override void OnTriggerEnter(Collider other)
{
base.OnTriggerEnter(other);
if (other.CompareTag("Player"))
{
if (weapons != null)
{
//隨機(jī)武器生成
index = Random.Range(0, weapons.Length);
GameObject NewWeapon = Instantiate(weapons[index]);
//標(biāo)識(shí)武器的主人
Weapon weaponScript = NewWeapon.GetComponent<Weapon>();
weaponScript.Tank = other.gameObject;
//拾取武器
MainTank tankScript = other.gameObject.GetComponent<MainTank>();
tankScript.ChangeWeapon(NewWeapon);
}
}
}
}
??(D)行為實(shí)現(xiàn)——四個(gè)屬性道具一個(gè)腳本
巧妙地使用了枚舉,使得四個(gè)腳本變成一個(gè)腳本共同使用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________項(xiàng)目: ______________
//___________功能: 加血道具邏輯封裝
//___________創(chuàng)建者:秩沅_______________
//_____________________________________
//-------------------------------------
public enum Type_Atrribute //枚舉類型的創(chuàng)建相當(dāng)于將四個(gè)屬性的腳本直接變成一個(gè)腳本
{
blood,
attack,
defence,
speed
}
public class props_Attribute : propsFather
{
public Type_Atrribute atrribute = Type_Atrribute.blood ;
public int atk = 20;
public int def = 20;
public int Spe = 20;
public int blo = 20;
public override void OnTriggerEnter(Collider other)
{
base.OnTriggerEnter(other);
if (other.CompareTag("Player"))
{
MainTank tankScript = other.gameObject.GetComponent<MainTank>();
switch (atrribute)
{
case Type_Atrribute.blood:
tankScript.nowBlood += blo;
if (tankScript.nowBlood > tankScript.maxBlood)
tankScript.nowBlood = tankScript.maxBlood;
break;
case Type_Atrribute.attack:
tankScript.attack += atk;
break;
case Type_Atrribute.defence:
tankScript.defence += def;
break;
case Type_Atrribute.speed:
tankScript.moveSpeed += Spe;
break;
default:
break;
}
}
}
}
??(E)行為實(shí)現(xiàn)——摧毀箱子爆出道具
箱子類相關(guān)封裝
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________項(xiàng)目: ______________
//___________功能: 箱子銷毀相關(guān)邏輯封裝
//___________創(chuàng)建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class BoxTrigger : MonoBehaviour
{
public int defence = 50; //箱子的防御力
public GameObject effect; //箱子銷毀特效
private GameObject[] props;
private int sufferAttack;
private int PropChance;
private void Start()
{
PropChance = Random.Range(1, 101);
if (PropChance <= 70)
{
props = Resources.LoadAll<GameObject>(@"Prefabs/Props");
}
}
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("bullet"))
{
BulletMove BulletSript = other.gameObject.GetComponent<BulletMove>();
//如果子彈是玩家打來的則獲取子彈的攻擊力
if(BulletSript.Tank.tag == "Player")
{
TankFather tankScript = BulletSript.Tank.GetComponent<TankFather>();
sufferAttack = tankScript.attack;
defence -= sufferAttack; //防御力減少
sufferAttack = 0; //子彈的傷害是一次性的所以傷害歸0
if (defence <= 0) RandomMakeProp();
}
}
}
//隨機(jī)生成道具
public void RandomMakeProp()
{
int index;
if (props != null) //70%概率生成道具
{
index = Random.Range(0, props.Length);
GameObject eff = Instantiate(effect, transform.position , transform.rotation);
//音效同步
AudioSource audioS = eff.GetComponent<AudioSource>();
audioS.enabled = MusicContol.SingleMusicContol.nowMusicData.soundSwitch;
audioS.volume = MusicContol.SingleMusicContol.nowMusicData.soundRoll;
Destroy(eff, 3); //延時(shí)銷毀特效
GameObject prop = Instantiate(props[index], transform.position +new Vector3(0, 2, 0), transform.rotation);
Destroy(gameObject);
}
else //30%的概率直接銷毀
{
GameObject eff = Instantiate(effect, transform.position , transform.rotation);
//音效同步
AudioSource audioS = eff.GetComponent<AudioSource>();
audioS.enabled = MusicContol.SingleMusicContol.nowMusicData.soundSwitch;
audioS.volume = MusicContol.SingleMusicContol.nowMusicData.soundRoll;
Destroy(eff, 3); //延時(shí)銷毀特效
Destroy(gameObject);
}
}
}
總結(jié):
- 枚舉在面向?qū)ο笾械拿钣?/li>
?相關(guān)文章?
?【2023unity游戲制作-mango的冒險(xiǎn)】-6.關(guān)卡設(shè)計(jì)
?【2023unity游戲制作-mango的冒險(xiǎn)】-5.攻擊系統(tǒng)的簡(jiǎn)單實(shí)現(xiàn)
?【2023unity游戲制作-mango的冒險(xiǎn)】-4.場(chǎng)景二的鏡頭和法球特效跟隨
?【2023unity游戲制作-mango的冒險(xiǎn)】-3.基礎(chǔ)動(dòng)作和動(dòng)畫API實(shí)現(xiàn)
?【2023unity游戲制作-mango的冒險(xiǎn)】-2.始畫面API制作
?【2023unity游戲制作-mango的冒險(xiǎn)】-1.場(chǎng)景搭建
?“狂飆”游戲制作—游戲分類圖鑒(網(wǎng)易游學(xué))
?本站最全-unity常用API大全(萬字詳解),不信你不收藏文章來源:http://www.zghlxwxcb.cn/news/detail-608546.html
你們的點(diǎn)贊?? 收藏? 留言?? 關(guān)注?是我持續(xù)創(chuàng)作,輸出優(yōu)質(zhì)內(nèi)容的最大動(dòng)力!文章來源地址http://www.zghlxwxcb.cn/news/detail-608546.html
到了這里,關(guān)于【unity之IMGUI實(shí)踐】通用API實(shí)現(xiàn)抽象行為封裝【五】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!