編寫Unity工具類
阿新 • • 發佈:2019-02-10
指令碼功能設定:遊戲GM工具(根據物品ID、數量向伺服器傳送資訊,獲取本地所需物品道具)
using UnityEngine; using System.Collections; using UnityEditor; using System.Collections.Generic; using System.Linq; using Tianyu; public class GMUtil : EditorWindow { string[] toolbarStr = new string[] { "標準新增道具", "搜尋新增道具" };//標題欄名稱 int toolbarOption = 0;//預設顯示也籤ID List<ItemNodeState> allItemNodeList; List<HeroNode> allHeroNodeList; bool isAddedListener = false; bool isRemovedListener = false;
//註冊訊息監聽
private void OnEnable()
{
MessageMediator.AddListener<CReadPacket>(MessageMediatType.gm_add_player_data_ret, ReceiveServGMMsg);
}
//銷燬訊息監聽 private void OnDisable() { MessageMediator.RemoveListener<CReadPacket>(MessageMediatType.gm_add_player_data_ret, ReceiveServGMMsg); }
//Window面板繪製 static GMUtil window; [MenuItem("Tools/GM工具 #&Q")] static void Main() { window = (GMUtil)EditorWindow.GetWindow(typeof(GMUtil), false, "GM工具"); window.Show(); } private void OnGUI() { if(!Application.isPlaying || SceneManage.Instance.SceneID != EnumSceneID.UI_MajorCity01) { GUILayout.Label("請進入主城後再使用GM工具"); return; } else {
//初始化獲取所有物品表資訊
if (isAddedListener == false)
{
isAddedListener = true;
}
if (allItemNodeList == null)
{
allItemNodeList = GameLibrary.Instance().ItemStateList.Values.ToList<ItemNodeState>();
}
if (allHeroNodeList == null)
{
allHeroNodeList = FSDataNodeTable<HeroNode>.GetSingleton().DataNodeList.Values.ToList<HeroNode>();
}
}
toolbarOption = GUILayout.Toolbar(toolbarOption, toolbarStr);
switch (toolbarOption)
{
case 0:
GetItemFromId();
break;
case 1:
GetItemFromSearch();
break;
}
}
int inputItemId;
int inputItemCount;
void GetItemFromId()
{
EditorGUILayout.Space();
//儲存輸入的道具ID和道具數量
inputItemId = EditorGUILayout.IntField("道具Id:", inputItemId);
inputItemCount = EditorGUILayout.IntField("道具數量:", inputItemCount);
EditorGUILayout.Space();
if (GUILayout.Button("確定"))
{
//向伺服器傳送
CommonGMTool.OnSubmitRequest("ty", inputItemId, inputItemCount);
}
}
string inputItemName;
string fullItemName;
bool isSearching = false;
Vector2 scrollPosition;
List<ItemNodeState> itemNodeList;
List<HeroNode> heroNodeList;
ItemData selectItemData;
bool isSending = false;
//提供模糊字搜尋功能檢索使用者可能需要的道具
void GetItemFromSearch()
{
EditorGUILayout.Space();
inputItemName = EditorGUILayout.TextField("道具名稱:", inputItemName);
EditorGUILayout.Space();
if (GUILayout.Button("模糊搜尋"))
{
Search();
}
if(isSearching)
{
//繪製根據使用者輸入的模糊字檢索出來的道具列表
EditorGUILayout.Space();
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
if (itemNodeList != null)
{
for (int i = 0; i < itemNodeList.Count; i++)
{
GUILayout.BeginHorizontal();
GUILayout.Label(itemNodeList[i].props_id.ToString());
GUILayout.Label(itemNodeList[i].name);
if (GUILayout.Button("選中"))
{
selectItemData = new ItemData();
selectItemData.id = itemNodeList[i].props_id;
selectItemData.name = itemNodeList[i].name;
isSearching = false;
isSending = true;
}
GUILayout.EndHorizontal();
}
}
if (heroNodeList != null)
{
for (int i = 0; i < heroNodeList.Count; i++)
{
GUILayout.BeginHorizontal();
GUILayout.Label(heroNodeList[i].hero_id.ToString());
GUILayout.Label(heroNodeList[i].name);
if (GUILayout.Button("選中"))
{
selectItemData = new ItemData();
selectItemData.id = heroNodeList[i].hero_id;
selectItemData.name = heroNodeList[i].name;
isSearching = false;
isSending = true;
}
GUILayout.EndHorizontal();
}
}
GUILayout.EndScrollView();
}
if(isSending && selectItemData != null)
{
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.LabelField("道具Id:", selectItemData.id.ToString());
EditorGUILayout.LabelField("道具名稱:", selectItemData.name.ToString());
inputItemCount = EditorGUILayout.IntField("道具數量:", inputItemCount);
EditorGUILayout.Space();
if (GUILayout.Button("確定"))
{
CommonGMTool.OnSubmitRequest(GetTypeById(selectItemData.id), selectItemData.id, inputItemCount);
notificationText = "[ID:" + selectItemData.id + " 名稱:" + selectItemData.name + "] 獲取中……";
}
GUIStyle fontStyle = new GUIStyle();
fontStyle.normal.background = null; //設定背景填充
fontStyle.normal.textColor = new Color(0, 1, 0); //設定字型顏色
fontStyle.fontSize = 11; //字型大小
GUILayout.Label(notificationText, fontStyle);
}
else
{
notificationText = "";
}
this.Repaint();
}
void Search()
{
itemNodeList = new List<ItemNodeState>();
heroNodeList = new List<HeroNode>();
try
{
var itemNodeEnumerator = allItemNodeList.GetEnumerator();
while (itemNodeEnumerator.MoveNext())
{
if (itemNodeEnumerator.Current.name.Contains(inputItemName))
{
itemNodeList.Add(itemNodeEnumerator.Current);
}
}
var heroNodeEnumerator = allHeroNodeList.GetEnumerator();
while (heroNodeEnumerator.MoveNext())
{
if (heroNodeEnumerator.Current.name.Contains(inputItemName))
{
heroNodeList.Add(heroNodeEnumerator.Current);
}
}
}
catch (System.Exception)
{
}
isSending = false;
isSearching = true;
}
string notificationText = "";
void ReceiveServGMMsg(CReadPacket packet)
{
if (packet == null || packet.data == null)
{
notificationText = "[ID:" + selectItemData.id + " 名稱:" + selectItemData.name + "] 獲取失敗";
return;
}
string str = packet.data.ContainsKey("ret") ? "成功" : "失敗";
notificationText = "[ID:" + selectItemData.id + " 名稱:" + selectItemData.name + "] 獲取" + str;
}
/// <summary>
/// 根據型別獲取物品型別
/// </summary>
private string GetTypeById(long Id)
{
ItemNodeState ins = null;
GameLibrary.Instance().ItemStateList.TryGetValue(Id, out ins);
if (ins != null)
{
return ins.types + "";
}
else
{
HeroNode node = null;
FSDataNodeTable<HeroNode>.GetSingleton().DataNodeList.TryGetValue(Id, out node);
if (node != null)
{
return node.types + "";
}
}
return string.Empty;
}
class ItemData
{
public long id;
public string name;
}
}