Photon Unity Networking基礎(chǔ)教程 2 Lobby UI

Photon Unity Networking基礎(chǔ)教程 2 Lobby UI

這部分將會集中于給Lobby創(chuàng)建用戶界面。這部分的內(nèi)容非常基礎(chǔ),并且跟網(wǎng)絡(luò)關(guān)系不大。

主要內(nèi)容

  • Play按鈕
  • 玩家名字
  • 連接進(jìn)度

Play按鈕

目前我們的Lobby是把我們自動連接到一個房間,這是有益于早期測試,但我們想讓用戶選擇是否以及何時開始游戲。 因此,我們將簡單地提供一個Button。

  1. 打開場景Launcher
  2. 使用Unity菜單'GameObject/UI/Button'創(chuàng)建一個按鈕,命名為Play Button,注意他在場景中創(chuàng)建了Canvas和EventSystem游戲?qū)ο螅×宋覀兊氖铝耍琻ice:)
  3. 編輯Play Button的Text值為“Play”
  4. 選擇Play Button然后定位到按鈕組建的On Click()部分
  5. 點(diǎn)擊小的加號“+”添加一條
  6. 把Launcher對象從Hierachy面板中拖過來
  7. 在下拉列表中選擇Launcher.connect()函數(shù)。我們現(xiàn)在把按鈕和Launcher腳本連起來了,當(dāng)用戶點(diǎn)擊按鈕的時候,將會調(diào)用Launcher腳本的connect()函數(shù)
  8. 打開Launcher腳本
  9. 把Start()函數(shù)中的connect()函數(shù)刪除
  10. 保存Launcher腳本和場景

現(xiàn)在點(diǎn)擊Play,你會發(fā)現(xiàn)需要點(diǎn)擊按鈕才能進(jìn)行連接了。

玩家名字

典型游戲的另一個重要的最低要求是讓用戶輸入他們的名字,以便其他玩家知道他們正在和誰玩。 我們將實(shí)現(xiàn)這個簡單的任務(wù),通過使用PlayerPrefs記住名字,以便當(dāng)用戶打開游戲,我們可以知道名字是什么。 要為您的游戲創(chuàng)建一個偉大的用戶體驗(yàn)的話,這是一個非常方便和相當(dāng)重要的功能。

讓我們先創(chuàng)建一個腳本來管理和記住玩家的名字,然后創(chuàng)建相關(guān)的UI。

創(chuàng)建玩家名字輸入框

  1. 創(chuàng)建一個新的C#腳本,命名為PlayerNameInputField

  2. 下面是腳本的全部內(nèi)容,編輯然后保存

     using UnityEngine;
     using UnityEngine.UI;
     using System.Collections;
      
     namespace Com.MyCompany.MyGame
     {
         /// <summary>
         /// Player name input field. Let the user input his name, will appear above the player in the game.
         /// </summary>
         [RequireComponent(typeof(InputField))]
         public class PlayerNameInputField : MonoBehaviour
         {
             #region Private Variables        
      
             // Store the PlayerPref Key to avoid typos
             static string playerNamePrefKey = "PlayerName";         
      
             #endregion         
      
             #region MonoBehaviour CallBacks         
      
             /// <summary>
             /// MonoBehaviour method called on GameObject by Unity during initialization phase.
             /// </summary>
             void Start () {         
      
                 string defaultName = "";
                 InputField _inputField = this.GetComponent<InputField>();
                 if (_inputField!=null)
                 {
                     if (PlayerPrefs.HasKey(playerNamePrefKey))
                     {
                         defaultName = PlayerPrefs.GetString(playerNamePrefKey);
                         _inputField.text = defaultName;
                     }
                 }         
      
                 PhotonNetwork.playerName =  defaultName;
             }         
      
             #endregion         
      
             #region Public Methods                 
             /// <summary>
             /// Sets the name of the player, and save it in the PlayerPrefs for future sessions.
             /// </summary>
             /// <param name="value">The name of the Player</param>
             public void SetPlayerName(string value)
             {
                 // #Important
                 PhotonNetwork.playerName = value + " "; // force a trailing space string in case value is an empty string, else playerName would not be updated.         
      
                 PlayerPrefs.SetString(playerNamePrefKey,value);
             }         
      
             #endregion
         }
     }
    

下面分析一下這個腳本:

  • RequireComponent(typeof(InputField))

我們首先保證這個腳本必須InputField組件,這可以方便快捷的保證使用該腳本沒有錯誤。

  • PlayerPrefs.HasKey(), PlayerPrefs.GetString() and PlayerPrefs.SetString()

PlayerPrefs是一個簡單的配對條目的查找列表(像一個excel表有兩列),一個是鍵,一個是值。 Key是一個字符串,是完全任意的,你決定如何命名,你需要在整個開發(fā)過程中記住它。因此,有必要總是將PlayerPrefs鍵存儲在一個地方,一個方便的方法是使用Static變量聲明,因?yàn)樗粫S著時間的推移在游戲過程中改變,并且每次都是相同的。

所以,邏輯非常簡單。如果PlayerPrefs有一個給定的鍵,我們可以得到它,并當(dāng)我們要用的時候直接賦值。在我們的案例中,我們在啟動時填充InputField時,在編輯過程中,我們把當(dāng)前InputField的值設(shè)置給PlayerPref鍵,然后我們確定它被存儲在用戶設(shè)備上以供稍后檢索(下一次用戶打開此游戲)。

  • PhotonNetwork.playerName

