Unity实现在Inspector面板中选择BuildSettings中已有的场景

实现效果

效果展示

代码

1、SceneNameDrawer.cs

using UnityEngine;
using UnityEditor;
using System.Linq;

public class SceneNameAttribute : PropertyAttribute {
    
     }

#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(SceneNameAttribute))]
public class SceneNameDrawer : PropertyDrawer
{
    
    
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
    
    
        // Get the list of scenes in Build Settings
        var scenes = EditorBuildSettings.scenes
            .Where(s => !string.IsNullOrEmpty(s.path))
            .Select(s => System.IO.Path.GetFileNameWithoutExtension(s.path))
            .ToArray();

        // Get the index of the currently selected scene
        var index = Mathf.Max(0, System.Array.IndexOf(scenes, property.stringValue));

        // Draw a popup to select the scene
        EditorGUI.BeginChangeCheck();
        index = EditorGUI.Popup(position, label.text, index, scenes);
        if (EditorGUI.EndChangeCheck())
        {
    
    
            property.stringValue = scenes[index];
        }
    }
}
#endif

2、Teleport.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Teleport : MonoBehaviour
{
    
    
    [SceneName]
    public string currentSceneName;  //当前场景名
    [SceneName]
    public string toGoSceneName;  //要去的场景名
}

猜你喜欢

转载自blog.csdn.net/qq_44887198/article/details/129240301