Sceneセレクタ
code:SceneSelector.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class SceneSelector : MonoBehaviour {
public GameObject buttonPrefab;
public GameObject panel;
public Vector3 startPosition = Vector3.zero;
private List<GameObject> sceneButtonList = new List<GameObject>();
// Use this for initialization
void Start () {
if (panel == null){
Debug.LogError("canvas not defined!");
return;
}
if (buttonPrefab == null){
Debug.LogError("butonPrefab not defined!");
return;
}
var current = SceneManager.GetActiveScene();
//var cnt = SceneManager.sceneCount;
var cnt = SceneManager.sceneCountInBuildSettings;
Debug.Log("scene count:" + cnt.ToString());
for (int i = 0; i < cnt; i++)
{
Scene scene = SceneManager.GetSceneByBuildIndex(i);
Debug.Log("scene:" + scene.name);
Debug.Log(" path:" + scene.path);
Debug.Log(scene.ToString());
if (scene != current){
var pos = startPosition + new Vector3(0, -30 * i, 0);
var worldPos = panel.transform.TransformPoint(pos);
var btn = Instantiate(buttonPrefab, worldPos, Quaternion.identity, panel.transform);
btn.GetComponentsInChildren<Text>()0.text = "Scene" + i.ToString() + ":" + scene.name; //local capture
var bi = i;
btn.GetComponent<Button>().onClick.AddListener(() =>
{
SceneManager.LoadScene(bi);
Debug.Log("loaded:" + scene.name);
Debug.Log("active:" + SceneManager.GetActiveScene().name);
});
}
}
}
}