GameFramework框架新建场景及流程跳转

  1. 新建场景Gameone,拼好UI并拖入UI预制体,例如GameoneForm
  2. 创建GameoneForm脚本挂载在UI预制上,并实现下面两个方法,否则场景UI会歪

GameFramework框架新建场景及流程跳转_第1张图片

  1. 在DefaultConfig表中添加Scene.Gameone(即场景名)
    GameFramework框架新建场景及流程跳转_第2张图片
  2. 在Scene表中添加场景编号和资源名称
    GameFramework框架新建场景及流程跳转_第3张图片
  3. 在UIForm表中添加界面编号,备注,和资源名称,UI预制的名字要打对
    GameFramework框架新建场景及流程跳转_第4张图片
  4. 在UIFormId中添加新UI预制的名字与编号
    GameFramework框架新建场景及流程跳转_第5张图片
  5. 新建流程ProcedureGameone
using GameFramework;
using GameFramework.DataTable;
using GameFramework.Event;
using GameFramework.Fsm;
using GameFramework.Procedure;
using GameFramework.Resource;

using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;

namespace StarForce
{
    public class ProcedureGameone : ProcedureBase
    {
        private const int gameOneSceneId = 5;
        public bool changeToGameOne = false;

        protected override void OnInit(ProcedureOwner procedureOwner)
        {
            base.OnInit(procedureOwner);
        }

        protected override void OnEnter(ProcedureOwner procedureOwner)
        {
            base.OnEnter(procedureOwner);
            //停止所有声音
            GameEntry.Entity.HideAllLoadingEntities();
            GameEntry.Entity.HideAllLoadedEntities();

            //隐藏所有实体
            GameEntry.Entity.HideAllLoadingEntities();
            GameEntry.Entity.HideAllLoadedEntities();

            //卸载所有场景
            string[] loadedSceneAssetNames = GameEntry.Scene.GetLoadedSceneAssetNames();
            for (int i = 0; i < loadedSceneAssetNames.Length; i++)
            {
                GameEntry.Scene.UnloadScene(loadedSceneAssetNames[i]);
            }

            //还原游戏速度
            GameEntry.Base.ResetNormalGameSpeed();
            //获取上一流程设置的NextSceneId
            int sceneId = procedureOwner.GetData<VarInt>(Constant.ProcedureData.NextSceneId).Value;


            //读表 
            IDataTable<DRScene> dRScenes = GameEntry.DataTable.GetDataTable<DRScene>();
            //获取其中的某一行数据
            DRScene dRScene = dRScenes.GetDataRow(gameOneSceneId);
            //切换场景
            GameEntry.Scene.LoadScene(AssetUtility.GetSceneAsset(dRScene.AssetName), this);//(某一行数据中的名称,此流程)
            //打开登录UI 参数为(枚举脚本里的LoginForm,此流程)
            GameEntry.UI.OpenUIForm(UIFormId.GameoneForm, this);
        }
        protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
        }
        

        public override bool UseNativeDialog
        {
            get
            {
                return false;
            }
        }
    }
}


  1. 在当前场景流程中添加切换场景及流程的代码,例如当前为ProcedureCreate 流程,如脚本所示.
using GameFramework;
using GameFramework.DataTable;
using GameFramework.Event;
using GameFramework.Fsm;
using GameFramework.Procedure;
using GameFramework.Resource;

using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;

namespace StarForce
{
    public class ProcedureCreate : ProcedureBase
    {
        private const int createSceneId = 4;
        private bool changeToGameOne = false;

        protected override void OnInit(ProcedureOwner procedureOwner)
        {
            base.OnInit(procedureOwner);
        }

