From 045e458aa8367a6ea77e87986dd1438b89a11122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20Cor=C3=A9n?= <2108U9@gmail.com> Date: Tue, 17 Apr 2018 17:02:18 +0200 Subject: [PATCH] Added EditorScripts for reference purpose and modified the NEtworkingManager to use the inpsector more --- MLAPI-Editor/GithubAsset.cs | 8 + MLAPI-Editor/GithubRelease.cs | 13 ++ MLAPI-Editor/JSONStructs/GithubAsset.cs | 8 + MLAPI-Editor/JSONStructs/GithubRelease.cs | 13 ++ MLAPI-Editor/MLAPI-Editor.csproj | 66 +++++++ MLAPI-Editor/MLAPIEditor.cs | 173 ++++++++++++++++++ MLAPI-Editor/NetworkedAnimatorEditor.cs | 97 ++++++++++ MLAPI-Editor/NetworkedBehaviourEditor.cs | 84 +++++++++ MLAPI-Editor/NetworkedObjectEditor.cs | 51 ++++++ MLAPI-Editor/NetworkingManagerEditor.cs | 75 ++++++++ MLAPI-Editor/Properties/AssemblyInfo.cs | 36 ++++ MLAPI-Editor/TrackedObjectEditor.cs | 34 ++++ MLAPI.sln | 8 + MLAPI/Data/NetworkConfig.cs | 6 +- MLAPI/Data/NetworkedPrefab.cs | 16 +- .../MonoBehaviours/Core/NetworkingManager.cs | 33 +--- 16 files changed, 692 insertions(+), 29 deletions(-) create mode 100644 MLAPI-Editor/GithubAsset.cs create mode 100644 MLAPI-Editor/GithubRelease.cs create mode 100644 MLAPI-Editor/JSONStructs/GithubAsset.cs create mode 100644 MLAPI-Editor/JSONStructs/GithubRelease.cs create mode 100644 MLAPI-Editor/MLAPI-Editor.csproj create mode 100644 MLAPI-Editor/MLAPIEditor.cs create mode 100644 MLAPI-Editor/NetworkedAnimatorEditor.cs create mode 100644 MLAPI-Editor/NetworkedBehaviourEditor.cs create mode 100644 MLAPI-Editor/NetworkedObjectEditor.cs create mode 100644 MLAPI-Editor/NetworkingManagerEditor.cs create mode 100644 MLAPI-Editor/Properties/AssemblyInfo.cs create mode 100644 MLAPI-Editor/TrackedObjectEditor.cs diff --git a/MLAPI-Editor/GithubAsset.cs b/MLAPI-Editor/GithubAsset.cs new file mode 100644 index 0000000..a407e74 --- /dev/null +++ b/MLAPI-Editor/GithubAsset.cs @@ -0,0 +1,8 @@ +using System; + +[Serializable] +public class GithubAsset +{ + public string browser_download_url; + public string name; +} diff --git a/MLAPI-Editor/GithubRelease.cs b/MLAPI-Editor/GithubRelease.cs new file mode 100644 index 0000000..fc5fe0a --- /dev/null +++ b/MLAPI-Editor/GithubRelease.cs @@ -0,0 +1,13 @@ +using System; + +[Serializable] +public class GithubRelease +{ + public string html_url; + public string tag_name; + public string name; + public string body; + public string published_at; + public bool prerelease; + public GithubAsset[] assets; +} diff --git a/MLAPI-Editor/JSONStructs/GithubAsset.cs b/MLAPI-Editor/JSONStructs/GithubAsset.cs new file mode 100644 index 0000000..a407e74 --- /dev/null +++ b/MLAPI-Editor/JSONStructs/GithubAsset.cs @@ -0,0 +1,8 @@ +using System; + +[Serializable] +public class GithubAsset +{ + public string browser_download_url; + public string name; +} diff --git a/MLAPI-Editor/JSONStructs/GithubRelease.cs b/MLAPI-Editor/JSONStructs/GithubRelease.cs new file mode 100644 index 0000000..fc5fe0a --- /dev/null +++ b/MLAPI-Editor/JSONStructs/GithubRelease.cs @@ -0,0 +1,13 @@ +using System; + +[Serializable] +public class GithubRelease +{ + public string html_url; + public string tag_name; + public string name; + public string body; + public string published_at; + public bool prerelease; + public GithubAsset[] assets; +} diff --git a/MLAPI-Editor/MLAPI-Editor.csproj b/MLAPI-Editor/MLAPI-Editor.csproj new file mode 100644 index 0000000..df4bf2e --- /dev/null +++ b/MLAPI-Editor/MLAPI-Editor.csproj @@ -0,0 +1,66 @@ + + + + + Debug + AnyCPU + {A45DBD43-D640-4562-9F24-6745269CEDF7} + Library + Properties + MLAPI_Editor + MLAPI-Editor + v4.6.1 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + ..\..\..\..\..\Program Files\Unity\Editor\Data\Managed\UnityEditor.dll + + + ..\..\..\..\..\Program Files\Unity\Editor\Data\Managed\UnityEngine.dll + + + + + + + + + + + + + + + + {EE431720-A9ED-43DC-9E74-10B693816D38} + MLAPI + + + + \ No newline at end of file diff --git a/MLAPI-Editor/MLAPIEditor.cs b/MLAPI-Editor/MLAPIEditor.cs new file mode 100644 index 0000000..ff0ab69 --- /dev/null +++ b/MLAPI-Editor/MLAPIEditor.cs @@ -0,0 +1,173 @@ +using System; +using System.Collections.Generic; +using System.IO; +using UnityEditor; +using UnityEngine; + +public class MLAPIEditor : EditorWindow +{ + private GithubRelease[] releases = new GithubRelease[0]; + private bool[] foldoutStatus = new bool[0]; + private long lastUpdated = 0; + private string currentVersion; + + [MenuItem("Window/MLAPI")] + public static void ShowWindow() + { + GetWindow(); + } + + private void Init() + { + lastUpdated = 0; + + if (EditorPrefs.HasKey("MLAPI_version")) + currentVersion = EditorPrefs.GetString("MLAPI_version"); + else + currentVersion = "None"; + } + + private void Awake() + { + Init(); + } + + private void OnFocus() + { + Init(); + } + + private void OnGUI() + { + if(foldoutStatus != null) + { + for (int i = 0; i < foldoutStatus.Length; i++) + { + if (releases[i] == null) + continue; + foldoutStatus[i] = EditorGUILayout.Foldout(foldoutStatus[i], releases[i].tag_name + " - " + releases[i].name); + if (foldoutStatus[i]) + { + EditorGUI.indentLevel++; + EditorGUILayout.LabelField("Release notes", EditorStyles.boldLabel); + EditorGUILayout.LabelField(releases[i].body, EditorStyles.wordWrappedLabel); + EditorGUILayout.Space(); + EditorGUILayout.Space(); + if (releases[i].prerelease) + { + GUIStyle style = new GUIStyle(EditorStyles.boldLabel); + style.normal.textColor = new Color(1f, 0.5f, 0f); + EditorGUILayout.LabelField("Pre-release", style); + } + else + { + GUIStyle style = new GUIStyle(EditorStyles.boldLabel); + style.normal.textColor = new Color(0f, 1f, 0f); + EditorGUILayout.LabelField("Stable-release", style); + } + if (currentVersion == releases[i].tag_name) + { + GUIStyle boldStyle = new GUIStyle(EditorStyles.boldLabel); + boldStyle.normal.textColor = new Color(0.3f, 1f, 0.3f); + EditorGUILayout.LabelField("Installed", boldStyle); + } + EditorGUILayout.LabelField("Release date: " + DateTime.Parse(DateTime.Parse(releases[i].published_at).ToString()), EditorStyles.miniBoldLabel); + + if(currentVersion != releases[i].tag_name && GUILayout.Button("Install")) + InstallRelease(i); + + EditorGUI.indentLevel--; + } + } + } + + GUILayout.BeginArea(new Rect(5, position.height - 20, position.width, 20)); + + if (GUILayout.Button("Check for updates")) + GetReleases(); + + GUILayout.EndArea(); + + string lastUpdatedString = lastUpdated == 0 ? "Never" : new DateTime(lastUpdated).ToShortTimeString(); + EditorGUI.LabelField(new Rect(5, position.height - 40, position.width, 20), "Last checked: " + lastUpdatedString, EditorStyles.centeredGreyMiniLabel); + + if ((DateTime.Now - new DateTime(lastUpdated)).Seconds > 3600) + GetReleases(); + + Repaint(); + } + + private void InstallRelease(int index) + { + for (int i = 0; i < releases[index].assets.Length; i++) + { + WWW www = new WWW(releases[index].assets[i].browser_download_url); + while (!www.isDone && string.IsNullOrEmpty(www.error)) + { + EditorGUI.ProgressBar(new Rect(5, position.height - 60, position.width, 20), www.progress, "Installing " + i + "/" + releases[index].assets.Length); + } + + if(!Directory.Exists(Application.dataPath + "/MLAPI/Lib/")) + Directory.CreateDirectory(Application.dataPath + "/MLAPI/Lib/"); + + File.WriteAllBytes(Application.dataPath + "/MLAPI/Lib/" + releases[index].assets[i].name, www.bytes); + + if (releases[index].assets[i].name.EndsWith(".unitypackage")) + AssetDatabase.ImportPackage(Application.dataPath + "/MLAPI/Lib/" + releases[index].assets[i].name, true); + } + + EditorPrefs.SetString("MLAPI_version", releases[index].tag_name); + currentVersion = releases[index].tag_name; + AssetDatabase.Refresh(); + } + + private void GetReleases() + { + lastUpdated = DateTime.Now.Ticks; + + WWW www = new WWW("https://api.github.com/repos/TwoTenPvP/MLAPI/releases"); + while(!www.isDone && string.IsNullOrEmpty(www.error)) + { + EditorGUI.ProgressBar(new Rect(5, position.height - 60, position.width, 20), www.progress, "Fetching..."); + } + string json = www.text; + + //This makes it from a json array to the individual objects in the array. + //The JSON serializer cant take arrays. We have to split it up outselves. + List releasesJson = new List(); + int depth = 0; + string currentObject = ""; + for (int i = 1; i < json.Length - 1; i++) + { + if (json[i] == '[') + depth++; + else if (json[i] == ']') + depth--; + else if (json[i] == '{') + depth++; + else if (json[i] == '}') + depth--; + + if ((depth == 0 && json[i] != ',') || depth > 0) + currentObject += json[i]; + + if (depth == 0 && json[i] == ',') + { + releasesJson.Add(currentObject); + currentObject = ""; + } + } + + releases = new GithubRelease[releasesJson.Count]; + foldoutStatus = new bool[releasesJson.Count]; + + for (int i = 0; i < releasesJson.Count; i++) + { + releases[i] = JsonUtility.FromJson(releasesJson[i]); + if (i == 0) + foldoutStatus[i] = true; + else + foldoutStatus[i] = false; + } + } +} diff --git a/MLAPI-Editor/NetworkedAnimatorEditor.cs b/MLAPI-Editor/NetworkedAnimatorEditor.cs new file mode 100644 index 0000000..bbe7f04 --- /dev/null +++ b/MLAPI-Editor/NetworkedAnimatorEditor.cs @@ -0,0 +1,97 @@ +using MLAPI.MonoBehaviours.Prototyping; +using System; +using UnityEditor.Animations; +using UnityEngine; + +namespace UnityEditor +{ + [CustomEditor(typeof(NetworkedAnimator), true)] + [CanEditMultipleObjects] + public class NetworkAnimatorEditor : Editor + { + private NetworkedAnimator networkedAnimatorTarget; + [NonSerialized] + private bool initialized; + + private SerializedProperty animatorProperty; + private GUIContent animatorLabel; + + void Init() + { + if (initialized) + return; + + initialized = true; + networkedAnimatorTarget = target as NetworkedAnimator; + + animatorProperty = serializedObject.FindProperty("_animator"); + animatorLabel = new GUIContent("Animator", "The Animator component to synchronize."); + } + + public override void OnInspectorGUI() + { + Init(); + serializedObject.Update(); + DrawControls(); + serializedObject.ApplyModifiedProperties(); + } + + void DrawControls() + { + EditorGUI.BeginChangeCheck(); + EditorGUILayout.PropertyField(animatorProperty, animatorLabel); + + if (EditorGUI.EndChangeCheck()) + networkedAnimatorTarget.ResetParameterOptions(); + + if (networkedAnimatorTarget.animator == null) + return; + + var controller = networkedAnimatorTarget.animator.runtimeAnimatorController as AnimatorController; + if (controller != null) + { + var showWarning = false; + EditorGUI.indentLevel += 1; + int i = 0; + + foreach (var p in controller.parameters) + { + if (i >= 32) + { + showWarning = true; + break; + } + + bool oldSend = networkedAnimatorTarget.GetParameterAutoSend(i); + bool send = EditorGUILayout.Toggle(p.name, oldSend); + if (send != oldSend) + { + networkedAnimatorTarget.SetParameterAutoSend(i, send); + EditorUtility.SetDirty(target); + } + i += 1; + } + + if (showWarning) + EditorGUILayout.HelpBox("NetworkAnimator can only select between the first 32 parameters in a mecanim controller", MessageType.Warning); + + EditorGUI.indentLevel -= 1; + } + + if (Application.isPlaying) + { + EditorGUILayout.Separator(); + if (networkedAnimatorTarget.param0 != "") + EditorGUILayout.LabelField("Param 0", networkedAnimatorTarget.param0); + if (networkedAnimatorTarget.param1 != "") + EditorGUILayout.LabelField("Param 1", networkedAnimatorTarget.param1); + if (networkedAnimatorTarget.param2 != "") + EditorGUILayout.LabelField("Param 2", networkedAnimatorTarget.param2); + if (networkedAnimatorTarget.param3 != "") + EditorGUILayout.LabelField("Param 3", networkedAnimatorTarget.param3); + if (networkedAnimatorTarget.param4 != "") + EditorGUILayout.LabelField("Param 4", networkedAnimatorTarget.param4); + } + } + } +} diff --git a/MLAPI-Editor/NetworkedBehaviourEditor.cs b/MLAPI-Editor/NetworkedBehaviourEditor.cs new file mode 100644 index 0000000..c72ccec --- /dev/null +++ b/MLAPI-Editor/NetworkedBehaviourEditor.cs @@ -0,0 +1,84 @@ +using MLAPI.Attributes; +using MLAPI.MonoBehaviours.Core; +using System; +using System.Collections.Generic; +using System.Reflection; +using UnityEngine; + +namespace UnityEditor +{ + [CustomEditor(typeof(NetworkedBehaviour), true)] + [CanEditMultipleObjects] + public class NetworkedBehaviourInspector : Editor + { + private bool initialized; + protected List syncedVarNames = new List(); + + private GUIContent syncedVarLabelGuiContent; + + private void Init(MonoScript script) + { + initialized = true; + + syncedVarLabelGuiContent = new GUIContent("SyncedVar", "This variable has been marked with the [SyncedVar] attribute."); + + FieldInfo[] fields = script.GetClass().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic); + for (int i = 0; i < fields.Length; i++) + { + Attribute[] attributes = (Attribute[])fields[i].GetCustomAttributes(typeof(SyncedVar), true); + if (attributes.Length > 0) + syncedVarNames.Add(fields[i].Name); + } + } + + public override void OnInspectorGUI() + { + if (!initialized) + { + serializedObject.Update(); + SerializedProperty scriptProperty = serializedObject.FindProperty("m_Script"); + if (scriptProperty == null) + return; + + MonoScript targetScript = scriptProperty.objectReferenceValue as MonoScript; + Init(targetScript); + } + + EditorGUI.BeginChangeCheck(); + serializedObject.Update(); + + SerializedProperty property = serializedObject.GetIterator(); + bool expanded = true; + while (property.NextVisible(expanded)) + { + bool isSyncVar = syncedVarNames.Contains(property.name); + if (property.propertyType == SerializedPropertyType.ObjectReference) + { + if (property.name == "m_Script") + EditorGUI.BeginDisabledGroup(true); + + EditorGUILayout.PropertyField(property, true); + + if (isSyncVar) + GUILayout.Label(syncedVarLabelGuiContent, EditorStyles.miniLabel, GUILayout.Width(EditorStyles.miniLabel.CalcSize(syncedVarLabelGuiContent).x)); + + if (property.name == "m_Script") + EditorGUI.EndDisabledGroup(); + } + else + { + EditorGUILayout.BeginHorizontal(); + EditorGUILayout.PropertyField(property, true); + + if (isSyncVar) + GUILayout.Label(syncedVarLabelGuiContent, EditorStyles.miniLabel, GUILayout.Width(EditorStyles.miniLabel.CalcSize(syncedVarLabelGuiContent).x)); + + EditorGUILayout.EndHorizontal(); + } + expanded = false; + } + serializedObject.ApplyModifiedProperties(); + EditorGUI.EndChangeCheck(); + } + } +} diff --git a/MLAPI-Editor/NetworkedObjectEditor.cs b/MLAPI-Editor/NetworkedObjectEditor.cs new file mode 100644 index 0000000..65ae98b --- /dev/null +++ b/MLAPI-Editor/NetworkedObjectEditor.cs @@ -0,0 +1,51 @@ +using MLAPI.MonoBehaviours.Core; +using UnityEngine; + +namespace UnityEditor +{ + [CustomEditor(typeof(NetworkedObject), true)] + [CanEditMultipleObjects] + public class NetworkedObjectEditor : Editor + { + private bool initialized; + private NetworkedObject networkedObject; + + private void Init() + { + if (initialized) + return; + initialized = true; + networkedObject = (NetworkedObject)target; + } + + public override void OnInspectorGUI() + { + Init(); + if (NetworkingManager.singleton == null || (!NetworkingManager.singleton.isServer && !NetworkingManager.singleton.isClient)) + base.OnInspectorGUI(); //Only run this if we are NOT running server. This is where the ServerOnly box is drawn + + if (!networkedObject.isSpawned && NetworkingManager.singleton != null && NetworkingManager.singleton.isServer) + { + EditorGUILayout.BeginHorizontal(); + EditorGUILayout.LabelField(new GUIContent("Spawn", "Spawns the object across the network")); + if (GUILayout.Toggle(false, "Spawn", EditorStyles.miniButtonLeft)) + { + networkedObject.Spawn(); + EditorUtility.SetDirty(target); + } + EditorGUILayout.EndHorizontal(); + } + else if(networkedObject.isSpawned) + { + EditorGUILayout.LabelField("NetworkId: ", networkedObject.NetworkId.ToString(), EditorStyles.label); + EditorGUILayout.LabelField("OwnerId: ", networkedObject.OwnerClientId.ToString(), EditorStyles.label); + EditorGUILayout.LabelField("isSpawned: ", networkedObject.isSpawned.ToString(), EditorStyles.label); + EditorGUILayout.LabelField("isLocalPlayer: ", networkedObject.isLocalPlayer.ToString(), EditorStyles.label); + EditorGUILayout.LabelField("isOwner: ", networkedObject.isOwner.ToString(), EditorStyles.label); + EditorGUILayout.LabelField("isPoolObject: ", networkedObject.isPlayerObject.ToString(), EditorStyles.label); + EditorGUILayout.LabelField("isPlayerObject: ", networkedObject.isPlayerObject.ToString(), EditorStyles.label); + //EditorGUILayout.LabelField("ServerOnly: ", networkedObject.ServerOnly.ToString(), EditorStyles.label); + } + } + } +} diff --git a/MLAPI-Editor/NetworkingManagerEditor.cs b/MLAPI-Editor/NetworkingManagerEditor.cs new file mode 100644 index 0000000..5fcf807 --- /dev/null +++ b/MLAPI-Editor/NetworkingManagerEditor.cs @@ -0,0 +1,75 @@ +using MLAPI.MonoBehaviours.Core; +using UnityEditor; +using UnityEngine; +using UnityEditorInternal; + +[CustomEditor(typeof(NetworkingManager), true)] +[CanEditMultipleObjects] +public class NetworkingManagerEditor : Editor +{ + private ReorderableList networkedObjectList; + + private NetworkingManager networkingManager; + private bool initialized; + + private void Init() + { + if (initialized) + return; + initialized = true; + networkingManager = (NetworkingManager)target; + } + + private void OnEnable() + { + networkedObjectList = new ReorderableList(serializedObject, serializedObject.FindProperty("NetworkConfig").FindPropertyRelative("NetworkedPrefabs"), true, true, true, true); + networkedObjectList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => + { + var element = networkedObjectList.serializedProperty.GetArrayElementAtIndex(index); + rect.y += 2; + EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width - 30, EditorGUIUtility.singleLineHeight), + element.FindPropertyRelative("prefab"), GUIContent.none); + EditorGUI.PropertyField(new Rect(rect.x + rect.width - 30, rect.y, 30, EditorGUIUtility.singleLineHeight), + element.FindPropertyRelative("playerPrefab"), GUIContent.none); + }; + + networkedObjectList.drawHeaderCallback = (Rect rect) => { + EditorGUI.LabelField(rect, "Networked Prefabs"); + }; + } + + public override void OnInspectorGUI() + { + Init(); + if (!networkingManager.isServer && !networkingManager.isClient) + { + EditorGUILayout.Space(); + serializedObject.Update(); + networkedObjectList.DoLayoutList(); + serializedObject.ApplyModifiedProperties(); + base.OnInspectorGUI(); //Only draw if we don't have a running client or server + } + else + { + string instanceType = ""; + if (networkingManager.isHost) + instanceType = "Host"; + else if (networkingManager.isServer) + instanceType = "Server"; + else if (networkingManager.isClient) + instanceType = "Client"; + + EditorGUILayout.HelpBox("You cannot edit the NetworkConfig when a " + instanceType + " is running", MessageType.Info); + if (GUILayout.Toggle(false, "Stop " + instanceType, EditorStyles.miniButtonMid)) + { + if (networkingManager.isHost) + networkingManager.StopHost(); + else if (networkingManager.isServer) + networkingManager.StopServer(); + else if (networkingManager.isClient) + networkingManager.StopClient(); + } + } + Repaint(); + } +} diff --git a/MLAPI-Editor/Properties/AssemblyInfo.cs b/MLAPI-Editor/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..225a422 --- /dev/null +++ b/MLAPI-Editor/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("MLAPI-Editor")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("MLAPI-Editor")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("a45dbd43-d640-4562-9f24-6745269cedf7")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/MLAPI-Editor/TrackedObjectEditor.cs b/MLAPI-Editor/TrackedObjectEditor.cs new file mode 100644 index 0000000..ebed8b1 --- /dev/null +++ b/MLAPI-Editor/TrackedObjectEditor.cs @@ -0,0 +1,34 @@ +using MLAPI.MonoBehaviours.Core; +using UnityEngine; + +namespace UnityEditor +{ + [CustomEditor(typeof(TrackedObject), true)] + [CanEditMultipleObjects] + public class TrackedObjectEditor : Editor + { + private TrackedObject trackedObject; + private bool initialized; + + private void Init() + { + if (initialized) + return; + + trackedObject = (TrackedObject)target; + initialized = true; + } + + public override void OnInspectorGUI() + { + Init(); + base.OnInspectorGUI(); + if(NetworkingManager.singleton != null && NetworkingManager.singleton.isServer) + { + EditorGUILayout.LabelField("Total points: ", trackedObject.TotalPoints.ToString(), EditorStyles.label); + EditorGUILayout.LabelField("Avg time between points: ", trackedObject.AvgTimeBetweenPointsMs.ToString() + " ms", EditorStyles.label); + } + Repaint(); + } + } +} diff --git a/MLAPI.sln b/MLAPI.sln index ba2e831..53a018d 100644 --- a/MLAPI.sln +++ b/MLAPI.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.27130.2027 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLAPI", "MLAPI\MLAPI.csproj", "{EE431720-A9ED-43DC-9E74-10B693816D38}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLAPI-Editor", "MLAPI-Editor\MLAPI-Editor.csproj", "{A45DBD43-D640-4562-9F24-6745269CEDF7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -18,6 +20,12 @@ Global {EE431720-A9ED-43DC-9E74-10B693816D38}.Development|Any CPU.Build.0 = Development|Any CPU {EE431720-A9ED-43DC-9E74-10B693816D38}.Release|Any CPU.ActiveCfg = Release|Any CPU {EE431720-A9ED-43DC-9E74-10B693816D38}.Release|Any CPU.Build.0 = Release|Any CPU + {A45DBD43-D640-4562-9F24-6745269CEDF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A45DBD43-D640-4562-9F24-6745269CEDF7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A45DBD43-D640-4562-9F24-6745269CEDF7}.Development|Any CPU.ActiveCfg = Debug|Any CPU + {A45DBD43-D640-4562-9F24-6745269CEDF7}.Development|Any CPU.Build.0 = Debug|Any CPU + {A45DBD43-D640-4562-9F24-6745269CEDF7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A45DBD43-D640-4562-9F24-6745269CEDF7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/MLAPI/Data/NetworkConfig.cs b/MLAPI/Data/NetworkConfig.cs index c49b806..5c9bf3d 100644 --- a/MLAPI/Data/NetworkConfig.cs +++ b/MLAPI/Data/NetworkConfig.cs @@ -50,13 +50,16 @@ namespace MLAPI.Data /// /// A list of spawnable prefabs /// + [HideInInspector] public List NetworkedPrefabs = new List(); internal Dictionary NetworkPrefabIds; internal Dictionary NetworkPrefabNames; /// /// The default player prefab /// - public string PlayerPrefabName; + [SerializeField] + [HideInInspector] + internal string PlayerPrefabName; /// /// The size of the receive message buffer. This is the max message size. /// @@ -100,6 +103,7 @@ namespace MLAPI.Data /// /// The data to send during connection which can be used to decide on if a client should get accepted /// + [HideInInspector] public byte[] ConnectionData = new byte[0]; /// /// The amount of seconds to keep a lag compensation position history diff --git a/MLAPI/Data/NetworkedPrefab.cs b/MLAPI/Data/NetworkedPrefab.cs index 9354827..e8d9eef 100644 --- a/MLAPI/Data/NetworkedPrefab.cs +++ b/MLAPI/Data/NetworkedPrefab.cs @@ -1,4 +1,5 @@ -using System; +using MLAPI.MonoBehaviours.Core; +using System; using UnityEngine; namespace MLAPI.Data @@ -9,13 +10,18 @@ namespace MLAPI.Data [Serializable] public class NetworkedPrefab { - /// - /// The name of the networked prefab - /// - public string name; + internal string name + { + get + { + return prefab.GetComponent().NetworkedPrefabName; + } + } /// /// The gameobject of the prefab /// public GameObject prefab; + + public bool playerPrefab; } } diff --git a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs index d07274e..8d8c465 100644 --- a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs +++ b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs @@ -173,34 +173,21 @@ namespace MLAPI.MonoBehaviours.Core { if (string.IsNullOrEmpty(NetworkConfig.NetworkedPrefabs[i].name)) { - Debug.LogWarning("MLAPI: The NetworkedPrefab " + NetworkConfig.NetworkedPrefabs[i].prefab.name + " does not have a NetworkedPrefabName. It has been set to the gameObject name"); - NetworkConfig.NetworkedPrefabs[i].name = NetworkConfig.NetworkedPrefabs[i].prefab.name; + Debug.LogWarning("MLAPI: The NetworkedPrefab " + NetworkConfig.NetworkedPrefabs[i].prefab.name + " does not have a NetworkedPrefabName."); } } - } - - if (NetworkConfig.HandleObjectSpawning) - { - if(!string.IsNullOrEmpty(NetworkConfig.PlayerPrefabName)) + int playerPrefabCount = NetworkConfig.NetworkedPrefabs.Count(x => x.playerPrefab == true); + if (playerPrefabCount == 0) { - //Handle spawning is on and a prefabName is set - GameObject playerPrefab = null; - for (int i = 0; i < NetworkConfig.NetworkedPrefabs.Count; i++) - { - if (NetworkConfig.NetworkedPrefabs[i].name == NetworkConfig.PlayerPrefabName) - { - playerPrefab = NetworkConfig.NetworkedPrefabs[i].prefab; - break; - } - } - if (playerPrefab == null) - Debug.LogWarning("MLAPI: There is no NetworkedPrefab with the name specified in the PlayerPrefabName"); + Debug.LogWarning("MLAPI: There is no NetworkedPrefab marked as a PlayerPrefab"); + } + else if (playerPrefabCount > 1) + { + Debug.LogWarning("MLAPI: Only one networked prefab can be marked as a player prefab"); } else - { - //Handle spawning but no prefabName is set - Debug.LogWarning("MLAPI: There is no PlayerPrefabName set."); - } + NetworkConfig.PlayerPrefabName = NetworkConfig.NetworkedPrefabs.Find(x => x.playerPrefab == true).name; + } if (!NetworkConfig.EnableEncryption)