這是此腳本的要點(diǎn),設(shè)置玩家在網(wǎng)絡(luò)上的名稱。 腳本在兩個地方使用它,一次是在Start()中檢查名稱是否存儲在PlayerPrefs中,一次是在公共方法SetPlayerName()中。 現(xiàn)在,沒地方調(diào)用這個方法,我們需要綁定InputField OnValueChange()來調(diào)用SetPlayerName(),這樣每次用戶編輯InputField時,我們都會記錄它。 我們可以做到這一點(diǎn),只有當(dāng)用戶按下播放,這取決于你,但是這需要些更多的腳本,所以為了清楚起見讓我們保持簡單。 這也意味著,不管用戶將做什么,輸入將被記住,這通常是期望的行為。

為玩家的名字創(chuàng)建UI

  1. 確保你打開的是Launcher場景
  2. 使用Unity菜單'GameObject/UI/InputField'創(chuàng)建InputField,命名為Name InputField
  3. 把RectTransform中的PosY值設(shè)置為35,它會在PlayButton的上面
  4. 定位到Name InputField的子對象PlaceHolder,設(shè)置它的文本值為"Enter your Name..."
  5. 選擇Name InputField對象
  6. 把我們剛才創(chuàng)建的PlayerNamerInputField腳本給它加上
  7. 定位到InputField組件的On Value Change (String)部分
  8. 點(diǎn)擊加號“+”添加一條
  9. 把PlayerNamerInputField拖拽到框里
  10. 下拉列表中選擇PlayerNameInputField.SetPlayerName()
  11. 保存場景

好了,你可以點(diǎn)擊play運(yùn)行,輸入你的名字,然后停止play,再次點(diǎn)擊play啟動,你剛才輸入的名字就會有了。

我們實(shí)現(xiàn)了功能,然而在用戶體驗(yàn)方面,我們?nèi)鄙龠B接進(jìn)度的反饋,還缺少當(dāng)連接期間和加入房間時出現(xiàn)問題時的反饋。

連接進(jìn)度

我們在這里盡量保持簡單,隱藏名稱字段和play按鈕,并在連接期間將其替換為簡單的文本“正在連接...”,并在需要時將其切換回來。

為此,我們把播放按鈕和名稱字段做成一個組,以便我們只需要激活和停用該組。 后來更多的功能可以添加到組,它不會影響我們的邏輯。

  1. 確保你打開的是Launcher場景
  2. 使用unity菜單'GameObject/UI/Panel'創(chuàng)建UI面板,命名為Control Panel
  3. 刪除Control Panel的Image和Canvas Renderer組件,我們不需要任何可視元素,我們只關(guān)心它的內(nèi)容
  4. 把Play Button 和 Name InputField拖拽到Control Panel對象上去
  5. 使用unity菜單'GameObject/UI/Text'創(chuàng)建UI文字,命名為Progress Label,不用關(guān)心它影響了顯示,我們將在運(yùn)行時激活和停用它們
  6. 選擇Progress Label的Text組件
  7. 設(shè)置對齊方式為center align和middle align
  8. 設(shè)置文字為“Connecting...”
  9. 設(shè)置顏色為白色或者其他和背景有區(qū)別
  10. 保存場景

此時,為了測試,您可以簡單地啟用/禁用Control Panel和Progress Label,以查看各種連接階段的情況。 現(xiàn)在讓我們編輯腳本以控制這兩個GameObjects激活。

  1. 編輯腳本Launcher

  2. 在Public Properties區(qū)塊添加下面兩個屬性

     [Tooltip("The Ui Panel to let the user enter name, connect and play")]
     public GameObject controlPanel;
     [Tooltip("The UI Label to inform the user that the connection is in progress")]
     public GameObject progressLabel;
    
  3. 把下面的代碼加入Start函數(shù)

     progressLabel.SetActive(false);
     controlPanel.SetActive(true);
    
  4. 在Connect方法開頭添加下面的代碼

     progressLabel.SetActive(true);
     controlPanel.SetActive(false);
    
  5. 在OnDisconnectedFromPhoton方法開頭添加下面的代碼

     progressLabel.SetActive(false);
     controlPanel.SetActive(true);
    
  6. 保存Launcher腳本,等待unity編譯結(jié)束

  7. 確保打開場景Launcher

  8. 在Hierarchy中選中Launcher對象

  9. 把Hierarchy中的Control Panel和Progress Label拖拽到對應(yīng)的Launcher中的組件

  10. 保存場景

現(xiàn)在,如果你運(yùn)行場景。 您將看到只有控制面板,可見,一旦您單擊播放,將顯示Progres標(biāo)簽。

到此為止,我們做好了Lobby部分。 為了進(jìn)一步增加Lobby的功能,我們需要切換到游戲本身,并創(chuàng)建各種場景,以便我們可以在加入房間時最終加載正確的級別。 我們將在接下來的部分完成,然后,我們將完成Lobby系統(tǒng)。

原文

http://doc.photonengine.com/en-US/pun/current/tutorials/pun-basics-tutorial/lobby-ui

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容