        protected override void OnEnter(ProcedureOwner procedureOwner)
        {
            base.OnEnter(procedureOwner);
            //停止所有声音
            GameEntry.Entity.HideAllLoadingEntities();
            GameEntry.Entity.HideAllLoadedEntities();

            //隐藏所有实体
            GameEntry.Entity.HideAllLoadingEntities();
            GameEntry.Entity.HideAllLoadedEntities();

            //卸载所有场景
            string[] loadedSceneAssetNames = GameEntry.Scene.GetLoadedSceneAssetNames();
            for (int i = 0; i < loadedSceneAssetNames.Length; i++)
            {
                GameEntry.Scene.UnloadScene(loadedSceneAssetNames[i]);
            }

            //还原游戏速度
            GameEntry.Base.ResetNormalGameSpeed();
            //获取上一流程设置的NextSceneId
            int sceneId = procedureOwner.GetData<VarInt>(Constant.ProcedureData.NextSceneId).Value;


            //读表 
            IDataTable<DRScene> dRScenes = GameEntry.DataTable.GetDataTable<DRScene>();
            //获取其中的某一行数据
            DRScene dRScene = dRScenes.GetDataRow(createSceneId);
            //切换场景
            GameEntry.Scene.LoadScene(AssetUtility.GetSceneAsset(dRScene.AssetName), this);//(某一行数据中的名称,此流程)
            //打开登录UI 参数为(枚举脚本里的LoginForm,此流程)
            GameEntry.UI.OpenUIForm(UIFormId.CreateForm, this);
        }
        /// 
        /// 每帧执行
        /// 
        /// 
        /// 
        /// 
        protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
            //只要changeToGameOne的bool值变为true,就设置下一个要跳转的场景的id,切换流程
            if (changeToGameOne)
            {
                //设置下一个要跳转的场景的id
                procedureOwner.SetData<VarInt>(Constant.ProcedureData.NextSceneId, GameEntry.Config.GetInt("Scene.Gameone"));
                //切换流程
                ChangeState<ProcedureGameone>(procedureOwner);
            }
        }
        public void ChangeToGameOne()//是否切换的布尔值
        {
            changeToGameOne = true;
        }
        public override bool UseNativeDialog
        {
            get
            {
                return false;
            }
        }
    }
}


  1. 在当前UIForm设置切换场景的触发条件,例如按钮点击事件,给按钮添加监听事件,当按钮按下时触发ProcedureCreate流程中 ChangeToGameOne事件,详细如下
    a. 获取流程
    在这里插入图片描述
    GameFramework框架新建场景及流程跳转_第6张图片

b. 获得按钮,添加监听事件
c. 编写监听事件
GameFramework框架新建场景及流程跳转_第7张图片
d.完整代码如下,附读表游戏对象随机取名实现代码

using System;
using GameFramework;
using UnityEngine;
using UnityEngine.UI;
using GameFramework.DataTable;


namespace StarForce
{
    public class CreateForm : UGuiForm
    {
        private ProcedureCreate procedureCreate = null;
        private Animator animator;
        private Animator animator1;
        private Animator animator2;

        //三个角色预制体
        private GameObject girlgameObject;
        private GameObject boy1gameObject;
        private GameObject gir2gameObject;

        //三个角色按钮
        private Button girl1Btn;
        private Button boy1Btn;
        private Button girl2Btn;

        private Button randomIDBtn;

        private InputField nameIDIpt;
        private Text heroText;
        //创建角色按钮
        private Button CreateBtn;

