Added EditorScripts for reference purpose and modified the NEtworkingManager to use the inpsector more

This commit is contained in:
Albin Corén 2018-04-17 17:02:18 +02:00
parent 7054220a40
commit 045e458aa8
16 changed files with 692 additions and 29 deletions

View File

@ -0,0 +1,8 @@
using System;
[Serializable]
public class GithubAsset
{
public string browser_download_url;
public string name;
}

View File

@ -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;
}

View File

@ -0,0 +1,8 @@
using System;
[Serializable]
public class GithubAsset
{
public string browser_download_url;
public string name;
}

View File

@ -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;
}

View File

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{A45DBD43-D640-4562-9F24-6745269CEDF7}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MLAPI_Editor</RootNamespace>
<AssemblyName>MLAPI-Editor</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="UnityEditor">
<HintPath>..\..\..\..\..\Program Files\Unity\Editor\Data\Managed\UnityEditor.dll</HintPath>
</Reference>
<Reference Include="UnityEngine">
<HintPath>..\..\..\..\..\Program Files\Unity\Editor\Data\Managed\UnityEngine.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="GithubAsset.cs" />
<Compile Include="GithubRelease.cs" />
<Compile Include="MLAPIEditor.cs" />
<Compile Include="NetworkedAnimatorEditor.cs" />
<Compile Include="NetworkedBehaviourEditor.cs" />
<Compile Include="NetworkedObjectEditor.cs" />
<Compile Include="NetworkingManagerEditor.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TrackedObjectEditor.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MLAPI\MLAPI.csproj">
<Project>{EE431720-A9ED-43DC-9E74-10B693816D38}</Project>
<Name>MLAPI</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

173
MLAPI-Editor/MLAPIEditor.cs Normal file
View File

@ -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<MLAPIEditor>();
}
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<string> releasesJson = new List<string>();
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<GithubRelease>(releasesJson[i]);
if (i == 0)
foldoutStatus[i] = true;
else
foldoutStatus[i] = false;
}
}
}

View File

@ -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);
}
}
}
}

View File

@ -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<string> syncedVarNames = new List<string>();
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();
}
}
}

View File

@ -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);
}
}
}
}

View File

@ -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();
}
}

View File

@ -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")]

View File

@ -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();
}
}
}

View File

@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.27130.2027
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLAPI", "MLAPI\MLAPI.csproj", "{EE431720-A9ED-43DC-9E74-10B693816D38}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLAPI", "MLAPI\MLAPI.csproj", "{EE431720-A9ED-43DC-9E74-10B693816D38}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLAPI-Editor", "MLAPI-Editor\MLAPI-Editor.csproj", "{A45DBD43-D640-4562-9F24-6745269CEDF7}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{EE431720-A9ED-43DC-9E74-10B693816D38}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -50,13 +50,16 @@ namespace MLAPI.Data
/// <summary> /// <summary>
/// A list of spawnable prefabs /// A list of spawnable prefabs
/// </summary> /// </summary>
[HideInInspector]
public List<NetworkedPrefab> NetworkedPrefabs = new List<NetworkedPrefab>(); public List<NetworkedPrefab> NetworkedPrefabs = new List<NetworkedPrefab>();
internal Dictionary<string, int> NetworkPrefabIds; internal Dictionary<string, int> NetworkPrefabIds;
internal Dictionary<int, string> NetworkPrefabNames; internal Dictionary<int, string> NetworkPrefabNames;
/// <summary> /// <summary>
/// The default player prefab /// The default player prefab
/// </summary> /// </summary>
public string PlayerPrefabName; [SerializeField]
[HideInInspector]
internal string PlayerPrefabName;
/// <summary> /// <summary>
/// The size of the receive message buffer. This is the max message size. /// The size of the receive message buffer. This is the max message size.
/// </summary> /// </summary>
@ -100,6 +103,7 @@ namespace MLAPI.Data
/// <summary> /// <summary>
/// The data to send during connection which can be used to decide on if a client should get accepted /// The data to send during connection which can be used to decide on if a client should get accepted
/// </summary> /// </summary>
[HideInInspector]
public byte[] ConnectionData = new byte[0]; public byte[] ConnectionData = new byte[0];
/// <summary> /// <summary>
/// The amount of seconds to keep a lag compensation position history /// The amount of seconds to keep a lag compensation position history

View File

@ -1,4 +1,5 @@
using System; using MLAPI.MonoBehaviours.Core;
using System;
using UnityEngine; using UnityEngine;
namespace MLAPI.Data namespace MLAPI.Data
@ -9,13 +10,18 @@ namespace MLAPI.Data
[Serializable] [Serializable]
public class NetworkedPrefab public class NetworkedPrefab
{ {
/// <summary> internal string name
/// The name of the networked prefab {
/// </summary> get
public string name; {
return prefab.GetComponent<NetworkedObject>().NetworkedPrefabName;
}
}
/// <summary> /// <summary>
/// The gameobject of the prefab /// The gameobject of the prefab
/// </summary> /// </summary>
public GameObject prefab; public GameObject prefab;
public bool playerPrefab;
} }
} }

View File

@ -173,34 +173,21 @@ namespace MLAPI.MonoBehaviours.Core
{ {
if (string.IsNullOrEmpty(NetworkConfig.NetworkedPrefabs[i].name)) 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"); Debug.LogWarning("MLAPI: The NetworkedPrefab " + NetworkConfig.NetworkedPrefabs[i].prefab.name + " does not have a NetworkedPrefabName.");
NetworkConfig.NetworkedPrefabs[i].name = NetworkConfig.NetworkedPrefabs[i].prefab.name;
} }
} }
} int playerPrefabCount = NetworkConfig.NetworkedPrefabs.Count(x => x.playerPrefab == true);
if (playerPrefabCount == 0)
if (NetworkConfig.HandleObjectSpawning)
{
if(!string.IsNullOrEmpty(NetworkConfig.PlayerPrefabName))
{ {
//Handle spawning is on and a prefabName is set Debug.LogWarning("MLAPI: There is no NetworkedPrefab marked as a PlayerPrefab");
GameObject playerPrefab = null; }
for (int i = 0; i < NetworkConfig.NetworkedPrefabs.Count; i++) else if (playerPrefabCount > 1)
{ {
if (NetworkConfig.NetworkedPrefabs[i].name == NetworkConfig.PlayerPrefabName) Debug.LogWarning("MLAPI: Only one networked prefab can be marked as a player prefab");
{
playerPrefab = NetworkConfig.NetworkedPrefabs[i].prefab;
break;
}
}
if (playerPrefab == null)
Debug.LogWarning("MLAPI: There is no NetworkedPrefab with the name specified in the PlayerPrefabName");
} }
else else
{ NetworkConfig.PlayerPrefabName = NetworkConfig.NetworkedPrefabs.Find(x => x.playerPrefab == true).name;
//Handle spawning but no prefabName is set
Debug.LogWarning("MLAPI: There is no PlayerPrefabName set.");
}
} }
if (!NetworkConfig.EnableEncryption) if (!NetworkConfig.EnableEncryption)