Tweety-666
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Socket多人連線、接資料,與遊戲設計 - 房間列表 處理好每個房間的類後,這些房間會排列在RoomListPanel。所以,接下來將處理RoomListPanel。針對房間列表,除了有**創建功能**,還有**刷新**,查看是否有新的房間增加。**進入**到房間中,也可以選擇**離開**。 文章中的程式碼,會看到很多函式,這些函式得先規範好怎麼命名。 * Click結尾是按下按鈕後,要執行的函式。 * Response是前端收到後端資訊後,要做出回應的函式。 ```C# using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; using UnityEngine.UI; using Common; public class RoomListPanel : BasePanel { //先聲明遊戲物件 private RectTransform battleRes;//對戰結果 private RectTransform roomList;//房間清單 private VerticalLayoutGroup roomLayout;//房間清單的排版 private GameObject roomItemPrefab; //RoomItem的類,做成預製資源 private ListRoomRequest listRoomRequest; //這UI介面上有哪些Request //列出房間Request private CreateRoomRequest createRoomRequest; //創建房間Request private JoinRoomRequest joinRoomRequest;//加入房間Request private List<UserData> udList = null; //玩家清單 //需要先建立好UserData的類 private UserData ud1 = null; //玩家1 //這遊戲是一打一 private UserData ud2 = null; //玩家2 //聲明好遊戲物件後,要定義 private void Start() { battleRes = transform.Find("BattleRes").GetComponent<RectTransform>(); roomList = transform.Find("RoomList").GetComponent<RectTransform>(); roomLayout = transform.Find("RoomList/ScrollRect/Layout").GetComponent<VerticalLayoutGroup>();//房間列表是垂直排列的列表 roomItemPrefab = Resources.Load("UIPanel/RoomItem") as GameObject;//RoomItem的類,是prefab //監聽按鈕,點擊觸發函式 transform.Find("RoomList/CloseButton").GetComponent<Button>().onClick.AddListener(OnCloseClick); transform.Find("RoomList/CreateRoomButton").GetComponent<Button>().onClick.AddListener(OnCreateRoomClick); transform.Find("RoomList/RefreshButton").GetComponent<Button>().onClick.AddListener(OnRefreshClick); listRoomRequest = GetComponent<ListRoomRequest>(); createRoomRequest = GetComponent<CreateRoomRequest>(); joinRoomRequest = GetComponent<JoinRoomRequest>(); //進場動畫 EnterAnim(); } public override void OnEnter() { SetBattleRes(); //進入面板就先呈現個人戰積 if (battleRes != null) EnterAnim(); if(listRoomRequest==null)//進入房間面板就列出房間 listRoomRequest = GetComponent<ListRoomRequest>(); listRoomRequest.SendRequest(); //面板叫Request腳本發出要列出房間清單的Request } public override void OnExit() { HideAnim(); } public override void OnPause() { HideAnim(); } public override void OnResume() { EnterAnim(); listRoomRequest.SendRequest(); //刷新,再列一次房間清單 //面板叫Request腳本發出要列出房間清單的Request } private void Update() //時時更新玩家資訊 { if (udList != null) { LoadRoomItem(udList);//載入玩家清單 udList = null; } if (ud1 != null && ud2 != null) { BasePanel panel = uiMng.PushPanel(UIPanelType.Room); (panel as RoomPanel).SetAllPlayerResSync(ud1, ud2);//設置兩位玩家 ud1 = null;ud2 = null; } } private void OnCloseClick() { PlayClickSound();//父類的函式,facade會呼叫AudioMng播放音效 uiMng.PopPanel(); } private void OnCreateRoomClick() //按下創建房間的按鈕,就觸發此函式 { BasePanel panel= uiMng.PushPanel(UIPanelType.Room); createRoomRequest.SetPanel(panel); createRoomRequest.SendRequest();//createRoomRequest會發出要求 } private void OnRefreshClick() //按下刷新按鈕,觸發此函式,listRoomRequest會發出要求 { listRoomRequest.SendRequest(); } private void EnterAnim() { gameObject.SetActive(true); battleRes.localPosition = new Vector3(-1000, 0); battleRes.DOLocalMoveX(-290, 0.5f); roomList.localPosition = new Vector3(1000, 0); roomList.DOLocalMoveX(171, 0.5f); } private void HideAnim() { battleRes.DOLocalMoveX(-1000, 0.5f); roomList.DOLocalMoveX(1000, 0.5f).OnComplete(() => gameObject.SetActive(false)); } private void SetBattleRes() //設置戰績,一進入此面板就會執行 { UserData ud = facade.GetUserData();//設置個人戰績前,得先抓到UserData //facade單例負責去抓 transform.Find("BattleRes/Username").GetComponent<Text>().text = ud.Username; transform.Find("BattleRes/TotalCount").GetComponent<Text>().text = "总场数:"+ud.TotalCount.ToString(); transform.Find("BattleRes/WinCount").GetComponent<Text>().text = "胜利:"+ud.WinCount.ToString(); } public void OnUpdateResultResponse(int totalCount,int winCount)//Response結尾都是後端發回來後,前端的回應函式 { facade.UpdateResult(totalCount, winCount);//叫此面板更新戰績,函式會叫facade單例更新戰績,再執行設置戰績的函式 SetBattleRes(); } public void LoadRoomItemSync(List<UserData> udList) { this.udList = udList; } private void LoadRoomItem( List<UserData> udList )//列每個房間RoomItem到列表上,同時載入UserData { RoomItem[] riArray= roomLayout.GetComponentsInChildren<RoomItem>();//抓到垂直的Layout下的子層(RoomItem) foreach(RoomItem ri in riArray) { ri.DestroySelf();//陣列內每個項目都摧毀 } int count = udList.Count;//依據udList.Count數量,重新載入 for (int i = 0; i < count; i++) { GameObject roomItem = GameObject.Instantiate(roomItemPrefab);//載入房間的Prefab roomItem.transform.SetParent(roomLayout.transform); //設置房間的父層,父層是垂直的Layout UserData ud = udList[i]; roomItem.GetComponent<RoomItem>().SetRoomInfo(ud.Id, ud.Username, ud.TotalCount, ud.WinCount,this);//設置房間資訊 } int roomCount = GetComponentsInChildren<RoomItem>().Length;//抓到RoomItem數目 Vector2 size = roomLayout.GetComponent<RectTransform>().sizeDelta; //抓取垂直Layout的長度 roomLayout.GetComponent<RectTransform>().sizeDelta = new Vector2(size.x, //重新設置roomLayout長度,因為多增加房間項目,Layout也得變長 roomCount * (roomItemPrefab.GetComponent<RectTransform>().sizeDelta.y + roomLayout.spacing));//RoomItem數目 * (一個房間在列表所需的長度+spacing) } public void OnJoinClick(int id) //Click結尾都是按扭按下去後,要執行的函式 { joinRoomRequest.SendRequest(id);//按下加入房間按鈕,此面板找joinRoomRequest去發送要求給後端 //參數是房間id } public void OnJoinResponse( ReturnCode returnCode,UserData ud1,UserData ud2) //前端收到後端資訊,做出回應 { switch (returnCode) { case ReturnCode.NotFound: uiMng.ShowMessageSync("房间被销毁无法加入"); break; case ReturnCode.Fail: uiMng.ShowMessageSync("房间已满,无法加入"); break; case ReturnCode.Success: //可以加入就設置雙方玩家的id this.ud1 = ud1; this.ud2 = ud2; break; } } //private void Update() //{ // if (Input.GetMouseButtonDown(0)) // { // LoadRoomItem(1); // } //} } ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully