Skip to content

Commit 045e458

Browse files
committed
Added EditorScripts for reference purpose and modified the NEtworkingManager to use the inpsector more
1 parent 7054220 commit 045e458

16 files changed

+691
-28
lines changed

MLAPI-Editor/GithubAsset.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System;
2+
3+
[Serializable]
4+
public class GithubAsset
5+
{
6+
public string browser_download_url;
7+
public string name;
8+
}

MLAPI-Editor/GithubRelease.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
[Serializable]
4+
public class GithubRelease
5+
{
6+
public string html_url;
7+
public string tag_name;
8+
public string name;
9+
public string body;
10+
public string published_at;
11+
public bool prerelease;
12+
public GithubAsset[] assets;
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System;
2+
3+
[Serializable]
4+
public class GithubAsset
5+
{
6+
public string browser_download_url;
7+
public string name;
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
[Serializable]
4+
public class GithubRelease
5+
{
6+
public string html_url;
7+
public string tag_name;
8+
public string name;
9+
public string body;
10+
public string published_at;
11+
public bool prerelease;
12+
public GithubAsset[] assets;
13+
}

MLAPI-Editor/MLAPI-Editor.csproj

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" 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>{A45DBD43-D640-4562-9F24-6745269CEDF7}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>MLAPI_Editor</RootNamespace>
11+
<AssemblyName>MLAPI-Editor</AssemblyName>
12+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="System" />
34+
<Reference Include="System.Core" />
35+
<Reference Include="System.Xml.Linq" />
36+
<Reference Include="System.Data.DataSetExtensions" />
37+
<Reference Include="Microsoft.CSharp" />
38+
<Reference Include="System.Data" />
39+
<Reference Include="System.Net.Http" />
40+
<Reference Include="System.Xml" />
41+
<Reference Include="UnityEditor">
42+
<HintPath>..\..\..\..\..\Program Files\Unity\Editor\Data\Managed\UnityEditor.dll</HintPath>
43+
</Reference>
44+
<Reference Include="UnityEngine">
45+
<HintPath>..\..\..\..\..\Program Files\Unity\Editor\Data\Managed\UnityEngine.dll</HintPath>
46+
</Reference>
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="GithubAsset.cs" />
50+
<Compile Include="GithubRelease.cs" />
51+
<Compile Include="MLAPIEditor.cs" />
52+
<Compile Include="NetworkedAnimatorEditor.cs" />
53+
<Compile Include="NetworkedBehaviourEditor.cs" />
54+
<Compile Include="NetworkedObjectEditor.cs" />
55+
<Compile Include="NetworkingManagerEditor.cs" />
56+
<Compile Include="Properties\AssemblyInfo.cs" />
57+
<Compile Include="TrackedObjectEditor.cs" />
58+
</ItemGroup>
59+
<ItemGroup>
60+
<ProjectReference Include="..\MLAPI\MLAPI.csproj">
61+
<Project>{EE431720-A9ED-43DC-9E74-10B693816D38}</Project>
62+
<Name>MLAPI</Name>
63+
</ProjectReference>
64+
</ItemGroup>
65+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
66+
</Project>

MLAPI-Editor/MLAPIEditor.cs

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using UnityEditor;
5+
using UnityEngine;
6+
7+
public class MLAPIEditor : EditorWindow
8+
{
9+
private GithubRelease[] releases = new GithubRelease[0];
10+
private bool[] foldoutStatus = new bool[0];
11+
private long lastUpdated = 0;
12+
private string currentVersion;
13+
14+
[MenuItem("Window/MLAPI")]
15+
public static void ShowWindow()
16+
{
17+
GetWindow<MLAPIEditor>();
18+
}
19+
20+
private void Init()
21+
{
22+
lastUpdated = 0;
23+
24+
if (EditorPrefs.HasKey("MLAPI_version"))
25+
currentVersion = EditorPrefs.GetString("MLAPI_version");
26+
else
27+
currentVersion = "None";
28+
}
29+
30+
private void Awake()
31+
{
32+
Init();
33+
}
34+
35+
private void OnFocus()
36+
{
37+
Init();
38+
}
39+
40+
private void OnGUI()
41+
{
42+
if(foldoutStatus != null)
43+
{
44+
for (int i = 0; i < foldoutStatus.Length; i++)
45+
{
46+
if (releases[i] == null)
47+
continue;
48+
foldoutStatus[i] = EditorGUILayout.Foldout(foldoutStatus[i], releases[i].tag_name + " - " + releases[i].name);
49+
if (foldoutStatus[i])
50+
{
51+
EditorGUI.indentLevel++;
52+
EditorGUILayout.LabelField("Release notes", EditorStyles.boldLabel);
53+
EditorGUILayout.LabelField(releases[i].body, EditorStyles.wordWrappedLabel);
54+
EditorGUILayout.Space();
55+
EditorGUILayout.Space();
56+
if (releases[i].prerelease)
57+
{
58+
GUIStyle style = new GUIStyle(EditorStyles.boldLabel);
59+
style.normal.textColor = new Color(1f, 0.5f, 0f);
60+
EditorGUILayout.LabelField("Pre-release", style);
61+
}
62+
else
63+
{
64+
GUIStyle style = new GUIStyle(EditorStyles.boldLabel);
65+
style.normal.textColor = new Color(0f, 1f, 0f);
66+
EditorGUILayout.LabelField("Stable-release", style);
67+
}
68+
if (currentVersion == releases[i].tag_name)
69+
{
70+
GUIStyle boldStyle = new GUIStyle(EditorStyles.boldLabel);
71+
boldStyle.normal.textColor = new Color(0.3f, 1f, 0.3f);
72+
EditorGUILayout.LabelField("Installed", boldStyle);
73+
}
74+
EditorGUILayout.LabelField("Release date: " + DateTime.Parse(DateTime.Parse(releases[i].published_at).ToString()), EditorStyles.miniBoldLabel);
75+
76+
if(currentVersion != releases[i].tag_name && GUILayout.Button("Install"))
77+
InstallRelease(i);
78+
79+
EditorGUI.indentLevel--;
80+
}
81+
}
82+
}
83+
84+
GUILayout.BeginArea(new Rect(5, position.height - 20, position.width, 20));
85+
86+
if (GUILayout.Button("Check for updates"))
87+
GetReleases();
88+
89+
GUILayout.EndArea();
90+
91+
string lastUpdatedString = lastUpdated == 0 ? "Never" : new DateTime(lastUpdated).ToShortTimeString();
92+
EditorGUI.LabelField(new Rect(5, position.height - 40, position.width, 20), "Last checked: " + lastUpdatedString, EditorStyles.centeredGreyMiniLabel);
93+
94+
if ((DateTime.Now - new DateTime(lastUpdated)).Seconds > 3600)
95+
GetReleases();
96+
97+
Repaint();
98+
}
99+
100+
private void InstallRelease(int index)
101+
{
102+
for (int i = 0; i < releases[index].assets.Length; i++)
103+
{
104+
WWW www = new WWW(releases[index].assets[i].browser_download_url);
105+
while (!www.isDone && string.IsNullOrEmpty(www.error))
106+
{
107+
EditorGUI.ProgressBar(new Rect(5, position.height - 60, position.width, 20), www.progress, "Installing " + i + "/" + releases[index].assets.Length);
108+
}
109+
110+
if(!Directory.Exists(Application.dataPath + "/MLAPI/Lib/"))
111+
Directory.CreateDirectory(Application.dataPath + "/MLAPI/Lib/");
112+
113+
File.WriteAllBytes(Application.dataPath + "/MLAPI/Lib/" + releases[index].assets[i].name, www.bytes);
114+
115+
if (releases[index].assets[i].name.EndsWith(".unitypackage"))
116+
AssetDatabase.ImportPackage(Application.dataPath + "/MLAPI/Lib/" + releases[index].assets[i].name, true);
117+
}
118+
119+
EditorPrefs.SetString("MLAPI_version", releases[index].tag_name);
120+
currentVersion = releases[index].tag_name;
121+
AssetDatabase.Refresh();
122+
}
123+
124+
private void GetReleases()
125+
{
126+
lastUpdated = DateTime.Now.Ticks;
127+
128+
WWW www = new WWW("https://api.github.com/repos/TwoTenPvP/MLAPI/releases");
129+
while(!www.isDone && string.IsNullOrEmpty(www.error))
130+
{
131+
EditorGUI.ProgressBar(new Rect(5, position.height - 60, position.width, 20), www.progress, "Fetching...");
132+
}
133+
string json = www.text;
134+
135+
//This makes it from a json array to the individual objects in the array.
136+
//The JSON serializer cant take arrays. We have to split it up outselves.
137+
List<string> releasesJson = new List<string>();
138+
int depth = 0;
139+
string currentObject = "";
140+
for (int i = 1; i < json.Length - 1; i++)
141+
{
142+
if (json[i] == '[')
143+
depth++;
144+
else if (json[i] == ']')
145+
depth--;
146+
else if (json[i] == '{')
147+
depth++;
148+
else if (json[i] == '}')
149+
depth--;
150+
151+
if ((depth == 0 && json[i] != ',') || depth > 0)
152+
currentObject += json[i];
153+
154+
if (depth == 0 && json[i] == ',')
155+
{
156+
releasesJson.Add(currentObject);
157+
currentObject = "";
158+
}
159+
}
160+
161+
releases = new GithubRelease[releasesJson.Count];
162+
foldoutStatus = new bool[releasesJson.Count];
163+
164+
for (int i = 0; i < releasesJson.Count; i++)
165+
{
166+
releases[i] = JsonUtility.FromJson<GithubRelease>(releasesJson[i]);
167+
if (i == 0)
168+
foldoutStatus[i] = true;
169+
else
170+
foldoutStatus[i] = false;
171+
}
172+
}
173+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using MLAPI.MonoBehaviours.Prototyping;
2+
using System;
3+
using UnityEditor.Animations;
4+
using UnityEngine;
5+
6+
namespace UnityEditor
7+
{
8+
[CustomEditor(typeof(NetworkedAnimator), true)]
9+
[CanEditMultipleObjects]
10+
public class NetworkAnimatorEditor : Editor
11+
{
12+
private NetworkedAnimator networkedAnimatorTarget;
13+
[NonSerialized]
14+
private bool initialized;
15+
16+
private SerializedProperty animatorProperty;
17+
private GUIContent animatorLabel;
18+
19+
void Init()
20+
{
21+
if (initialized)
22+
return;
23+
24+
initialized = true;
25+
networkedAnimatorTarget = target as NetworkedAnimator;
26+
27+
animatorProperty = serializedObject.FindProperty("_animator");
28+
animatorLabel = new GUIContent("Animator", "The Animator component to synchronize.");
29+
}
30+
31+
public override void OnInspectorGUI()
32+
{
33+
Init();
34+
serializedObject.Update();
35+
DrawControls();
36+
serializedObject.ApplyModifiedProperties();
37+
}
38+
39+
void DrawControls()
40+
{
41+
EditorGUI.BeginChangeCheck();
42+
EditorGUILayout.PropertyField(animatorProperty, animatorLabel);
43+
44+
if (EditorGUI.EndChangeCheck())
45+
networkedAnimatorTarget.ResetParameterOptions();
46+
47+
if (networkedAnimatorTarget.animator == null)
48+
return;
49+
50+
var controller = networkedAnimatorTarget.animator.runtimeAnimatorController as AnimatorController;
51+
if (controller != null)
52+
{
53+
var showWarning = false;
54+
EditorGUI.indentLevel += 1;
55+
int i = 0;
56+
57+
foreach (var p in controller.parameters)
58+
{
59+
if (i >= 32)
60+
{
61+
showWarning = true;
62+
break;
63+
}
64+
65+
bool oldSend = networkedAnimatorTarget.GetParameterAutoSend(i);
66+
bool send = EditorGUILayout.Toggle(p.name, oldSend);
67+
if (send != oldSend)
68+
{
69+
networkedAnimatorTarget.SetParameterAutoSend(i, send);
70+
EditorUtility.SetDirty(target);
71+
}
72+
i += 1;
73+
}
74+
75+
if (showWarning)
76+
EditorGUILayout.HelpBox("NetworkAnimator can only select between the first 32 parameters in a mecanim controller", MessageType.Warning);
77+
78+
EditorGUI.indentLevel -= 1;
79+
}
80+
81+
if (Application.isPlaying)
82+
{
83+
EditorGUILayout.Separator();
84+
if (networkedAnimatorTarget.param0 != "")
85+
EditorGUILayout.LabelField("Param 0", networkedAnimatorTarget.param0);
86+
if (networkedAnimatorTarget.param1 != "")
87+
EditorGUILayout.LabelField("Param 1", networkedAnimatorTarget.param1);
88+
if (networkedAnimatorTarget.param2 != "")
89+
EditorGUILayout.LabelField("Param 2", networkedAnimatorTarget.param2);
90+
if (networkedAnimatorTarget.param3 != "")
91+
EditorGUILayout.LabelField("Param 3", networkedAnimatorTarget.param3);
92+
if (networkedAnimatorTarget.param4 != "")
93+
EditorGUILayout.LabelField("Param 4", networkedAnimatorTarget.param4);
94+
}
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)