- Add
TransportPickerAttribute
attribute so popup doesn't apply to all Transport fields, just the one with attribute - if Transport is already on gameobject, set
property.objectReferenceValue
to that one instead of adding new component
Created
August 11, 2023 13:54
-
-
Save James-Frowen/2a1feecea3c3620ddd372a7ee8faceaa to your computer and use it in GitHub Desktop.
popup to add transports to Mirror's networkmanager
This file contains hidden or 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
[CustomPropertyDrawer(typeof(Transport))] | |
public class NetworkTransportDrawer: PropertyDrawer | |
{ | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
var fieldRect = position; | |
var buttonRect = position; | |
fieldRect.width -= 50; | |
buttonRect.xMin = fieldRect.xMax; | |
buttonRect.width = 50; | |
EditorGUI.PropertyField(fieldRect, property, label); | |
var options = FindAllTransports(); | |
var strings = options.Select(x => x.Name).Prepend("Add").ToArray(); | |
var index = EditorGUI.Popup(buttonRect, 0, strings); | |
Debug.Log($"NetworkTransportDrawer {index}"); | |
if (index > 0) | |
{ | |
var target = (MonoBehaviour)property.serializedObject.targetObject; | |
// -1 because 0 is "Add" | |
var transport = (Transport)Undo.AddComponent(target.gameObject, options[index-1]); | |
property.objectReferenceValue = transport; | |
property.serializedObject.ApplyModifiedProperties(); | |
} | |
} | |
static Type[] Transports; | |
private Type[] FindAllTransports() | |
{ | |
if (Transports != null) | |
return Transports; | |
var transportType = typeof(Transport); | |
Transports = AppDomain.CurrentDomain.GetAssemblies() | |
.SelectMany(assembly => assembly.GetTypes()) | |
.Where(type => transportType.IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract) | |
.ToArray(); | |
return Transports; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment