Skip to content

Commit

Permalink
feat: Button to register all prefabs in NetworkClient (#179)
Browse files Browse the repository at this point in the history
* feat: Button to register all prefabs in NetworkClient

* Fix smells

* Method can be private

* Make sure user prefabs are still there

* fix tests
  • Loading branch information
paulpach committed Apr 14, 2020
1 parent 46cd32b commit 9f5f0b2
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
53 changes: 53 additions & 0 deletions Assets/Mirror/Editor/NetworkClientInspector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;


namespace Mirror
{
[CustomEditor(typeof(NetworkClient), true)]
[CanEditMultipleObjects]
public class NetworkClientInspector : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();

if (GUILayout.Button("Register All Prefabs"))
{
RegisterPrefabs((NetworkClient)target);
Undo.RecordObject(target, "Changed Area Of Effect");
}
}

public void RegisterPrefabs(NetworkClient gameObject)
{
ISet<GameObject> prefabs = LoadPrefabsContaining<NetworkIdentity>("Assets");

foreach (var existing in gameObject.spawnPrefabs)
{
prefabs.Add(existing);
}
gameObject.spawnPrefabs.Clear();
gameObject.spawnPrefabs.AddRange(prefabs);
}

private static ISet<GameObject> LoadPrefabsContaining<T>(string path) where T : UnityEngine.Component
{
var result = new HashSet<GameObject>();

var guids = AssetDatabase.FindAssets("t:Object", new[] { path });

foreach (var guid in guids)
{
var assetPath = AssetDatabase.GUIDToAssetPath(guid);

T obj = AssetDatabase.LoadAssetAtPath<T>(assetPath);

if (obj != null)
result.Add(obj.gameObject);
}
return result;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Mirror/Editor/NetworkClientInspector.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Assets/Mirror/Tests/Editor/Mirror.Tests.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"MirrorNG",
"Mirror.Tests.Common"
"Mirror.Tests.Common",
"Mirror.Editor"
],
"includePlatforms": [
"Editor"
Expand Down
50 changes: 50 additions & 0 deletions Assets/Mirror/Tests/Editor/NetworkClientInspectorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;

namespace Mirror.Tests
{
[TestFixture(Category = "NetworkClient")]
public class NetworkClientInspectorTest
{

[Test]
public void RegisterPrefabs()
{
var gameObject = new GameObject("NetworkClient", typeof(NetworkClient));

NetworkClient client = gameObject.GetComponent<NetworkClient>();

NetworkClientInspector inspector = ScriptableObject.CreateInstance<NetworkClientInspector>();
inspector.RegisterPrefabs(client);

Assert.That(client.spawnPrefabs, Has.Count.GreaterThan(13));

foreach (var prefab in client.spawnPrefabs)
{
Assert.That(prefab.GetComponent<NetworkIdentity>(), Is.Not.Null);
}
GameObject.DestroyImmediate(gameObject);
}

[Test]
public void PreserveExisting()
{
var preexisting = new GameObject("object", typeof(NetworkIdentity));

var gameObject = new GameObject("NetworkClient", typeof(NetworkClient));
NetworkClient client = gameObject.GetComponent<NetworkClient>();
client.spawnPrefabs.Add(preexisting);

NetworkClientInspector inspector = ScriptableObject.CreateInstance<NetworkClientInspector>();

inspector.RegisterPrefabs(client);

Assert.That(client.spawnPrefabs, Contains.Item(preexisting));

GameObject.DestroyImmediate(gameObject);
GameObject.DestroyImmediate(preexisting);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Mirror/Tests/Editor/NetworkClientInspectorTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9f5f0b2

Please sign in to comment.