-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Button to register all prefabs in NetworkClient (#179)
* 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
Showing
5 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
Assets/Mirror/Tests/Editor/NetworkClientInspectorTest.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.