Last active
November 18, 2025 06:33
-
-
Save TakaakiIchijo/a2c69edd2d3a3afc67f971ec40160f3a to your computer and use it in GitHub Desktop.
Extension Unity MultiPlayerPlaymode, Get IsClone and Number of Instances
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #if UNITY_EDITOR | |
| using System; | |
| using System.Reflection; | |
| using UnityEditor; | |
| using UnityEngine; | |
| namespace NetcodeExtension | |
| { | |
| [InitializeOnLoad] | |
| public static class MultiPlayerPlaymodeExtensions | |
| { | |
| public static readonly bool IsClone = false; | |
| public static readonly int PlayerCount = 0; | |
| static MultiPlayerPlaymodeExtensions() | |
| { | |
| //This Instance is Clone or not | |
| var targetType = Type.GetType("Unity.Multiplayer.Playmode.VirtualProjects.Editor.VirtualProjectsEditor,Unity.Multiplayer.Playmode.VirtualProjects.Editor"); | |
| if (targetType == null) | |
| { | |
| Debug.LogError("Type VirtualProjectsEditor not found."); | |
| } | |
| var targetPropertyInfo = targetType?.GetProperty("IsClone", BindingFlags.Public | BindingFlags.Static); | |
| if (targetPropertyInfo == null) | |
| { | |
| Debug.LogError("Event IsClone not found."); | |
| } | |
| if (targetPropertyInfo == null) | |
| { | |
| Debug.LogError("VirtualProjectsEditor IsClone property not found."); | |
| }else { | |
| IsClone = (bool)targetPropertyInfo.GetValue(null); | |
| Debug.Log("VirtualPlayer.IsClone "+IsClone); | |
| } | |
| //VirtualProjects + Main count | |
| targetType = Type.GetType("Unity.Multiplayer.Playmode.VirtualProjects.Editor.VirtualProjectsApi,Unity.Multiplayer.Playmode.VirtualProjects.Editor"); | |
| if (targetType == null) | |
| { | |
| Debug.LogError("Type VirtualProjectsEditor not found."); | |
| } | |
| var method = targetType?.GetMethod("GetProjects", BindingFlags.Public | BindingFlags.Static); | |
| if (method == null) | |
| { | |
| Debug.LogError("method GetProjects not found."); | |
| }else{ | |
| var result = new object[1] { "__all" }; | |
| if (result is Array arr) | |
| { | |
| PlayerCount = arr.Length + 1; | |
| Debug.Log("Multiplayer Play Mode Player count "+PlayerCount); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment