added scoring system for the bow game
This commit is contained in:
@@ -242,7 +242,7 @@ namespace FishNet.Discovery
|
||||
//
|
||||
// return;
|
||||
// }
|
||||
// Debug.Log("NetworkDiscovery is searching on port " + port);
|
||||
Debug.Log("NetworkDiscovery is searching");
|
||||
|
||||
if (InstanceFinder.IsClient)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
using FishNet;
|
||||
using FishNet.Discovery;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class NetworkMenuUI : MonoBehaviour
|
||||
{
|
||||
[Header("UI References")]
|
||||
public Button startPlayingButton;
|
||||
public Button joinMultiplayerButton;
|
||||
public Transform serverListContainer;
|
||||
public GameObject serverListItemPrefab;
|
||||
public TMP_Text statusText;
|
||||
|
||||
[Header("Networking")]
|
||||
public NetworkDiscovery networkDiscovery;
|
||||
public Camera uiCamera; // Optional, disable if connecting
|
||||
|
||||
private readonly List<IPEndPoint> foundServers = new();
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (networkDiscovery == null)
|
||||
networkDiscovery = FindObjectOfType<NetworkDiscovery>();
|
||||
|
||||
startPlayingButton.onClick.AddListener(OnStartPlaying);
|
||||
joinMultiplayerButton.onClick.AddListener(OnJoinMultiplayer);
|
||||
|
||||
networkDiscovery.ServerFoundCallback += OnServerFound;
|
||||
}
|
||||
|
||||
private void OnStartPlaying()
|
||||
{
|
||||
statusText.text = "Starting host...";
|
||||
StartCoroutine(HostAndSearchRoutine());
|
||||
}
|
||||
|
||||
|
||||
private void OnJoinMultiplayer()
|
||||
{
|
||||
statusText.text = "Searching for servers...";
|
||||
ClearServerList();
|
||||
foundServers.Clear();
|
||||
networkDiscovery.StartSearchingForServers();
|
||||
}
|
||||
|
||||
private void OnServerFound(IPEndPoint endPoint)
|
||||
{
|
||||
if (foundServers.Contains(endPoint)) return;
|
||||
foundServers.Add(endPoint);
|
||||
|
||||
// Auto-join if started as host
|
||||
if (InstanceFinder.IsServer)
|
||||
{
|
||||
//networkDiscovery.StopAdvertisingServer();
|
||||
networkDiscovery.StopSearchingForServers();
|
||||
|
||||
uiCamera.enabled = false;
|
||||
|
||||
InstanceFinder.ClientManager.StartConnection("192.168.42.212");
|
||||
statusText.text = $"Joined server: {endPoint.Address}";
|
||||
return;
|
||||
}
|
||||
|
||||
// Display in UI for manual joining
|
||||
GameObject item = Instantiate(serverListItemPrefab, serverListContainer);
|
||||
TMP_Text label = item.GetComponentInChildren<TMP_Text>();
|
||||
label.text = endPoint.Address.ToString();
|
||||
|
||||
Button btn = item.GetComponent<Button>();
|
||||
btn.onClick.AddListener(() =>
|
||||
{
|
||||
networkDiscovery.StopSearchingForServers();
|
||||
if (uiCamera != null) uiCamera.enabled = false;
|
||||
InstanceFinder.ClientManager.StartConnection(endPoint.Address.ToString());
|
||||
statusText.text = $"Joined server: {endPoint.Address}";
|
||||
});
|
||||
}
|
||||
private Coroutine joinRoutine;
|
||||
|
||||
private IEnumerator HostAndSearchRoutine()
|
||||
{
|
||||
// Step 1: Start hosting
|
||||
InstanceFinder.ServerManager.StartConnection();
|
||||
|
||||
// Step 2: Start advertising
|
||||
yield return new WaitForSeconds(0.5f); // Let the server settle
|
||||
networkDiscovery.StartAdvertisingServer();
|
||||
|
||||
// Step 3: Begin searching
|
||||
yield return new WaitForSeconds(0.5f); // Let advertisement initialize
|
||||
networkDiscovery.StartSearchingForServers();
|
||||
|
||||
// Step 4: Try to find and join a better server
|
||||
yield return new WaitForSeconds(0.5f); // Give discovery a moment
|
||||
StartJoinRoutine();
|
||||
}
|
||||
|
||||
|
||||
public void StartJoinRoutine()
|
||||
{
|
||||
if (joinRoutine != null) StopCoroutine(joinRoutine);
|
||||
joinRoutine = StartCoroutine(TryJoinServerRepeatedly());
|
||||
}
|
||||
|
||||
private IEnumerator TryJoinServerRepeatedly()
|
||||
{
|
||||
float timeout = 10f;
|
||||
float timer = 0f;
|
||||
|
||||
while (timer < timeout)
|
||||
{
|
||||
if (foundServers.Count > 0)
|
||||
{
|
||||
var firstServer = foundServers[0];
|
||||
//networkDiscovery.StopAdvertisingServer();
|
||||
networkDiscovery.StopSearchingForServers();
|
||||
InstanceFinder.ClientManager.StartConnection(firstServer.Address.ToString());
|
||||
statusText.text = $"Joined server: {firstServer.Address}";
|
||||
yield break;
|
||||
}
|
||||
|
||||
statusText.text = $"Searching for servers... ({Mathf.FloorToInt(timer)}s)";
|
||||
yield return new WaitForSeconds(1f);
|
||||
timer += 1f;
|
||||
}
|
||||
|
||||
|
||||
statusText.text = "No servers found. Acting as host.";
|
||||
// You can optionally start your own server here
|
||||
}
|
||||
|
||||
private void ClearServerList()
|
||||
{
|
||||
foreach (Transform child in serverListContainer)
|
||||
{
|
||||
Destroy(child.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!1 &1243484539689895160
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 6102112898136418063}
|
||||
- component: {fileID: 4162166102933070300}
|
||||
- component: {fileID: 8982425673842065088}
|
||||
- component: {fileID: 6642303600386318624}
|
||||
m_Layer: 5
|
||||
m_Name: Server List item prefab
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!224 &6102112898136418063
|
||||
RectTransform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1243484539689895160}
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1.2, y: 1.2, z: 1.2}
|
||||
m_ConstrainProportionsScale: 1
|
||||
m_Children:
|
||||
- {fileID: 6068593683225063891}
|
||||
m_Father: {fileID: 0}
|
||||
m_RootOrder: -1
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
m_AnchorMin: {x: 0, y: 0}
|
||||
m_AnchorMax: {x: 0, y: 0}
|
||||
m_AnchoredPosition: {x: 0, y: 0}
|
||||
m_SizeDelta: {x: 160, y: 30}
|
||||
m_Pivot: {x: 0.5, y: 0.5}
|
||||
--- !u!222 &4162166102933070300
|
||||
CanvasRenderer:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1243484539689895160}
|
||||
m_CullTransparentMesh: 1
|
||||
--- !u!114 &8982425673842065088
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1243484539689895160}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_Material: {fileID: 0}
|
||||
m_Color: {r: 1, g: 1, b: 1, a: 1}
|
||||
m_RaycastTarget: 1
|
||||
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
|
||||
m_Maskable: 1
|
||||
m_OnCullStateChanged:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0}
|
||||
m_Type: 1
|
||||
m_PreserveAspect: 0
|
||||
m_FillCenter: 1
|
||||
m_FillMethod: 4
|
||||
m_FillAmount: 1
|
||||
m_FillClockwise: 1
|
||||
m_FillOrigin: 0
|
||||
m_UseSpriteMesh: 0
|
||||
m_PixelsPerUnitMultiplier: 1
|
||||
--- !u!114 &6642303600386318624
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1243484539689895160}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_Navigation:
|
||||
m_Mode: 3
|
||||
m_WrapAround: 0
|
||||
m_SelectOnUp: {fileID: 0}
|
||||
m_SelectOnDown: {fileID: 0}
|
||||
m_SelectOnLeft: {fileID: 0}
|
||||
m_SelectOnRight: {fileID: 0}
|
||||
m_Transition: 1
|
||||
m_Colors:
|
||||
m_NormalColor: {r: 0.21698111, g: 0.21698111, b: 0.21698111, a: 1}
|
||||
m_HighlightedColor: {r: 0.21960784, g: 0.21960784, b: 0.21960784, a: 1}
|
||||
m_PressedColor: {r: 0.21960784, g: 0.21960784, b: 0.21960784, a: 1}
|
||||
m_SelectedColor: {r: 0.21960784, g: 0.21960784, b: 0.21960784, a: 1}
|
||||
m_DisabledColor: {r: 0.21960784, g: 0.21960784, b: 0.21960784, a: 0.5019608}
|
||||
m_ColorMultiplier: 1
|
||||
m_FadeDuration: 0.1
|
||||
m_SpriteState:
|
||||
m_HighlightedSprite: {fileID: 0}
|
||||
m_PressedSprite: {fileID: 0}
|
||||
m_SelectedSprite: {fileID: 0}
|
||||
m_DisabledSprite: {fileID: 0}
|
||||
m_AnimationTriggers:
|
||||
m_NormalTrigger: Normal
|
||||
m_HighlightedTrigger: Highlighted
|
||||
m_PressedTrigger: Pressed
|
||||
m_SelectedTrigger: Selected
|
||||
m_DisabledTrigger: Disabled
|
||||
m_Interactable: 1
|
||||
m_TargetGraphic: {fileID: 8982425673842065088}
|
||||
m_OnClick:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
--- !u!1 &8712802155540729905
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 6068593683225063891}
|
||||
- component: {fileID: 767001398382591515}
|
||||
- component: {fileID: 730381457657509126}
|
||||
m_Layer: 5
|
||||
m_Name: Text (TMP)
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!224 &6068593683225063891
|
||||
RectTransform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 8712802155540729905}
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children: []
|
||||
m_Father: {fileID: 6102112898136418063}
|
||||
m_RootOrder: -1
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
m_AnchorMin: {x: 0, y: 0}
|
||||
m_AnchorMax: {x: 1, y: 1}
|
||||
m_AnchoredPosition: {x: 0, y: 0}
|
||||
m_SizeDelta: {x: 0, y: 0}
|
||||
m_Pivot: {x: 0.5, y: 0.5}
|
||||
--- !u!222 &767001398382591515
|
||||
CanvasRenderer:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 8712802155540729905}
|
||||
m_CullTransparentMesh: 1
|
||||
--- !u!114 &730381457657509126
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 8712802155540729905}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_Material: {fileID: 0}
|
||||
m_Color: {r: 1, g: 1, b: 1, a: 1}
|
||||
m_RaycastTarget: 1
|
||||
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
|
||||
m_Maskable: 1
|
||||
m_OnCullStateChanged:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_text: ...
|
||||
m_isRightToLeft: 0
|
||||
m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
|
||||
m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
|
||||
m_fontSharedMaterials: []
|
||||
m_fontMaterial: {fileID: 0}
|
||||
m_fontMaterials: []
|
||||
m_fontColor32:
|
||||
serializedVersion: 2
|
||||
rgba: 4294967295
|
||||
m_fontColor: {r: 1, g: 1, b: 1, a: 1}
|
||||
m_enableVertexGradient: 0
|
||||
m_colorMode: 3
|
||||
m_fontColorGradient:
|
||||
topLeft: {r: 1, g: 1, b: 1, a: 1}
|
||||
topRight: {r: 1, g: 1, b: 1, a: 1}
|
||||
bottomLeft: {r: 1, g: 1, b: 1, a: 1}
|
||||
bottomRight: {r: 1, g: 1, b: 1, a: 1}
|
||||
m_fontColorGradientPreset: {fileID: 0}
|
||||
m_spriteAsset: {fileID: 0}
|
||||
m_tintAllSprites: 0
|
||||
m_StyleSheet: {fileID: 0}
|
||||
m_TextStyleHashCode: -1183493901
|
||||
m_overrideHtmlColors: 0
|
||||
m_faceColor:
|
||||
serializedVersion: 2
|
||||
rgba: 4294967295
|
||||
m_fontSize: 20
|
||||
m_fontSizeBase: 20
|
||||
m_fontWeight: 400
|
||||
m_enableAutoSizing: 0
|
||||
m_fontSizeMin: 18
|
||||
m_fontSizeMax: 72
|
||||
m_fontStyle: 0
|
||||
m_HorizontalAlignment: 2
|
||||
m_VerticalAlignment: 512
|
||||
m_textAlignment: 65535
|
||||
m_characterSpacing: 0
|
||||
m_wordSpacing: 0
|
||||
m_lineSpacing: 0
|
||||
m_lineSpacingMax: 0
|
||||
m_paragraphSpacing: 0
|
||||
m_charWidthMaxAdj: 0
|
||||
m_TextWrappingMode: 1
|
||||
m_wordWrappingRatios: 0.4
|
||||
m_overflowMode: 0
|
||||
m_linkedTextComponent: {fileID: 0}
|
||||
parentLinkedComponent: {fileID: 0}
|
||||
m_enableKerning: 0
|
||||
m_ActiveFontFeatures: 6e72656b
|
||||
m_enableExtraPadding: 0
|
||||
checkPaddingRequired: 0
|
||||
m_isRichText: 1
|
||||
m_EmojiFallbackSupport: 1
|
||||
m_parseCtrlCharacters: 1
|
||||
m_isOrthographic: 1
|
||||
m_isCullingEnabled: 0
|
||||
m_horizontalMapping: 0
|
||||
m_verticalMapping: 0
|
||||
m_uvLineOffset: 0
|
||||
m_geometrySortingOrder: 0
|
||||
m_IsTextObjectScaleStatic: 0
|
||||
m_VertexBufferAutoSizeReduction: 0
|
||||
m_useMaxVisibleDescender: 1
|
||||
m_pageToDisplay: 1
|
||||
m_margin: {x: 0, y: 0, z: 0, w: 0}
|
||||
m_isUsingLegacyAnimationComponent: 0
|
||||
m_isVolumetricText: 0
|
||||
m_hasFontAssetChanged: 0
|
||||
m_baseMaterial: {fileID: 0}
|
||||
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
|
||||
Reference in New Issue
Block a user