一直不知道應該把下一步我們要更新的代碼怎麼歸類,在網上看了下這個分類,我覺得這個完全是針對玩家體驗的,比如說,裝備系統,(揹包)倉庫系統,交易系統這些都是相輔相成的,分開真的好嗎?而這裏的世界系統僅僅涉及一個場景跳轉,我想問,強化,倉庫,交易這些不都發生在遊戲世界中嗎,爲啥說他們是獨立的呢,就因爲看到的不一樣,其實這些都是遊戲世界中的一個部分,說白了,他們只是遊戲經濟系統中的一個部分,因爲這些東西都可以在遊戲虛擬世界中貨幣化,要靠遊戲中提供的經濟模型來支撐,當然單機遊戲裏面經濟模型較爲簡單,因爲不需要考慮貨幣通脹等等問題(這個要放大的話就要寫好多字,我大學主修的就是經濟學【第二學位修的計算機】,建立一個經濟數學模型,我自己沒有信心搞出來,所以我就不多說話了),我們就我們的仙劍demo,來簡單看看遊戲中的這些東西代碼怎麼寫吧,如果做網遊的話,那麼就讓數值策劃去擔心遊戲經濟模型,我們這裏就不考慮了。
首先,我們看看遊戲中的交易代碼是怎麼實現的。
先看看效果吧,看看遊戲裏是什麼樣的。
對話結束後,進入實質的購買界面
可以看到左邊的商店物品列表
右邊是自身的包裹,這個是怎麼實現的呢?我們看下李鐵匠身上綁定的腳本
我們看下這就是npc李鐵匠,上面的AI腳本,我們前面講過了,這裏我們只需要看npcsetup和shopitemlist,npcsetup是一個設置人物npc交互的腳本
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NpcSetup : MonoBehaviour {
public enum NameTagSetup {NpcName,PlayerName,Other};
public enum NpcType {RegularNpc,ShopKeeper,QuestNpc,SaveNpc};
public enum FaceSetup {NpcFace,PlayerFace,Other};
public enum QuestNpcState{StartQuest,InProgress,Complete,AfterComplete};
public NpcType npcType;
public string npcName;
public Texture2D npcFace;
private bool startCountDialogBox;
[System.Serializable]
public class MessageBoxSetting
{
public bool enableNameTag = true;
public NameTagSetup nameTagSetup;
public string otherNameTag;
public bool enableFace = true;
public FaceSetup faceSetup;
public Texture2D otherFace;
[Multiline]
public string message;
}
public List<MessageBoxSetting> dialogSetting = new List<MessageBoxSetting>();
public int questID;
public QuestNpcState questNpcState;
public List<MessageBoxSetting> dialogQuest = new List<MessageBoxSetting>();
public List<MessageBoxSetting> dialogQuestInProgress = new List<MessageBoxSetting>();
public List<MessageBoxSetting> dialogQuestComplete = new List<MessageBoxSetting>();
public List<MessageBoxSetting> dialogQuestAfterComplete = new List<MessageBoxSetting>();
private HeroController playerControl;
private int indexDialog;
private int indexCurrentQuest;
private bool disableMovePlayer;
private Quest_Data questData;
private QuestWindow questWindow;
public GUI_Menu inventory;
private GameObject player;
public static bool resetMessageBox;
public static bool disableNext;
//Editor Variable
[HideInInspector]
public int sizeDialog=0;
[HideInInspector]
public int sizeDialogQuest=0;
[HideInInspector]
public int sizeDialogQuestInProgress=0;
[HideInInspector]
public int sizeDialogQuestComplete=0;
[HideInInspector]
public int sizeDialogQuestAfterComplete=0;
[HideInInspector]
public List<bool> showSizeDialog = new List<bool>();
[HideInInspector]
public List<bool> showSizeDialogQuest = new List<bool>();
[HideInInspector]
public List<bool> showSizeDialogQuestInProgress = new List<bool>();
[HideInInspector]
public List<bool> showSizeDialogQuestComplete = new List<bool>();
[HideInInspector]
public List<bool> showSizeDialogQuestAfterComplete = new List<bool>();
// Use this for initialization
void Start () {
// Change this tag to Npc
if(this.tag != "Npc")
this.tag = "Npc";
if(this.gameObject.layer != 13)
this.gameObject.layer = 13;
indexDialog = 0;
playerControl = GameObject.FindGameObjectWithTag("Player").GetComponent<HeroController>();
if(npcType == NpcType.QuestNpc)
{
questData = GameObject.Find("QuestData").GetComponent<Quest_Data>();
questWindow = GameObject.Find("GUI Manager/QuestWindow").GetComponent<QuestWindow>();
player = GameObject.FindGameObjectWithTag("Player");
inventory = player.transform.FindChild("Inventory").GetComponent<GUI_Menu>();
}
}
// Update is called once per frame
void Update () {
if(npcType == NpcType.RegularNpc || npcType == NpcType.ShopKeeper || npcType == NpcType.SaveNpc)
NpcDetect();
else if(npcType == NpcType.QuestNpc)
QuestNpcDetect();
}
void NpcDetect()
{
if(disableMovePlayer)
DisableMove();
if(startCountDialogBox)
{
SetupDialogBox(indexDialog);
if(Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.KeypadEnter))
{
SoundManager.instance.PlayingSound("Next_Dialog");
if(indexDialog < dialogSetting.Count-1)
{
indexDialog++;
}else
{
if(npcType == NpcType.ShopKeeper || npcType == NpcType.SaveNpc)
CallNextCommand();
indexDialog = 0;
startCountDialogBox = false;
MessageBox.showMessageBox = false;
MessageBox.showNameTag = false;
MessageBox.showFace = false;
Invoke("EnableMovePlayer",0.3f);
}
}
}
}
void QuestNpcDetect()
{
if(disableMovePlayer)
DisableMove();
if(startCountDialogBox)
{
if(questNpcState == QuestNpcState.StartQuest)
SetupDialogBoxQuest(indexDialog,dialogQuest);
else if(questNpcState == QuestNpcState.InProgress)
SetupDialogBoxQuest(indexDialog,dialogQuestInProgress);
else if(questNpcState == QuestNpcState.Complete)
SetupDialogBoxQuest(indexDialog,dialogQuestComplete);
else if(questNpcState == QuestNpcState.AfterComplete)
SetupDialogBoxQuest(indexDialog,dialogQuestAfterComplete);
if(questNpcState == QuestNpcState.StartQuest)
{
if(Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.KeypadEnter) || resetMessageBox)
{
if(!QuestWindow.enableWindow)
SoundManager.instance.PlayingSound("Next_Dialog");
if(indexDialog < dialogQuest.Count-1)
{
indexDialog++;
if(indexDialog == dialogQuest.Count-1)
{
QuestWindow.enableWindow = true;
QuestWindow.enableButtonAccept = true;
disableNext = true;
questWindow.SetupQuestWindow(questID);
}
}else if(!disableNext)
{
disableNext = false;
indexDialog = 0;
startCountDialogBox = false;
MessageBox.showMessageBox = false;
MessageBox.showNameTag = false;
MessageBox.showFace = false;
resetMessageBox = false;
Invoke("EnableMovePlayer",0.3f);
}
}
}else
if(Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.KeypadEnter))
{
SoundManager.instance.PlayingSound("Next_Dialog");
if(questNpcState == QuestNpcState.InProgress)
{
if(indexDialog < dialogQuestInProgress.Count-1)
{
indexDialog++;
}else
{
indexDialog = 0;
startCountDialogBox = false;
MessageBox.showMessageBox = false;
MessageBox.showNameTag = false;
MessageBox.showFace = false;
QuestWindow.enableWindow = false;
Invoke("EnableMovePlayer",0.3f);
}
}
else if(questNpcState == QuestNpcState.Complete)
{
if(indexDialog < dialogQuestComplete.Count-1)
{
indexDialog++;
}else
{
if(!questData.questSetting[indexCurrentQuest].repeatQuest)
{
questData.questSetting[indexCurrentQuest].questState = 3;
}else
{
questData.questSetting[indexCurrentQuest].questState = 0;
}
GiveReward(questID);
indexDialog = 0;
startCountDialogBox = false;
MessageBox.showMessageBox = false;
MessageBox.showNameTag = false;
MessageBox.showFace = false;
Invoke("EnableMovePlayer",0.3f);
}
}
else if(questNpcState == QuestNpcState.AfterComplete)
{
if(indexDialog < dialogQuestAfterComplete.Count-1)
{
indexDialog++;
}else
{
indexDialog = 0;
startCountDialogBox = false;
MessageBox.showMessageBox = false;
MessageBox.showNameTag = false;
MessageBox.showFace = false;
Invoke("EnableMovePlayer",0.3f);
}
}
}
}
}
public void DisableMove()
{
playerControl.dontMove = true;
playerControl.dontClick = true;
}
public void CallDialogBox()
{
startCountDialogBox = true;
MessageBox.showMessageBox = true;
disableMovePlayer = true;
}
void EnableMovePlayer()
{
disableMovePlayer = false;
playerControl.ResetState();
}
void SetupDialogBox(int i)
{
if(dialogSetting[i].enableNameTag)
{
if(dialogSetting[i].nameTagSetup == NameTagSetup.PlayerName)
MessageBox.nameTagStatic = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerStatus>().playerName;
else if(dialogSetting[i].nameTagSetup == NameTagSetup.NpcName)
MessageBox.nameTagStatic = npcName;
else if(dialogSetting[i].nameTagSetup == NameTagSetup.Other)
MessageBox.nameTagStatic = dialogSetting[i].otherNameTag;
MessageBox.showNameTag = true;
}else
{
MessageBox.showNameTag = false;
}
if(dialogSetting[i].enableFace)
{
if(dialogSetting[i].faceSetup == FaceSetup.PlayerFace)
MessageBox.faceStatic = GameObject.FindGameObjectWithTag("Player").GetComponent<HeroController>().heroImage;
else if(dialogSetting[i].faceSetup == FaceSetup.NpcFace)
MessageBox.faceStatic = npcFace;
else if(dialogSetting[i].faceSetup == FaceSetup.Other)
MessageBox.faceStatic = dialogSetting[i].otherFace;
MessageBox.showFace = true;
}else
{
MessageBox.showFace = false;
}
MessageBox.messageStatic = dialogSetting[i].message;
}
void SetupDialogBoxQuest(int i,List<MessageBoxSetting> mBoxQuest)
{
if(mBoxQuest[i].enableNameTag)
{
if(mBoxQuest[i].nameTagSetup == NameTagSetup.PlayerName)
MessageBox.nameTagStatic = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerStatus>().playerName;
else if(mBoxQuest[i].nameTagSetup == NameTagSetup.NpcName)
MessageBox.nameTagStatic = npcName;
else if(mBoxQuest[i].nameTagSetup == NameTagSetup.Other)
MessageBox.nameTagStatic = mBoxQuest[i].otherNameTag;
MessageBox.showNameTag = true;
}else
{
MessageBox.showNameTag = false;
}
if(mBoxQuest[i].enableFace)
{
if(mBoxQuest[i].faceSetup == FaceSetup.PlayerFace)
MessageBox.faceStatic = GameObject.FindGameObjectWithTag("Player").GetComponent<HeroController>().heroImage;
else if(mBoxQuest[i].faceSetup == FaceSetup.NpcFace)
MessageBox.faceStatic = npcFace;
else if(mBoxQuest[i].faceSetup == FaceSetup.Other)
MessageBox.faceStatic = mBoxQuest[i].otherFace;
MessageBox.showFace = true;
}else
{
MessageBox.showFace = false;
}
MessageBox.messageStatic = mBoxQuest[i].message;
}
void CallNextCommand()
{
if(npcType == NpcType.ShopKeeper)
{
GUI_Menu.instance.CallShop(this.gameObject);
}else if(npcType == NpcType.QuestNpc)
{
}else if(npcType == NpcType.SaveNpc)
{
GUI_Menu.instance.CallSaveWindow(this.gameObject);
}
}
public void SetupDialogQuest(int id)
{
for(int i=0;i<questData.questSetting.Count;i++)
{
if(questID == questData.questSetting[i].questID)
{
if(questData.questSetting[i].questState == 1)
CheckConditionQuest(id);
if(questData.questSetting[i].questState == 0)
{
questNpcState = QuestNpcState.StartQuest;
if(indexDialog == dialogQuest.Count-1)
{
QuestWindow.enableWindow = true;
QuestWindow.enableButtonAccept = true;
disableNext = true;
questWindow.SetupQuestWindow(questID);
}
}else if(questData.questSetting[i].questState == 1)
{
questNpcState = QuestNpcState.InProgress;
questWindow.SetupQuestWindow(questID);
QuestWindow.enableWindow = true;
}else if(questData.questSetting[i].questState == 2)
{
questNpcState = QuestNpcState.Complete;
}else if(questData.questSetting[i].questState == 3)
{
questNpcState = QuestNpcState.AfterComplete;
}
indexCurrentQuest = i;
break;
}
}
}
void CheckConditionQuest(int id)
{
for(int i=0;i<questData.questSetting.Count;i++)
{
if(questID == questData.questSetting[i].questID)
{
if(questData.questSetting[i].questCondition == Quest_Data.QuestCondition.Hunting)
{
if(questData.questSetting[i].killCount >= (int)questData.questSetting[i].idCondition.y)
{
questData.questSetting[i].killCount = 0;
questData.questSetting[i].questState = 2;
}
}else
{
if(CheckConditionItem((int)questData.questSetting[i].idCondition.x,(int)questData.questSetting[i].idCondition.y))
{
questData.questSetting[i].killCount = 0;
questData.questSetting[i].questState = 2;
}
}
break;
}
}
}
bool CheckConditionItem(int id,int amout)
{
for(int i =0;i<inventory.bag_item.Count;i++)
{
if(inventory.bag_item[i].item_id == id && inventory.bag_item[i].item_amount >= amout)
{
inventory.bag_item[i].item_amount -= amout;
if(inventory.bag_item[i].item_amount <= 0)
{
inventory.bag_item[i].item_id = 0;
}
return true;
}
}
return false;
}
void GiveReward(int id)
{
for(int i=0;i<questData.questSetting.Count;i++)
{
if(questID == questData.questSetting[i].questID)
{
inventory.GiveItem((int)questData.questSetting[i].itemIDReward.x,(int)questData.questSetting[i].itemIDReward.y);
Debug.Log("Give");
break;
}
}
}
}
可以拿來直接使用,主要代碼都是用來控制頭像更換和簡單的劇情任務對話,
我們可以根據自己遊戲的需要自己來修改,如左邊,這個本來就不是重點,重點我們看看交易過程是什麼樣的
/// <summary>
/// Npc shop.
/// This script use to create a shop to sell item
/// </summary>
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ShopItemlist : MonoBehaviour {
public List<int> itemID = new List<int>();
void Start()
{
if(this.gameObject.tag == "Untagged")
this.gameObject.tag = "Npc_Shop";
}
}
shopitemlist腳本我們一看,只有短短的這麼幾行,那我們的交易是如何實現的呢?
我們看這個shopItemList的引用
在gui_menu.cs中
//Check on NPC Shop
if(hit.collider.tag == "Npc_Shop" && !CheckHoverInventory() && !CheckHoverEquipment() && !CheckHoverSkillWindow() && CheckHoverSlotHotkey() == -1 && itemShop[0].openItemShop == false){
OpenItemShopWindow();
itemShop[0].itemID = hit.collider.GetComponent<ShopItemlist>().itemID;
}
我們看,hit.collider.tag這個我們前面鼠標點選的時候就說明了,這個攝像機發射一條射線,與物體相交的檢測,我們利用這個方法還做了‘水果忍者’遊戲,其實這一句就是把我們在李鐵匠身上定義的物品列表賦值給我們真正的shop代碼的itemshop數組,那商店怎麼畫出來的呢,我們看代碼
private void ShowItemShopWindow(){
if(itemShop[0].openItemShop){
if(useStyle){
GUI.SetNextControlName("ItemShopWindow");
itemShop[0].windowItemShopRect = GUI.Window(itemShop[0].windowItemShopID, itemShop[0].windowItemShopRect, FunctionItemShopWindow, itemShop[0].nameItemShopWindow, guiStyleGroup[0].windowItemShopStyle);
}else{
GUI.SetNextControlName("ItemShopWindow");
itemShop[0].windowItemShopRect = GUI.Window(itemShop[0].windowItemShopID, itemShop[0].windowItemShopRect, FunctionItemShopWindow, itemShop[0].nameItemShopWindow);
}
if(Input.GetKeyDown(KeyCode.Escape)){
itemShop[0].openItemShop = false;
}
}
}
就是這麼畫出來的,上面就是一個u3d提供的話窗體的方法,我們的商店就是這麼畫出來的,當然畫出來,裏面要有賣東西的列表,這個列表在哪呢?看FunctionItemShopWindow方法
[HideInInspector]
public Vector2 scrollPositionShop;
/**
* Shopdraw
*/
private void FunctionItemShopWindow(int windowID){
GUI.DragWindow(new Rect(0,0,250,60));
itemShop[0].scrollViewRect.height = (((itemShop[0].itemID.Count-1) * itemShop[0].slotItemRect.height) * itemShop[0].space) + itemShop[0].offset.y + (itemShop[0].slotItemRect.height/2 + itemShop[0].offset.y/2);
scrollPositionShop = GUI.BeginScrollView(itemShop[0].scrollViewPosition , scrollPositionShop, itemShop[0].scrollViewRect);
for(int i = 0; i < itemShop[0].itemID.Count; i++){
if(useStyle){
GUI.Box(new Rect(itemShop[0].slotItemRect.x + itemShop[0].offset.x, itemShop[0].slotItemRect.y + ((i * itemShop[0].slotItemRect.height) * itemShop[0].space) + itemShop[0].offset.y, itemShop[0].slotItemRect.width, itemShop[0].slotItemRect.height), new GUIContent("","itemShopWindow"+(i).ToString()), guiStyleGroup[0].itemShopSlotStyle);
GUI.Box(new Rect(itemShop[0].descriptionRect.x + itemShop[0].offset.x, itemShop[0].descriptionRect.y + ((i * itemShop[0].descriptionRect.height) * itemShop[0].space) + itemShop[0].offset.y, itemShop[0].descriptionRect.width, itemShop[0].descriptionRect.height), new GUIContent("","itemShopWindow"+(i).ToString()), guiStyleGroup[0].descriptionItemShopStyle);
GUI.Box(new Rect(itemShop[0].itemNameRect.x + itemShop[0].offset.x, itemShop[0].itemNameRect.y + ((i * itemShop[0].itemNameRect.height) * itemShop[0].spaceItemName) + itemShop[0].offset.y, itemShop[0].itemNameRect.width, itemShop[0].itemNameRect.height), new GUIContent(Item_Data.instance.Get_Item(itemShop[0].itemID[i]).item_Name ,"itemShopWindow"+(i).ToString()), guiStyleGroup[0].itemShopItemNameStyle);
GUI.Box(new Rect(itemShop[0].itemTypeRect.x + itemShop[0].offset.x, itemShop[0].itemTypeRect.y + ((i * itemShop[0].itemTypeRect.height) * itemShop[0].spaceItemType) + itemShop[0].offset.y, itemShop[0].itemTypeRect.width, itemShop[0].itemTypeRect.height), new GUIContent(Item_Data.instance.Get_Item(itemShop[0].itemID[i]).item_Type ,"itemShopWindow"+(i).ToString()), guiStyleGroup[0].itemShopItemTypeStyle);
GUI.Box(new Rect(itemShop[0].itemDescriptionRect.x + itemShop[0].offset.x, itemShop[0].itemDescriptionRect.y + ((i * itemShop[0].itemDescriptionRect.height) * itemShop[0].spaceItemDescription) + itemShop[0].offset.y, itemShop[0].itemDescriptionRect.width, itemShop[0].itemDescriptionRect.height), new GUIContent(Item_Data.instance.Get_Item(itemShop[0].itemID[i]).description ,"itemShopWindow"+(i).ToString()), guiStyleGroup[0].itemShopDescriptionStyle);
GUI.Box(new Rect(itemShop[0].priceRect.x + itemShop[0].offset.x, itemShop[0].priceRect.y + ((i * itemShop[0].priceRect.height) * itemShop[0].spacePrice) + itemShop[0].offset.y, itemShop[0].priceRect.width, itemShop[0].priceRect.height), new GUIContent(Item_Data.instance.Get_Item(itemShop[0].itemID[i]).price.ToString() ,"itemShopWindow"+(i).ToString()), guiStyleGroup[0].itemShopPriceStyle);
if(GUI.Button(new Rect(itemShop[0].buttonBuyRect.x + itemShop[0].offset.x, itemShop[0].buttonBuyRect.y + ((i * itemShop[0].buttonBuyRect.height) * itemShop[0].spaceButtonBuy) + itemShop[0].offset.y, itemShop[0].buttonBuyRect.width, itemShop[0].buttonBuyRect.height), new GUIContent("BUY","itemShopWindow"+(i).ToString()), guiStyleGroup[0].itemShopButtonBuyStyle)){
if(itemShop[0].itemID[i] != null && showDropAmount == false){
item_pickup = new Bag();
item_pickup.item_id = itemShop[0].itemID[i];
item_pickup.isItem = true;
item_pickup.equipmentType = Item_Data.instance.Get_Item(itemShop[0].itemID[i]).equipment_Type;
isBuyItem = true;
//amountItemDrop = "1";
showDropAmount = true;
}
}
}else{
GUI.Box(new Rect(itemShop[0].slotItemRect.x + itemShop[0].offset.x, itemShop[0].slotItemRect.y + ((i * itemShop[0].slotItemRect.height) * itemShop[0].space) + itemShop[0].offset.y, itemShop[0].slotItemRect.width, itemShop[0].slotItemRect.height), new GUIContent("","itemShopWindow"+(i).ToString()));
GUI.Box(new Rect(itemShop[0].descriptionRect.x + itemShop[0].offset.x, itemShop[0].descriptionRect.y + ((i * itemShop[0].descriptionRect.height) * itemShop[0].space) + itemShop[0].offset.y, itemShop[0].descriptionRect.width, itemShop[0].descriptionRect.height), new GUIContent("" ,"itemShopWindow"+(i).ToString()));
GUI.Box(new Rect(itemShop[0].itemNameRect.x + itemShop[0].offset.x, itemShop[0].itemNameRect.y + ((i * itemShop[0].itemNameRect.height) * itemShop[0].spaceItemName) + itemShop[0].offset.y, itemShop[0].itemNameRect.width, itemShop[0].itemNameRect.height), new GUIContent(Item_Data.instance.Get_Item(itemShop[0].itemID[i]).item_Name ,"itemShopWindow"+(i).ToString()));
GUI.Box(new Rect(itemShop[0].itemTypeRect.x + itemShop[0].offset.x, itemShop[0].itemTypeRect.y + ((i * itemShop[0].itemTypeRect.height) * itemShop[0].spaceItemType) + itemShop[0].offset.y, itemShop[0].itemTypeRect.width, itemShop[0].itemTypeRect.height), new GUIContent(Item_Data.instance.Get_Item(itemShop[0].itemID[i]).item_Type ,"itemShopWindow"+(i).ToString()));
GUI.Box(new Rect(itemShop[0].itemDescriptionRect.x + itemShop[0].offset.x, itemShop[0].itemDescriptionRect.y + ((i * itemShop[0].itemDescriptionRect.height) * itemShop[0].spaceItemDescription) + itemShop[0].offset.y, itemShop[0].itemDescriptionRect.width, itemShop[0].itemDescriptionRect.height), new GUIContent(Item_Data.instance.Get_Item(itemShop[0].itemID[i]).description ,"itemShopWindow"+(i).ToString()));
GUI.Box(new Rect(itemShop[0].priceRect.x + itemShop[0].offset.x, itemShop[0].priceRect.y + ((i * itemShop[0].priceRect.height) * itemShop[0].spacePrice) + itemShop[0].offset.y, itemShop[0].priceRect.width, itemShop[0].priceRect.height), new GUIContent(Item_Data.instance.Get_Item(itemShop[0].itemID[i]).price.ToString() ,"itemShopWindow"+(i).ToString()));
if(GUI.Button(new Rect(itemShop[0].buttonBuyRect.x + itemShop[0].offset.x, itemShop[0].buttonBuyRect.y + ((i * itemShop[0].buttonBuyRect.height) * itemShop[0].spaceButtonBuy) + itemShop[0].offset.y, itemShop[0].buttonBuyRect.width, itemShop[0].buttonBuyRect.height), new GUIContent("BUY","itemShopWindow"+(i).ToString()))){
if(itemShop[0].itemID[i] != null && showDropAmount == false){
item_pickup = new Bag();
item_pickup.item_id = itemShop[0].itemID[i];
item_pickup.isItem = true;
item_pickup.equipmentType = Item_Data.instance.Get_Item(itemShop[0].itemID[i]).equipment_Type;
isBuyItem = true;
//amountItemDrop = "1";
showDropAmount = true;
}
}
}
if(Item_Data.instance.Get_Item(itemShop[0].itemID[i]) != null){
GUI.DrawTexture(new Rect(itemShop[0].slotItemRect.x + itemShop[0].offset.x, itemShop[0].slotItemRect.y + ((i * itemShop[0].slotItemRect.height) * itemShop[0].space) + itemShop[0].offset.y, itemShop[0].slotItemRect.width, itemShop[0].slotItemRect.height), Item_Data.instance.Get_Item(itemShop[0].itemID[i]).item_Img);
}
}
if(pickupStay){
float maxSpacePos = (itemShop[0].scrollViewRect.height - scrollPositionShop.y) / (itemShop[0].slotItemRect.height * itemShop[0].space);
Vector3 mousePos = Input.mousePosition;
if(itemImg_Pickup != null){
GUI.DrawTexture(new Rect((mousePos.x - itemShop[0].scrollViewPosition.x)-20-itemShop[0].windowItemShopRect.x, (Screen.height - (mousePos.y + itemShop[0].scrollViewPosition.y - itemShop[0].valueTuningIconFollowMouse))-20-itemShop[0].windowItemShopRect.y + (scrollPositionShop.y - maxSpacePos* (-1*(itemShop[0].slotItemRect.height * itemShop[0].space/100))), 40,40),itemImg_Pickup);
}
}
GUI.EndScrollView();
ShowDescriptionBag(itemShop[0].windowItemShopRect , id_Hover);
ShowDescriptionBag(itemShop[0].windowItemShopRect , id_Hover);
if(useStyle){
GUI.Box(itemShop[0].moneyRect,money.ToString(),guiStyleGroup[0].goldStyle);
if(GUI.Button(itemShop[0].buttonCancelRect,"",guiStyleGroup[0].itemShopCancel)){
itemShop[0].openItemShop = false;
}
}else{
GUI.Box(itemShop[0].moneyRect,money.ToString());
if(GUI.Button(itemShop[0].buttonCancelRect,"X")){
itemShop[0].openItemShop = false;
}
}
GUI.DrawTexture(itemShop[0].coinIconRect, coinIcon);
}
哇,好多,代碼雖然多,其實不難理解,用u3d自帶api也好,用ngui編輯界面再畫也罷,實現效果其實都差不多, itemShop這個是我們在李鐵匠身上設定的shop列表,根據這個列表,我們用for循環畫出賣的所有item,當買這個button確定之後,方法中觸發這樣一個方法
if(itemShop[0].itemID[i] != null && showDropAmount == false){
item_pickup = new Bag();
item_pickup.item_id = itemShop[0].itemID[i];
item_pickup.isItem = true;
item_pickup.equipmentType = Item_Data.instance.Get_Item(itemShop[0].itemID[i]).equipment_Type;
isBuyItem = true;
//amountItemDrop = "1";
showDropAmount = true;
}
就是我們的拾取物品代碼,我們可以直接以上面這幾行爲例,綁在一個物品上,做場景中的礦石或草藥(我們在demo已經加了這方面野外物品拾取,不過做的方式類似於寶箱)獲取。就這樣,我們的shop就畫出來了。