I want to fill scene in LevelSO by selecting scene or drag/drop into the field
- User
I want to fill scene in LevelSO by selecting scene or drag/drop into the field
Assistant
To allow filling the
sceneNamefield inLevelSOby either selecting a scene from a dropdown or dragging/dropping a scene asset directly into the field, we can use Unity's Scene Asset type and custom property drawers. Here's how to implement this:1. Update
LevelSOto UseSceneAssetFirst, modify the
LevelSOscript to useSceneAssetinstead of a plain string for the scene reference:// LevelSO.cs using UnityEngine; using UnityEngine.SceneManagement; [CreateAssetMenu(fileName = "Level_", menuName = "Level System/Level Data")] public class LevelSO : ScriptableObject, ILevelInfo { [SerializeField] private string levelId; [SerializeField] private string displayName; [SerializeField] private SceneAsset sceneAsset; // Use SceneAsset instead of string [SerializeField] private LevelDifficulty difficulty; public string LevelId => levelId; public string DisplayName => displayName; public string SceneName => sceneAsset != null ? sceneAsset.name : ""; // Convert SceneAsset to scene name public enum LevelDifficulty { Easy, Medium, Hard } }2. Create a Custom Property Drawer for
SceneAssetTo ensure the
SceneAssetfield is displayed correctly in the Inspector, Unity already provides a default property drawer forSceneAsset. However, if you want to customize it further or ensure compatibility, you can create a simple editor script:// SceneAssetDrawer.cs #if UNITY_EDITOR using UnityEditor; using UnityEngine; [CustomPropertyDrawer(typeof(SceneAsset))] public class SceneAssetDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(position, label, property); // Draw the default SceneAsset selector EditorGUI.BeginChangeCheck(); var sceneAsset = EditorGUI.ObjectField(position, label, property.objectReferenceValue, typeof(SceneAsset), false) as SceneAsset; if (EditorGUI.EndChangeCheck() && sceneAsset != property.objectReferenceValue) { property.objectReferenceValue = sceneAsset; } EditorGUI.EndProperty(); } } #endifHowever, since Unity already handles
SceneAssetcorrectly in the Inspector, the above script is optional. The default behavior will allow you to:- Drag and Drop: Drag a scene asset from the Project window directly into the
Scene Assetfield. - Select from Dropdown: Click the circle to the right of the field to select a scene from the list of available scenes in your project.
3. Inspector Usage
With these changes, the
LevelSOInspector will now display aScene Assetfield where you can:- Drag and drop a
.unityscene file from the Project window. - Use the object picker to select a scene from the list of available scenes in your project.
4. Accessing the Scene Name
The
SceneNameproperty inLevelSOconverts theSceneAssetto a scene name string, which can be used withSceneManagerfor loading:// Example usage in LevelLoader.cs public class LevelLoader : MonoBehaviour, ILevelLoader { public void LoadLevel(string levelId) { var levelData = GetLevelData(levelId); if (levelData == null) return; // Load the scene using the scene name from SceneAsset SceneManager.LoadSceneAsync(levelData.SceneName, LoadSceneMode.Additive); } // ... other methods }Final Notes:
- Scene Asset:
SceneAssetis a built-in Unity type that represents a scene file. It automatically handles the correct file type and provides a user-friendly interface in the Inspector. - Scene Name: The
SceneNameproperty extracts the scene name from theSceneAsset(e.g., "MyScene" from "Assets/Scenes/MyScene.unity").
This approach ensures a clean and user-friendly way to manage scene references in your level data Scriptable Objects.
- Drag and Drop: Drag a scene asset from the Project window directly into the
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.