        protected override void OnInit(object userData)
        {
            base.OnInit(userData);
            InitCom();
        }
        void InitCom()
        {
            girl1Btn = transform.Find("girl1Btn").GetComponent<Button>();
            if (girl1Btn != null)
            {
                girl1Btn.onClick.AddListener(ShowgirlgameObject);
            }
            boy1Btn = transform.Find("boy1Btn").GetComponent<Button>();
            if (boy1Btn != null)
            {
                boy1Btn.onClick.AddListener(Showboy1gameObject);
            }
            girl2Btn = transform.Find("girl2Btn").GetComponent<Button>();
            if (girl2Btn != null)
            {
                girl2Btn.onClick.AddListener(Showgir2gameObject);
            }

            randomIDBtn = transform.Find("RandomIDBtn").GetComponent<Button>();
            if (randomIDBtn != null)
            {
                
                randomIDBtn.onClick.AddListener(RandomIDname);
            }
            CreateBtn = transform.Find("CreateBtn").GetComponent<Button>();
            if (CreateBtn != null)
            {

                CreateBtn.onClick.AddListener(OnCreateSucessText);
            }
            girlgameObject = transform.Find("girl1").gameObject;
            boy1gameObject = transform.Find("boy1").gameObject;
            gir2gameObject = transform.Find("girl2").gameObject;

            nameIDIpt = transform.Find("NameIpt").GetComponent<InputField>();
            animator = girlgameObject.GetComponent<Animator>();
            animator1= boy1gameObject.GetComponent<Animator>();
            animator2 = gir2gameObject.GetComponent<Animator>();
            heroText = transform.Find("heroDescriImg/heroDescriText").GetComponent<Text>();
        }

        protected override void OnOpen(object userData)
        {
            //在打开触发中获得流程对象
            if (userData!=null)
            {
                procedureCreate = (ProcedureCreate)userData;
            }
            base.OnOpen(userData);
            //添加创建成功的监听
            EventDispatcher.AddEventListener<bool>(EventKey.OnPlayerCreateSuccess, OnCreateSucess);
        }
        protected override void OnClose(object userData)
        {
            base.OnClose(userData);
            EventDispatcher.RemoveEventListener<bool>(EventKey.OnPlayerCreateSuccess, OnCreateSucess);
        }
        private void OnCreateSucess(bool obj)
        {
            Close();
            //跳转流程
            procedureCreate.ChangeToGameOne();
        }
        private bool isCreateHerosucess = false;
        public bool IsCreateHerosucess
        {
            get { return isCreateHerosucess; }
            set
            {

                if (isCreateHerosucess != value)
                {
                    isCreateHerosucess = value;
                    EventDispatcher.TriggerEvent<bool>(EventKey.OnPlayerCreateSuccess, isCreateHerosucess);
                }
            }
        }
        private void OnCreateSucessText()
        {
            IsCreateHerosucess = true;
        }

        private void RandomIDname()
        {
            IDataTable<DRFirstName> DRname = StarForce.GameEntry.DataTable.GetDataTable<DRFirstName>();
            DRFirstName drname1 = DRname.GetDataRow(UnityEngine.Random.Range(1001, 1434));
            DRFirstName drname2 = DRname.GetDataRow(UnityEngine.Random.Range(1001, 1434));
            DRFirstName drname3 = DRname.GetDataRow(UnityEngine.Random.Range(1001, 1434));

            nameIDIpt.text = drname1.FIRSTNAME + drname2.MIDDLENAME + drname3.LASTNAME;
        }



        private void ShowgirlgameObject()
        {
            SetRoleShow(true, false, false);
            animator.SetBool("win",true);
            Invoke("falseani", 1);
            heroText.text ="集万千宠爱于一身的小师妹";
        }
        void falseani()
        {
            animator.SetBool("win", false);
        }
        void falseani1()
        {
            animator1.SetBool("win", false);
        }
        void falseani2()
        {
            animator2.SetBool("win", false);
        }

        private void Showboy1gameObject()
        {
            SetRoleShow(false, true, false);
            animator1.SetBool("win", true);
            Invoke("falseani1", 1.5f);
            heroText.text = "潇洒帅气的小师弟";

        }
        private void Showgir2gameObject()
        {
            SetRoleShow(false, false, true);
            animator2.SetBool("win", true);
            Invoke("falseani2", 1);
            heroText.text = "妩媚妖娆的小师姐";

        }
        private void SetRoleShow(bool girl1,bool boy1,bool girl2)
        {
            girlgameObject.SetActive(girl1);
            boy1gameObject.SetActive(boy1);
            gir2gameObject.SetActive(girl2);
        }
    }
}


  1. 然后就可以跳转流程和场景还有打开UI界面了

你可能感兴趣的:(GameFramework框架,Unity,GameFramework框架)