-
-
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: use scriptable object for spawnable prefabs (#1127)
squash of 3 commits: - added network prefabs scriptable object - add tests - renamed Drawers to Inspector
- Loading branch information
1 parent
d4bc727
commit 1973e76
Showing
14 changed files
with
358 additions
and
59 deletions.
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
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,88 @@ | ||
using System.Collections.Generic; | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace Mirage | ||
{ | ||
[CustomEditor(typeof(NetworkPrefabs))] | ||
public partial class NetworkPrefabsInspector : Editor | ||
{ | ||
private SerializedProperty prefabs; | ||
|
||
private void OnEnable() | ||
{ | ||
prefabs = serializedObject.FindProperty(nameof(NetworkPrefabs.Prefabs)); | ||
} | ||
|
||
public override void OnInspectorGUI() | ||
{ | ||
serializedObject.Update(); | ||
|
||
if (GUILayout.Button("Register All Prefabs")) | ||
{ | ||
RegisterPrefabs(); | ||
} | ||
|
||
EditorGUILayout.PropertyField(prefabs); | ||
|
||
serializedObject.ApplyModifiedProperties(); | ||
} | ||
|
||
public void RegisterPrefabs() | ||
{ | ||
var loadedPrefabs = LoadPrefabsContaining<NetworkIdentity>("Assets"); | ||
|
||
for (var i = 0; i < prefabs.arraySize; i++) | ||
{ | ||
var item = prefabs.GetArrayElementAtIndex(i).objectReferenceValue; | ||
|
||
if (item != null && item is NetworkIdentity identity) | ||
{ | ||
loadedPrefabs.Add(identity); | ||
} | ||
} | ||
|
||
prefabs.ClearArray(); | ||
prefabs.arraySize = loadedPrefabs.Count; | ||
|
||
var index = 0; | ||
foreach (var prefab in loadedPrefabs) | ||
{ | ||
prefabs.GetArrayElementAtIndex(index).objectReferenceValue = prefab; | ||
index++; | ||
} | ||
|
||
serializedObject.ApplyModifiedProperties(); | ||
} | ||
|
||
private static ISet<T> LoadPrefabsContaining<T>(string path) where T : Component | ||
{ | ||
var result = new HashSet<T>(); | ||
|
||
var guids = AssetDatabase.FindAssets("t:GameObject", new[] | ||
{ | ||
path | ||
}); | ||
|
||
for (var i = 0; i < guids.Length; i++) | ||
{ | ||
var assetPath = AssetDatabase.GUIDToAssetPath(guids[i]); | ||
|
||
var obj = AssetDatabase.LoadAssetAtPath<T>(assetPath); | ||
|
||
if (obj != null) | ||
{ | ||
result.Add(obj); | ||
} | ||
|
||
if (i % 100 == 99) | ||
{ | ||
EditorUtility.UnloadUnusedAssetsImmediate(); | ||
} | ||
} | ||
|
||
EditorUtility.UnloadUnusedAssetsImmediate(); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#if UNITY_2022_2_OR_NEWER | ||
using UnityEditor.UIElements; | ||
using UnityEngine.UIElements; | ||
|
||
namespace Mirage | ||
{ | ||
public partial class NetworkPrefabsInspector | ||
{ | ||
private ListView listView; | ||
|
||
public override VisualElement CreateInspectorGUI() | ||
{ | ||
var root = new VisualElement(); | ||
|
||
var button = new Button(() => | ||
{ | ||
serializedObject.Update(); | ||
RegisterPrefabs(); | ||
}) { text = "Register All Prefabs" }; | ||
|
||
root.Add(button); | ||
|
||
var prefabsField = new PropertyField(prefabs); | ||
|
||
prefabsField.RegisterCallback<GeometryChangedEvent, VisualElement>((evt, element) => | ||
{ | ||
if (listView == null) | ||
{ | ||
listView = element.Q<ListView>(); | ||
if (listView != null) | ||
{ | ||
listView.style.maxHeight = new StyleLength(new Length(100, LengthUnit.Percent)); | ||
} | ||
} | ||
}, prefabsField); | ||
|
||
root.Add(prefabsField); | ||
|
||
return root; | ||
} | ||
} | ||
} | ||
#endif |
3 changes: 3 additions & 0 deletions
3
Assets/Mirage/Editor/NetworkPrefabsInspectorUIToolkit.cs.meta
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,13 @@ | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
namespace Mirage | ||
{ | ||
/// <summary> | ||
/// A scriptable object that contains a list of prefabs that can be spawned on the network. | ||
/// </summary> | ||
public sealed class NetworkPrefabs : ScriptableObject | ||
{ | ||
public List<NetworkIdentity> Prefabs = new List<NetworkIdentity>(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.