Unity开发 MMORPG类游戏引导系统

下面展示一些 MMORPG类游戏引导系统逻辑代码

using LitJson;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 class GuideController : MonoBehaviour
    {
    
    
        public static GuideController instance;

        public List<GameObject> tapTipList;
        public List<GameObject> Guides;//引导窗口
        public List<GameObject> tapTips;//点击提示窗口
        public List<GameObject> playSkillTips;//技巧提示
        [HideInInspector]
        public GameObject swapTip;

        //判断Unity端引导过程是否结束
        [HideInInspector]
        public bool inGuide;
        
        [HideInInspector]
        public int nextStep;

        /// <summary>
        /// 玩家当前引导步数
        /// </summary>
        [HideInInspector]
        public int StepSign = 0;

        [HideInInspector]
        //引导是否没有
        public bool fromBegin;

        [HideInInspector]
        /// <summary>
        /// 用于判断是否第一次触发了吃饼干,只用一次
        /// </summary>
        public bool afterFood;

        [HideInInspector]
        /// <summary>
        /// 用于判断是否第一次触发了zombi死亡,只用一次
        /// </summary>
        public bool zombi;

        [HideInInspector]
        /// <summary>
        /// 用于判断是否第一次触发了mummy死亡
        /// </summary>
        public bool mummy;
        MainView mainView;
        [HideInInspector]
        /// <summary>
        /// 用于存储引导键值对
        /// </summary>
        public Dictionary<string, string> guideDic = new Dictionary<string, string>();

        public IEnumerator stopGuide2;

        void Awake()
        {
    
    
            instance = this;
            mainView = MainView.instance;
            stopGuide2 = null;
            foreach (GameObject temp in Guides)
            {
    
    
                temp.SetActive(false);
            }
        }

        //temp Before blackDragon enter.
        public void ShowBlackDragonGuide()
        {
    
    
            KnapsackView kView = KnapsackView.instance;
            MainView mView = MainView.instance;
            //先让蒙版遮住所有,获取物品的面板消失
            kView.settleMentPop.SetActive(false);
            mView.guideMask.SetActive(true);
            mView.guideMask.transform.SetAsLastSibling();
            mView.blackDragonTip.transform.SetAsLastSibling();

            kView.closeButton.interactable = false;
            if (kView.inventoryPopup.closeButton)
                kView.inventoryPopup.closeButton.interactable = false;

            foreach (GameObject item in tapTipList)
            {
    
    
                item.SetActive(false);
            }
            mView.blackDragonTip.SetActive(true);
            //游戏暂停
            //Time.timeScale = 0;
        }

        public IEnumerator ShowRedDragonGuide()
        {
    
    
            KnapsackView kView = KnapsackView.instance;
            MainView mView = MainView.instance;
            yield return new WaitForSeconds(2);
            //先让蒙版遮住所有,获取物品的面板消失
            kView.settleMentPop.SetActive(false);
            mView.guideMask.SetActive(true);
            mView.guideMask.transform.SetAsLastSibling();
            mView.redDragonTip.transform.SetAsLastSibling();

            kView.closeButton.interactable = false;
            if (kView.inventoryPopup.closeButton)
                kView.inventoryPopup.closeButton.interactable = false;

            foreach (GameObject item in tapTipList)
            {
    
    
                item.SetActive(false);
            }
            mView.redDragonTip.SetActive(true);
            //游戏暂停
            //Time.timeScale = 0;
        }


        /// <summary>
        /// 常规引导流程,常规引导操作
        /// </summary>
        void GuidePrepare()
        {
    
    
            KnapsackView kView = KnapsackView.instance;
            MainView mView = MainView.instance;
            //先让蒙版遮住所有,获取物品的面板消失
            kView.settleMentPop.SetActive(false);
            mView.guideMask.SetActive(true);
            mView.guideMask.transform.SetAsLastSibling();

            kView.closeButton.interactable = false;
            if (kView.inventoryPopup.closeButton)
                kView.inventoryPopup.closeButton.interactable = false;

            foreach (GameObject item in tapTipList)
            {
    
    
                item.SetActive(false);

            }
            //tapTipList[nextStep - 1].SetActive(true);

            //Operations.GuideStepUpdate(PlayerController.instance.PlayerMe.UserId, nextStep + 1);

            //游戏暂停
            //Time.timeScale = 0;

        }

        //public void UpdateStepRequest()
        //{
    
    
        //    Operations.GuideStepUpdate(PlayerController.instance.PlayerMe.UserId, nextStep + 1);
        //}

        //喂食引导,挂在按钮上调用
        public void ObjectGuideOn()
        {
    
    
            MainView mView = MainView.instance;
            //KnapsackView kView = KnapsackView.instance;
            mView.objectPanel.SetActive(true);
            //激活华夫饼跳动
            //kView.objectItUI.GetComponentInChildren<ObjectButton>().objectImgAnim.enabled = true;
            GuidePrepare();
            mView.objectPanel.transform.SetParent(mView.knapsackCanvas.transform);
            mView.objectPanel.transform.SetAsLastSibling();

            //出小手
            ShowGuideSolo(6);
        }

        public void ObjectGuideOff()
        {
    
    
            MainView mainView = MainView.instance;
            mainView.objectPanel.transform.SetParent(mainView.bottomButtonGroup.transform);
            mainView.objectPanel.transform.SetSiblingIndex(1);
            AfterGuide();
        }

        public void DelayPet1GuideOn(float time, int step)
        {
    
    
           
            //if (delayPet1Guide_cor == null)
            //{
    
    
            delayPet1Guide_cor = DelayPet1GuideOnCor(time, step);
            //}
            StartCoroutine(delayPet1Guide_cor);

        }

        public void DelayPet1GuideOff()
        {
    
    
            if (delayPet1Guide_cor != null)
                StopCoroutine(delayPet1Guide_cor);
        }

        IEnumerator delayPet1Guide_cor;

        //等两秒,跳过物品展示框后,出现召唤怪物引导
        IEnumerator DelayPet1GuideOnCor(float time, int step)
        {
    
    
            yield return new WaitForSeconds(time);

            KnapsackView kView = KnapsackView.instance;

            //如果按钮上有东西或者是需要切换宠物,才能触发引导
            if (kView.pet1ItUI.inventoryItem != null || step == 8)
            {
    
    
                //Debug.LogError(222);
                Pet1GuideOn(step);
            }

        }
        public void Pet1GuideOn(int step)
        {
    
    
            MainView mView = MainView.instance;

            GuidePrepare();
            mView.pet1Panel.transform.SetParent(mView.knapsackCanvas.transform);
            mView.pet1Panel.transform.SetAsLastSibling();
            //出小手
            ShowGuideSolo(step);

        }

        public void Pet1GuideOff()
        {
    
    
            MainView mainView = MainView.instance;
            mainView.pet1Panel.transform.SetParent(mainView.bottomButtonGroup.transform);
            mainView.pet1Panel.transform.SetSiblingIndex(1);
            AfterGuide();

        }

        public void SkillGuideOn()
        {
    
    
            MainView mView = MainView.instance;
            mView.clickSkillPanel.SetActive(true);

            //GuidePrepare();
            //mView.clickSkillPanel.transform.SetParent(mView.mainCanvas.transform);
            //mView.clickSkillPanel.transform.SetAsLastSibling();

            //引导中才可以跳动
            //if (inGuide)
            //{
    
    
            //    mView.clickSkillPanel.GetComponentInChildren<SkillButton>().skillImageAnim.enabled = true;
            //}
        }

        public void SkillGuideOff()
        {
    
    
            //MainView mainView = MainView.instance;
            //mainView.clickSkillPanel.transform.SetParent(mainView.bottomButtonGroup.transform);
            //mainView.clickSkillPanel.transform.SetSiblingIndex(1);
            //AfterGuide();
        }

        /// <summary>
        /// 辅助的单独的引导,往往是再次提醒和引导,只需出现三次,不会暂停游戏
        /// </summary>
        void ShowGuideSolo(int step)
        {
    
    
            //Debug.LogError(111);
            tapTipList[step - 1].SetActive(true);
        }

        public void ShowGuide(int step)
        {
    
    
            tapTipList[step - 1].SetActive(true);

        }
        /// <summary>
        /// 引导步骤大于7的话,就结束Unity端的引导喽
        /// </summary>
        /// <param name="newStep"></param>
        /// <param name="onGet"> 是get到的step还是更新的step</param>
        public void GuideStepChange(int newStep, bool onGet)
        {
    
    
            nextStep = newStep;
            Debug.LogError("nextStep:  " + nextStep);

            //3D端引导完,nextStep值为7
            if (nextStep > 1)
            {
    
    
                inGuide = false;

                BattleController.instance.CountDown();

                //不能在这里将InGuide设为0,因为这个时候不一定打完引导中的怪
            }
            else
            {
    
    
                inGuide = true;

            }

            //if (onGet)
            //{
    
    
            //    //如果大招引导没执行,还是要先召唤宠物
            //    if (nextStep == 4)
            //    {
    
    
            //        DelayGuide4();
            //    }
            //    //回退一步
            //    else if (nextStep == 5)
            //    {
    
    
            //        nextStep = 4;
            //        DelayGuide4();
            //    }
            //    return;
            //}
            避免背包过早打开,误触发引导
            //if (nextStep == 1)
            //{
    
    
            //    MainView.instance.backPack.SetActive(false);
            //}
            提示选择宠物蛋
            //else if (nextStep == 2)
            //{
    
    
            //    ShowGuide(2);
            //}
            //else if (nextStep == 3)
            //{
    
    
            //    ShowGuide(3);
            //}
            //else if (nextStep == 4)
            //{
    
    
            //    DisableGuide();
            //}
            //else if (nextStep == 5)
            //{
    
    
            //    DisableGuide();
            //}
            //else if (nextStep == 6)
            //{
    
    
            //    DisableGuide();
            //}
            //else if (nextStep == 7)
            //{
    
    
            //    DisableGuide();
            //}

        }
        public void AfterGuide()
        {
    
    
            MainView mView = MainView.instance;
            KnapsackView kView = KnapsackView.instance;
            Time.timeScale = 1;

            mView.guideMask.SetActive(false);

            kView.closeButton.interactable = true;
            if (kView.inventoryPopup.closeButton)
                kView.inventoryPopup.closeButton.interactable = true;

            foreach (GameObject item in tapTipList)
                item.SetActive(false);
        }

        /// <summary>
        /// 打开引导弹窗
        /// </summary>
        /// <param name="step"> 步骤</param>
        public void StepControl(int step,float time) {
    
    
            PlayerController playerCon = PlayerController.instance;
            Debug.Log("StepSign:" + StepSign + "    参数step :" + step);
            if (step > StepSign)
            {
    
    
                StepSign++;
                if (step >= 4) {
    
    
                    StepSign = step;
                }
                foreach (GameObject temp in Guides)
                {
    
    
                    if (temp != null)
                        temp.SetActive(false);
                }
                step -= 1;//数组里是从0开始的

                //if (StepSign == 3)
                //{
    
    
                //    mainView.Guides[2].SetActive(true);
                //    stopGuide2 = StopGuide(5);
                //    StartCoroutine(stopGuide2);
                //}

                
                if (StepSign != 6 && Guides[step] != null) {
    
    
                    Guides[step].SetActive(true);
                }
                // StepSign == 6 则引导结束
                if (playerCon.PlayerMe != null) {
    
    
                    string val = "";
                    if (guideDic.TryGetValue("BattleStep", out val))
                    {
    
    
                        guideDic["BattleStep"] = StepSign.ToString();
                    }
                    else
                    {
    
    
                        //不存在,则添加
                        guideDic.Add("BattleStep", StepSign.ToString());
                    }
                    Operations.GuideStepUpdate(playerCon.PlayerMe.UserId, JsonMapper.ToJson(guideDic));

                }
                if (time > 0) {
    
    
                    StartCoroutine(StopGuide(time));
                }
            }
            Debug.LogError("StepSign : " + StepSign);

        }
        public void CloseGuide() {
    
    
            foreach (GameObject temp in Guides)
            {
    
    
                if (temp != null)
                    temp.SetActive(false);
            }
            foreach (GameObject temp in tapTips)
            {
    
    
                if (temp != null)
                    temp.SetActive(false);
            }
        }
        IEnumerator StopGuide(float time) {
    
    
            yield return new WaitForSeconds(time);
            foreach (GameObject temp in Guides)
            {
    
    
                if (temp != null)
                    temp.SetActive(false);
            }
            foreach (GameObject temp in tapTips)
            {
    
    
                if (temp != null)
                    temp.SetActive(false);
            }
        }

        public void OpenTipsPanel(int index, float time) {
    
    
            for(int i = 0;i< tapTips.Count;i++)
            {
    
    
                if (tapTips[i] != null) {
    
    
                    if(i == index)
                        tapTips[i].SetActive(true);
                    else
                        tapTips[i].SetActive(false);
                }
            }
            if (time > 0)
            {
    
    
                StartCoroutine(StopGuide(time));
            }
        }
        public void OpenTipsPanel(int index, float time,string texts)
        {
    
    
            foreach (GameObject temp in tapTips)
            {
    
    
                if (temp != null)
                    temp.SetActive(false);
            }

            if (tapTips[index] != null) {
    
    
                if (texts != null)
                {
    
    
                    tapTips[index].transform.Find("Text").GetComponent<Text>().text = texts;
                }
                tapTips[index].SetActive(true);
            }
            if (time > 0)
            {
    
    
                StartCoroutine(StopGuide(time));
            }
        }

        public void GetPlayerGuide() {
    
    
            UnityPhotonClient unityPhotonClient = UnityPhotonClient.Instance;
            //Debug.Log("player.UserId :" + unityPhotonClient.player.UserId);

            //如果是注册,第一次登录
            if (unityPhotonClient.player.Registered)
            {
    
    
                Debug.Log("第一次注册,发送引导请求");
                //Operations.GuideStepUpdate(player.UserId, 1);//------------------------------cl
                Operations.GetGuideStep(unityPhotonClient.player.UserId);//------------------------------cl
            }
            //以后登录
            else
            {
    
    
                //获取玩家引导步数,方便判断是否需要引导
                Operations.GetGuideStep(unityPhotonClient.player.UserId);//------------------------------cl
                Debug.Log("战斗前先获取引导Guide :" + StepSign);
                //if (guideCon.StepSign == 0) {
    
    
                //}
            }
        }


    }

在这里插入图片描述

效果如下图:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43505432/article/details/111468227