Last active
January 25, 2026 11:02
-
-
Save TakaakiIchijo/7dae5c3f2f071405338f5b8f114e9fb2 to your computer and use it in GitHub Desktop.
Detect this player is vitual player or not in Unity Multiplayer Play Mode package
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; | |
| using Unity.Multiplayer.PlayMode.Editor; | |
| 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.Editor.VirtualProjects.Editor.VirtualProjectsEditor," + "Unity.Multiplayer.PlayMode.Editor.VirtualProjects.Editor"); | |
| //for Multi Player Playmode v1.x | |
| //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.Editor.VirtualProjectsEditor.VirtualProjectsApi,Unity.Multiplayer.PlayMode.Editor.VirtualProjectsEditor"); | |
| //for Multi Player Playmode v1.x | |
| //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