UnityEngine.SceneManagement.SceneUtility.GetBuildIndexByScenePath C# (CSharp) Method

GetBuildIndexByScenePath() private method

private GetBuildIndexByScenePath ( string scenePath ) : int
scenePath string
return int
        public static extern int GetBuildIndexByScenePath(string scenePath);
        /// <summary>

Usage Example

コード例 #1
0
        void OnGUI()
        {
            if (projectList == null)
            {
                projectList = new ReorderableList(scenesInProject, typeof(SceneInfo), false, false, false, false);
                projectList.drawHeaderCallback =
                    (Rect rect) =>
                {
                    EditorGUI.LabelField(rect, "Scenes In Project", EditorStyles.boldLabel);
                };
                projectList.drawElementCallback =
                    (Rect rect, int index, bool isActive, bool isFocused) =>
                {
                    var sceneInfo = scenesInProject[index];
                    EditorGUI.LabelField(new Rect(rect.x, rect.y, rect.width - 60, rect.height), sceneInfo.name);
                    if (GUI.Button(new Rect(rect.width - 60, rect.y, 20, rect.height), "P", GUIStyle.none))
                    {
                        EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath <SceneAsset>(sceneInfo.path));
                    }
                    if (GUI.Button(new Rect(rect.width - 40, rect.y, 20, rect.height), "O", GUIStyle.none))
                    {
                        List <SceneInfo> scenes = this.GetScenesInHierarchy();                               //得到 Hierarchy 中的 已加载的 场景
                        int sceneIndex          = scenes.FindIndex(v => v.path == sceneInfo.path);           //确认当前请求添加的场景 的状态
                        if (sceneIndex == -1)
                        {
                            bool savemodified = EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
                            if (savemodified) //After the user responds positively (with or without saving), open the scene he specified.
                            {
                                int switchIndex = EditorUtility.DisplayDialogComplex("OpenSceneMode", "Addition:Add scene and keep the loaded scene\nSingle:Will Remove all the loaded scene of hierarchy", "Single", "Cancel", "Addition");
                                switch (switchIndex)
                                {
                                case 0:
                                    EditorSceneManager.OpenScene(sceneInfo.path);
                                    break;

                                case 2:
                                    EditorSceneManager.OpenScene(sceneInfo.path, OpenSceneMode.Additive);
                                    break;

                                case 1:
                                default:
                                    this.ShowNotification(new GUIContent("Aborting!"));
                                    break;
                                }
                            }
                            else
                            {
                                this.ShowNotification(new GUIContent("Aborting!"));
                            }
                        }
                        else                          //If it is stay in the hierarchy,no matter whether it is load or unload, ping this scene
                        {
                            EditorGUIUtility.PingObject(scenes[sceneIndex].instanceID);
                            this.ShowNotification(new GUIContent("This scene already in the hierarchy!"));
                        }
                    }
                    if (GUI.Button(new Rect(rect.width - 20, rect.y, 20, rect.height), "A", GUIStyle.none))
                    {
                        int sceneIndex = SceneUtility.GetBuildIndexByScenePath(sceneInfo.path);
                        if (sceneIndex == -1)
                        {
                            var tmpSceneIn = EditorBuildSettings.scenes.ToList();
                            tmpSceneIn.Add(new EditorBuildSettingsScene(sceneInfo.path, true));
                            EditorBuildSettings.scenes = tmpSceneIn.ToArray();
                            ADB.Refresh();
                        }
                        else
                        {
                            this.ShowNotification(new GUIContent("This scene already in the buildlist!"));
                        }
                    }
                };
            }
            if (settingList == null)
            {
                settingList = new ReorderableList(scenesInSettings, typeof(SceneInfo), true, false, false, true);
                settingList.onReorderCallback =
                    (ReorderableList list) =>
                {
                    EditorBuildSettings.scenes =
                        list.list.Cast <SceneInfo>()
                        .Select(scene =>
                    {
                        var editorScene     = new EditorBuildSettingsScene();
                        editorScene.enabled = scene.enabledInSettings;
                        editorScene.path    = scene.path;
                        return(editorScene);
                    })
                        .ToArray();
                };
                settingList.drawHeaderCallback =
                    (Rect rect) =>
                {
                    EditorGUI.LabelField(rect, "Scenes In Setting", EditorStyles.boldLabel);
                };
                settingList.drawElementCallback =
                    (Rect rect, int index, bool isActive, bool isFocused) =>
                {
                    var sceneInfo = scenesInSettings[index];
                    sceneInfo.enabledInSettings = GUI.Toggle(new Rect(rect.x, rect.y, 14, rect.height), sceneInfo.enabledInSettings, string.Empty);
                    var tmpScenesInSetting = EditorBuildSettings.scenes.ToList();
                    tmpScenesInSetting.Where(scene => scene.path == sceneInfo.path).First().enabled = sceneInfo.enabledInSettings;
                    EditorGUI.LabelField(new Rect(rect.x + 14, rect.y, rect.width - 34, rect.height), sceneInfo.name);
                    EditorGUI.LabelField(new Rect(rect.width - 8, rect.y, 20, rect.height), index.ToString());

                    if (GUI.Button(new Rect(rect.width - 65, rect.y, 20, rect.height), "PH", GUIStyle.none))
                    {
                        int id = this.GetInstanceId(sceneInfo);
                        if (id != -1)
                        {
                            RemoveNotification();
                            EditorGUIUtility.PingObject(id);
                        }
                        else
                        {
                            ShowNotification(new GUIContent("The scene you pinged is not in hierarchy!"));
                        }
                    }
                    if (GUI.Button(new Rect(rect.width - 30, rect.y, 20, rect.height), "PP", GUIStyle.none))
                    {
                        var assetObj = ADB.LoadAssetAtPath <SceneAsset>(sceneInfo.path);
                        if (null != assetObj)
                        {
                            RemoveNotification();
                            EditorGUIUtility.PingObject(assetObj);
                        }
                    }
                };
                settingList.onRemoveCallback = (ReorderableList list) =>
                {
                    if (EditorUtility.DisplayDialog("Attention:", "Really want to remove this scene from the BuildList?", "Yes", "No"))
                    {
                        var tempList = EditorBuildSettings.scenes.ToList();
                        tempList.RemoveAt(list.index);
                        tempList.TrimExcess();
                        EditorBuildSettings.scenes = tempList.ToArray();
                        ReorderableList.defaultBehaviours.DoRemoveButton(list);
                    }
                };
            }
            scenesInProject  = GetScenesInProject();
            scenesInSettings = GetScenesInSettings();

            projectList.list = scenesInProject;
            settingList.list = scenesInSettings;

            // settingScrollPosition = EditorGUILayout.BeginScrollView(settingScrollPosition);
            settingList.DoLayoutList();
            //EditorGUILayout.EndScrollView();

            projectScrollPosition = EditorGUILayout.BeginScrollView(projectScrollPosition);
            projectList.DoLayoutList();
            EditorGUILayout.EndScrollView();
        }