游戲模擬自動(dòng)化戰(zhàn)斗需求

前言
有些項(xiàng)目需要模擬人真實(shí)操作游戲的需求,所以就有了現(xiàn)在的這套功能代碼。
這里我們來分析一下需求,人玩游戲時(shí)是怎么一個(gè)流程?一般分為:


操控游戲流程.png

這里我們不難發(fā)現(xiàn),我們要做的其實(shí)只要在自動(dòng)化輸入這塊就行了。后面的流程都是按游戲正常游戲來就可以了。
那怎么實(shí)現(xiàn)這樣的功能呢?我們可以拆分我們的輸入指令類型,游戲里最多的邏輯應(yīng)該就屬于游戲ui觸發(fā)的了。然后ui觸發(fā)在unity常見類型就是Button,EventTrigger這些。這些可以在后續(xù)完善中再拓展就好。除了ui事件外,我們可能也需要一些自定義的事件類型。那觸發(fā)類型就可以先定義兩種大類型。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class AutoLogic : MonoBehaviour
{
    public enum ActiveType
    {
        Button,
        EventTrigger,
        Action
    }

    public int id;
    public float time = 2;//時(shí)間間隔執(zhí)行
    public float randomTime = 0;//用于隨機(jī)時(shí)間區(qū)間
    public Button btn;//UI按扭事件
    public EventTrigger eventTrigger;
    public Action action;//自定義事件
    public bool isDestoryObject;//刪除物體(用于自定義事件)
    public ActiveType activeType = ActiveType.Button;//自動(dòng)邏輯適配類型

    void OnEnable()
    {
        if (btn == null)
            btn = GetComponent<Button>();
        if (eventTrigger == null)
            eventTrigger = GetComponent<EventTrigger>();
        id = this.gameObject.GetInstanceID();
        if (randomTime != 0)
            time = UnityEngine.Random.Range(time, randomTime);
        DNWBManager.instance.AddAutoLogics(this);
    }
}

接下來我們需要集中處理這些邏輯,我們需要一個(gè)管理類。我們可以用集合的方式,按列表的序列逐個(gè)執(zhí)行。
測試過程發(fā)現(xiàn),同一個(gè)邏輯可能會(huì)被重復(fù)添加,所以我們要加入兼容機(jī)制。原來用協(xié)程實(shí)現(xiàn)的,但是發(fā)現(xiàn)有觸發(fā)時(shí)機(jī)問題,所以最好是在同個(gè)時(shí)間幀里做區(qū)分與觸發(fā)。
實(shí)現(xiàn)代碼如下:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class AutoLogicManager : MonoBehaviour
{
    public static AutoLogicManager instance;
    public List<AutoLogic> autoLogics = new List<AutoLogic>();


    public void AddAutoLogics(AutoLogic al)
    {
        if (autoLogics.Count <= 0)
        {
            autoLogics.Add(al);
            return;
        }
        bool isAdd = true;
        //避免重復(fù)添加組件
        foreach (var logic in autoLogics)
        {
            if (al.id != 0 && logic.id != 0)
            {
                    if (logic != null && al.gameObject == logic.gameObject)
                    {
                        isAdd = false;
                     }
            }
        }
        if (isAdd)
            autoLogics.Add(al);
    }

    void StartAutoLogic()
    {
        if (autoLogics[0].activeType == AutoLogic.ActiveType.Action)
        {
            if (autoLogics[0].action != null)
                autoLogics[0].action.Invoke();
        }
        else if (autoLogics[0].activeType == AutoLogic.ActiveType.EventTrigger)
        {
            if (autoLogics[0].eventTrigger != null)
            {
                foreach (var trigger in autoLogics[0].eventTrigger.triggers)
                {
                    trigger.callback.Invoke(null);
                }
            }
        }
        else
        {
            if (autoLogics[0].btn != null && autoLogics[0].btn.onClick != null)
                autoLogics[0].btn.onClick.Invoke();
        }
        if (autoLogics[0].isDestoryObject)
        {
            Destroy(autoLogics[0].gameObject);
        }
        autoLogics.RemoveAt(0);
    }
    void Update()
    {
        if (autoLogics.Count > 0)
        {
            if (autoLogics[0] == null)
            {
                autoLogics.RemoveAt(0);
                return;
            }
            autoLogics[0].time -= Time.deltaTime;
            if (autoLogics[0].time <= 0)
            {
                Debug.LogError(autoLogics[0].gameObject.name + "執(zhí)行自動(dòng)邏輯??!" + autoLogics[0].id);
                StartAutoLogic();
            }
        }
    }
}

使用方法有兩種:
1.可以直接掛載AutoLogic腳本到對(duì)應(yīng)的Button組件物體上,然后在組合上填入需要的數(shù)據(jù)。
2.也可以通過代碼AddComponent,調(diào)整對(duì)應(yīng)的數(shù)據(jù)。
如果用到的是Action的方式,也可以用添加組件的方式,或者用委托賦值等。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容