SceneDrawer.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace Mirror
  4. {
  5. [CustomPropertyDrawer(typeof(SceneAttribute))]
  6. public class SceneDrawer : PropertyDrawer
  7. {
  8. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  9. {
  10. if (property.propertyType == SerializedPropertyType.String)
  11. {
  12. SceneAsset sceneObject = AssetDatabase.LoadAssetAtPath<SceneAsset>(property.stringValue);
  13. if (sceneObject == null && !string.IsNullOrWhiteSpace(property.stringValue))
  14. {
  15. // try to load it from the build settings for legacy compatibility
  16. sceneObject = GetBuildSettingsSceneObject(property.stringValue);
  17. }
  18. if (sceneObject == null && !string.IsNullOrWhiteSpace(property.stringValue))
  19. {
  20. Debug.LogError($"Could not find scene {property.stringValue} in {property.propertyPath}, assign the proper scenes in your NetworkManager");
  21. }
  22. SceneAsset scene = (SceneAsset)EditorGUI.ObjectField(position, label, sceneObject, typeof(SceneAsset), true);
  23. property.stringValue = AssetDatabase.GetAssetPath(scene);
  24. }
  25. else
  26. {
  27. EditorGUI.LabelField(position, label.text, "Use [Scene] with strings.");
  28. }
  29. }
  30. protected SceneAsset GetBuildSettingsSceneObject(string sceneName)
  31. {
  32. foreach (EditorBuildSettingsScene buildScene in EditorBuildSettings.scenes)
  33. {
  34. SceneAsset sceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(buildScene.path);
  35. if (sceneAsset!= null && sceneAsset.name == sceneName)
  36. {
  37. return sceneAsset;
  38. }
  39. }
  40. return null;
  41. }
  42. }
  43. }