目次
1. Excel構成テーブルのtxtファイルをロードします(読み取りと書き込み)
エンジニアリングリソース
https://github.com/AMikeW/BStandShaderResources
***リソースの使用に関する注意事項***
1つはシナリオで、もう1つは、ログログクラスが印刷ログの表示を開始するために必要なスクリプト定義マークです。もちろん、UnityEngine.Debugを使用すれば問題ありません。
1. Excel構成テーブルのtxtファイルをロードします(読み取りと書き込み)
.txtテキストファイルとして直接保存するか、サフィックスを.txtに直接変更してテキストファイルに変換します
Unityディレクトリに配置します。必ずしもResources特別ディレクトリに配置する必要はありません。ここでは、テスト用です。
using GameFramework.DataTable;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;
public class TestConfig : IDataRow
{
public int Id { get; protected set; }
public string Name { get; protected set; }
public int Atk { get; protected set; }
public bool ParseDataRow(string dataRowString, object userData)
{
try
{
string[] text = dataRowString.Split('\t');
int index = 0;
index++;
Id = int.Parse(text[index++]);
Name = text[index++];
Atk = int.Parse(text[index++]);
}
catch (Exception e)
{
Log.Error("TestConfig ParseDataRow Error:" + e.ToString());
return false;
}
return true;
}
public bool ParseDataRow(byte[] dataRowBytes, int startIndex, int length, object userData)
{
throw new System.NotImplementedException();
}
public override string ToString()
{
return string.Format("Id:{0},Name:{1},Atk:{2}", Id, Name, Atk);
}
}
private void StartLoadConfig()
{
//监听加载配置表成功事件
EventComponent Event = GameEntry.GetComponent<EventComponent>();
Event.Subscribe(LoadDataTableSuccessEventArgs.EventId, OnDataLoadedSuccess);
// 获取框架数据表组件
DataTableComponent dataTableComponent
= UnityGameFramework.Runtime.GameEntry.GetComponent<DataTableComponent>();
IDataTable<TestConfig> dataTable = dataTableComponent.CreateDataTable<TestConfig>();
DataTableBase dataTableBase = dataTable as DataTableBase;
dataTableBase.ReadData("Assets/AssetBundles/Resources/Test.txt");
}
private void OnDataLoadedSuccess(object sender, GameEventArgs e)
{
Log.Error("加载成功:");
DataTableComponent dataTableComponent = GameEntry.GetComponent<DataTableComponent>();
IDataTable<TestConfig> dtScene = dataTableComponent.GetDataTable<TestConfig>();
TestConfig[] allTestConfigs = dtScene.GetAllDataRows();// 所有的行获取为数组
Log.Debug("allTestConfig : " + allTestConfigs.Length);
TestConfig first = dtScene.GetDataRow(1); //获取的是ID为1的TestConfig
if (first != null)
{
Log.Debug("first id = 1:" + first.ToString());
}
else
{
}
//获取所有满足条件的行
TestConfig[] testConfigsIdGreater0s = dtScene.GetDataRows((x) => { return x.Id > 0; });
foreach (var v in testConfigsIdGreater0s)
{
Log.Debug("testConfigsIdGreater0 :" + v.ToString());
}
//获取第一次满足条件的行 返回它
TestConfig testConfigsNameEqualCondition = dtScene.GetDataRow((x) => { return x.Name == "mutou"; });
Log.Debug("testConfigsNameEqualCondition:" + testConfigsNameEqualCondition.ToString());
}
可能性とヒント:
①。ここでのロードパスは、アセットの先頭の相対パスでなければならないことに注意してください。
②旧バージョンで構成テーブルをロードする方法は、DataTableComponent.LoadDataTable <T>(string)です。
2.GameFrameworkの手順プロセス
上の図に示すように、エントリプロセスはProcedureLaunchプロセスであり、次のように、ProcedureBaseから継承されたC#スクリプトです。
using GameFramework.Procedure;
using UnityGameFramework.Runtime;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;
public class ProcedureLaunch : ProcedureBase
{
protected override void OnEnter(ProcedureOwner procedureOwner)
{
base.OnEnter(procedureOwner);
Log.Debug("初始化!");
SceneComponent scene = UnityGameFramework.Runtime.GameEntry.GetComponent<SceneComponent>();
scene.LoadScene("Assets/Scenes/Menu.unity", this);
ChangeState<ProcedureMenu>(procedureOwner);
}
}
ゲームの実行中は、初期プロセスのOnEnterメソッドに入ります。別のプロシージャプロセスに切り替える必要がある場合は、ProcedureBaseクラスメソッドChangeState <T>(ProcedureOwner)を使用して次のプロセスに進みます。難しいことではありません。それが単なるステートマシンであることを見つけるために。OnEnterがある場合、OnExitメソッドがある場合、および他のメソッドがある場合は、自分でテストしてください。上記のメソッドもSceneComponentクラスオブジェクトを使用してシーンをロードします。これは同期追加ロードです。つまり、既存のシーンは破棄されません。ロード後、次の図に示されています。SceneComponentクラスは、GameFrameworkオブジェクトの下にあるサブオブジェクトSceneのスクリプト化されたSceneです(ピット①を残します)。
メニューシーンに入ってProcedureMenuプロセスに切り替えた後(現時点では、シーンがロードされた後にプロシージャが入力され、ピットを離れているかどうかは不明です②)。メニュープロセスに入った後、パネルをロードする予定です。パネルに[ゲームシーンに入る]ボタンを設定し、ProcedureGameプロセスに切り替えます。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameFramework;
using GameFramework.Procedure;
using UnityGameFramework.Runtime;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;
using GameFramework.Fsm;
using GameFramework.UI;
using GameFramework.Event;
public class ProcedureMenu : ProcedureBase
{
ProcedureOwner procedureOwner;
protected override void OnEnter(ProcedureOwner procedureOwner)
{
base.OnEnter(procedureOwner);
Log.Debug("菜单场景 加载UI");
//GameFramework框架的事件系统
EventComponent Event = GameEntry.GetComponent<EventComponent>();
//EventName是一个枚举,它是用户自定义的枚举,代表事件ID(必须唯一)
Event.Subscribe((int)EventName.GameStart, OnGameStart);
//加载UI并显示UI
UIComponent UI = GameEntry.GetComponent<UIComponent>();
//(按顺序进行出现) 第三个参数优先级priority暂时未发现用途
int id = UI.OpenUIForm("Assets/AssetBundles/Prefab/UIMenu.prefab", "NormalGroup");
this.procedureOwner = procedureOwner;
}
/// <summary>
/// 触发EventName.GameStart事件时的回调方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnGameStart(object sender, GameEventArgs e)
{
ChangeState<ProcedureGame>(procedureOwner);
}
}
UIMenu.prefabは、UIFormLogicから継承されたスクリプトをマウントし、OnInit OnOpenなどの独自のライフサイクルメソッドのいくつかを実装します。ボタンを押すと、現在のメニューシーンが破棄され、ゲームシーンが同期的に追加され、イベントが追加されます。 ProcedureMenuプロセスによって監視されると、Classオブジェクト(そのIDはEventName.GameStart)が送信され、UIComponentのclose UIメソッドを呼び出して、現在のUIを閉じます。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;
public class UIMenu : UIFormLogic
{
private Button button;
private void Awake()
{
}
protected override void OnInit(object userData)
{
base.OnInit(userData);
button = transform.Find("EnterGameBtn").GetComponent<Button>();
}
protected override void OnOpen(object userData)
{
base.OnOpen(userData);
button.onClick.AddListener(EnterGame);
}
protected override void OnClose(bool isShutdown, object userData)
{
base.OnClose(isShutdown, userData);
button.onClick.RemoveAllListeners();
}
protected override void OnRecycle()
{
base.OnRecycle();
button = null;
}
public void EnterGame()
{
SceneComponent scene = GameEntry.GetComponent<SceneComponent>();
卸载所有场景
//string[] loadedSceneAssetNames = scene.GetLoadedSceneAssetNames();
//for (int i = 0; i < loadedSceneAssetNames.Length; i++)
//{
// scene.UnloadScene(loadedSceneAssetNames[i]);
//}
scene.UnloadScene("Assets/Scenes/Menu.unity", this);
scene.LoadScene("Assets/Scenes/Game.unity", this);
EventComponent Event = GameEntry.GetComponent<EventComponent>();
Event.Fire(this, new CommonGameEventArgs((int)EventName.GameStart));
UIComponent UI = GameEntry.GetComponent<UIComponent>();
UI.CloseUIForm(UIForm);//关闭自身
}
}
その中で、CommonGameEventArgsは、パブリックゲームイベントパラメータクラスと呼ばれるカスタムイベントパラメータクラスであり、着信イベントIDが固定IDではなく独自のIDを動的に設定できるようにしますが、これには欠点もあり、書き込みもできません。データやその他の状況を処理するための多くの非公開の方法。(この例は参照用です。実際の戦闘では、単一のIDとクラスを用意し、GameFrameworkの組み込みイベントパラメータークラスを模倣することをお勧めします)
using GameFramework.Event;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CommonGameEventArgs : GameEventArgs
{
private int m_Id;
public override int Id => m_Id;
public CommonGameEventArgs(int id)
{
m_Id = id;
}
public override void Clear()
{
}
}
ゲームに参加するためのボタンをクリックすると、ProcedureMenuクラスのOnGameStartメソッドがトリガーされ、イベントがディスパッチされた後にProcedureGameプロセスにジャンプします。
ProcedureGameプロセスは、構成テーブルをロードし、組み込みのロード構成成功イベントLoadDataTableSuccessEventArgs.EventIdをリッスンします。
using GameFramework.Procedure;
using UnityGameFramework.Runtime;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;
using GameFramework.Event;
using GameFramework.DataTable;
public class ProcedureGame : ProcedureBase
{
protected override void OnEnter(ProcedureOwner procedureOwner)
{
base.OnEnter(procedureOwner);
Log.Debug("ProcedureGame 状态进入!");
EventComponent Event = GameEntry.GetComponent<EventComponent>();
Event.Subscribe(LoadDataTableSuccessEventArgs.EventId, OnDataLoadedSuccess);
// 获取框架数据表组件
DataTableComponent dataTableComponent
= UnityGameFramework.Runtime.GameEntry.GetComponent<DataTableComponent>();
IDataTable<TestConfig> dataTable = dataTableComponent.CreateDataTable<TestConfig>();
DataTableBase dataTableBase = dataTable as DataTableBase;
dataTableBase.ReadData("Assets/AssetBundles/Resources/Test.txt");
}
private void OnDataLoadedSuccess(object sender, GameEventArgs e)
{
UIComponent UI = GameEntry.GetComponent<UIComponent>();
int id = UI.OpenUIForm("Assets/AssetBundles/Prefab/UIGame.prefab", "NormalGroup", this);
}
}
UIGameクラスは、以下に示すように、ロードされた構成テーブルの内容をテストするためのUIクラスです。
using GameFramework.DataTable;
using GameFramework.Event;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;
public class UIGame : UIFormLogic
{
Button button;
protected override void OnInit(object userData)
{
base.OnInit(userData);
button = transform.Find("Button").GetComponent<Button>();
}
protected override void OnOpen(object userData)
{
base.OnOpen(userData);
button.onClick.AddListener(OnClick);
}
protected override void OnClose(bool isShutdown, object userData)
{
base.OnClose(isShutdown, userData);
button.onClick.RemoveAllListeners();
}
private void OnClick()
{
//获取配置表TestConfig表
DataTableComponent dataTableComponent = GameEntry.GetComponent<DataTableComponent>();
IDataTable<TestConfig> dtScene = dataTableComponent.GetDataTable<TestConfig>();
TestConfig[] allTestConfigs = dtScene.GetAllDataRows();// 所有的行获取为数组
//注意:Config必须要有一个int名为id的字段
TestConfig first = dtScene.GetDataRow(1); //获取的是ID为1的TestConfig
if (first != null)
{
Log.Debug("first id = 1:" + first.ToString());
}
else
{
}
//获取所有满足条件的行
TestConfig[] testConfigsIdGreater0s = dtScene.GetDataRows((x) => { return x.Id > 0; });
foreach (var v in testConfigsIdGreater0s)
{
Log.Debug("testConfigsIdGreater0 :" + v.ToString());
}
//获取第一次满足条件的行 返回它
TestConfig testConfigsNameEqualCondition = dtScene.GetDataRow((x) => { return x.Name == "mutou"; });
Log.Debug("testConfigsNameEqualCondition:" + testConfigsNameEqualCondition.ToString());
}
}
対応するプレハブ、Excelなどを作成するための参照用の関連情報のスクリーンショット。(実際には、Excelは構成テーブルをロードするテーブルです)