Skip to content

Commit 9b8746d

Browse files
committed
Merge branch 'master' of https://github.com/MidLevel/MLAPI
2 parents ea626b5 + 96e4643 commit 9b8746d

10 files changed

+420
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using MLAPI;
2+
3+
namespace MLAPI_Examples
4+
{
5+
public class ConvenienceMessagingPermission : NetworkedBehaviour
6+
{
7+
[ServerRPC(RequireOwnership = false)]
8+
public void RunCodeOnServerWithoutOwnershipChecks(int randomNumber, int constantNumber)
9+
{
10+
// Both parameters are sent over the network
11+
// This method can be invoked by any client. If you need permissions verify the sender
12+
}
13+
14+
// Require ownership defaults to true
15+
[ServerRPC]
16+
public void RunCodeOnServerWithOwnershipChecks()
17+
{
18+
// Both parameters are sent over the network
19+
// This method will ONLY run when invoked by the owner of the object and is thus safe
20+
}
21+
}
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using MLAPI;
2+
using UnityEngine;
3+
4+
namespace MLAPI_Examples
5+
{
6+
public class ConvenienceMessagingPing : NetworkedBehaviour
7+
{
8+
public override void NetworkStart()
9+
{
10+
if (IsClient)
11+
{
12+
int rnd = Random.Range(0, int.MaxValue);
13+
14+
Debug.LogFormat("Pining server with number {0}", rnd);
15+
16+
InvokeServerRpc(PingServer, rnd);
17+
}
18+
}
19+
20+
[ServerRPC(RequireOwnership = false)]
21+
public void PingServer(int number)
22+
{
23+
uint sender = ExecutingRpcSender;
24+
25+
Debug.LogFormat("Got pinged by {0} with the number {1}", sender, number);
26+
27+
// Sends the number back to client
28+
InvokeClientRpcOnClient(PingClient, sender, number);
29+
}
30+
31+
[ClientRPC]
32+
public void PingClient(int number)
33+
{
34+
Debug.LogFormat("Server replied with {0}!", number);
35+
}
36+
}
37+
}

MLAPI-Examples/MLAPI-Examples.csproj

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{FED83E10-8157-42C6-A58F-C2545C0D93B4}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>MLAPI_Examples</RootNamespace>
11+
<AssemblyName>MLAPI-Examples</AssemblyName>
12+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="System" />
36+
<Reference Include="System.Core" />
37+
<Reference Include="System.Data" />
38+
<Reference Include="System.Xml" />
39+
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
40+
<HintPath>..\Libraries\Unity\UnityEngine.dll</HintPath>
41+
</Reference>
42+
</ItemGroup>
43+
<ItemGroup>
44+
<Compile Include="ConvenienceMessagingPermission.cs" />
45+
<Compile Include="ConvenienceMessagingPing.cs" />
46+
<Compile Include="ManagerExamples.cs" />
47+
<Compile Include="NetworkedBehaviourCallbacks.cs" />
48+
<Compile Include="Properties\AssemblyInfo.cs" />
49+
<Compile Include="RunRpcOnOtherGameObject.cs" />
50+
<Compile Include="SerializedNetworkedVarExample.cs" />
51+
<Compile Include="UnetNetworkingManagerHud.cs" />
52+
</ItemGroup>
53+
<ItemGroup>
54+
<ProjectReference Include="..\MLAPI\MLAPI.csproj">
55+
<Project>{ee431720-a9ed-43dc-9e74-10b693816d38}</Project>
56+
<Name>MLAPI</Name>
57+
</ProjectReference>
58+
</ItemGroup>
59+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
60+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
61+
Other similar extension points exist, see Microsoft.Common.targets.
62+
<Target Name="BeforeBuild">
63+
</Target>
64+
<Target Name="AfterBuild">
65+
</Target>
66+
-->
67+
</Project>

MLAPI-Examples/ManagerExamples.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using MLAPI;
2+
using MLAPI.Components;
3+
using MLAPI.Cryptography;
4+
using MLAPI.Data;
5+
6+
namespace MLAPI_Examples
7+
{
8+
// Features example calls for things often needed for things like Game managers
9+
public class ManagerExamples : NetworkedBehaviour
10+
{
11+
public NetworkedObject GetPlayerGameObject(uint clientId)
12+
{
13+
return SpawnManager.GetPlayerObject(clientId);
14+
}
15+
16+
// Only runs on host and client
17+
public NetworkedObject GetLocalPlayerObject()
18+
{
19+
return SpawnManager.GetLocalPlayerObject();
20+
}
21+
22+
// Only runs on server
23+
public byte[] GetAESKeyForClient(uint clientId)
24+
{
25+
return CryptographyHelper.GetClientKey(clientId);
26+
}
27+
28+
// Only runs on client
29+
public byte[] GetAESKeyForServer()
30+
{
31+
return CryptographyHelper.GetServerKey();
32+
}
33+
34+
// Contains player object, owned objects, cryptography keys and more
35+
public NetworkedClient GetClient(uint clientId)
36+
{
37+
return NetworkingManager.Singleton.ConnectedClients[clientId];
38+
}
39+
}
40+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.IO;
2+
using MLAPI;
3+
4+
namespace MLAPI_Examples
5+
{
6+
public class NetworkedBehaviourCallbacks : NetworkedBehaviour
7+
{
8+
public override void NetworkStart(Stream payload)
9+
{
10+
// Invoked when the object is spawned and ready
11+
// All properties like IsLocalPlayer etc is guaranteed to be ready
12+
// The NetWorkStart can be used with or without the "Stream payload" parameter
13+
// If it's included it will contain the payload included in the Spawn method if applicable.
14+
}
15+
16+
public override void OnEnabled()
17+
{
18+
// Use this instead of OnEnabled, that is because the OnEnable method is stolen by the NetworkedBehaviour class
19+
// Usage of OnEnable will throw a error pointing you to this method and WILL break the behaviour
20+
}
21+
22+
public override void OnDisabled()
23+
{
24+
// Use this instead of OnDisable, that is because the OnDisable method is stolen by the NetworkedBehaviour class
25+
// Usage of OnDisable will throw a error pointing you to this method and WILL break the behaviour
26+
}
27+
28+
public override void OnDestroyed()
29+
{
30+
// Use this instead of OnDestroy, that is because the OnDestroy method is stolen by the NetworkedBehaviour class
31+
// Usage of OnDestroy will throw a error pointing you to this method and WILL break the behaviour
32+
}
33+
}
34+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
4+
// General Information about an assembly is controlled through the following
5+
// set of attributes. Change these attribute values to modify the information
6+
// associated with an assembly.
7+
[assembly: AssemblyTitle("MLAPI_Examples")]
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("MLAPI_Examples")]
12+
[assembly: AssemblyCopyright("Copyright © 2019")]
13+
[assembly: AssemblyTrademark("")]
14+
[assembly: AssemblyCulture("")]
15+
16+
// Setting ComVisible to false makes the types in this assembly not visible
17+
// to COM components. If you need to access a type in this assembly from
18+
// COM, set the ComVisible attribute to true on that type.
19+
[assembly: ComVisible(false)]
20+
21+
// The following GUID is for the ID of the typelib if this project is exposed to COM
22+
[assembly: Guid("FED83E10-8157-42C6-A58F-C2545C0D93B4")]
23+
24+
// Version information for an assembly consists of the following four values:
25+
//
26+
// Major Version
27+
// Minor Version
28+
// Build Number
29+
// Revision
30+
//
31+
// You can specify all the values or you can default the Build and Revision Numbers
32+
// by using the '*' as shown below:
33+
// [assembly: AssemblyVersion("1.0.*")]
34+
[assembly: AssemblyVersion("1.0.0.0")]
35+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using MLAPI;
2+
using UnityEngine;
3+
4+
namespace MLAPI_Examples
5+
{
6+
// Note that the script invoking doesn't have to be a NetworkedBehaviour
7+
public class RunRpcOnOtherGameObject : MonoBehaviour
8+
{
9+
public ConvenienceMessagingPing Target;
10+
11+
public void OnEnable()
12+
{
13+
// Note this is NOT good practice. Don't rely on time to make sure the object is ready to be sent
14+
Invoke(nameof(ExecuteRemoteRpc), 5f);
15+
}
16+
17+
private void ExecuteRemoteRpc()
18+
{
19+
// Long version of IsServer
20+
if (Target != null && NetworkingManager.Singleton.IsClient)
21+
{
22+
int rnd = Random.Range(0, int.MaxValue);
23+
24+
Debug.LogFormat("Pining server with number {0}", rnd);
25+
26+
// Note that the InvokeServerRPC has to be called on the same behaviour as the actual RPC method
27+
Target.InvokeServerRpc(Target.PingServer, rnd);
28+
}
29+
}
30+
}
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using MLAPI;
2+
using MLAPI.NetworkedVar;
3+
4+
namespace MLAPI_Examples
5+
{
6+
public class SerializedNetworkedVarExample : NetworkedBehaviour
7+
{
8+
// Default settings, default value
9+
public NetworkedVarInt SerializedNetworkedVarInt = new NetworkedVarInt();
10+
// Default settings, initialization value 5
11+
public NetworkedVarInt SerializedNetworkedVarIntValue = new NetworkedVarInt(5);
12+
// Custom settings
13+
public NetworkedVarInt SerializedNetworkedVarIntSettings = new NetworkedVarInt(new NetworkedVarSettings()
14+
{
15+
SendChannel = "MySendChannel", // The var value will be synced over this channel
16+
ReadPermission = NetworkedVarPermission.Everyone, // The var values will be synced to everyone
17+
ReadPermissionCallback = null, // Only used when using "Custom" read permission
18+
SendTickrate = 2, // The var will sync no more than 2 times per second
19+
WritePermission = NetworkedVarPermission.OwnerOnly, // Only the owner of this object is allowed to change the value
20+
WritePermissionCallback = null // Only used when write permission is "Custom"
21+
});
22+
}
23+
}

0 commit comments

Comments
 (0)