Last active
June 9, 2016 10:55
-
-
Save kankikuchi/60a984ee111ff1c518a8 to your computer and use it in GitHub Desktop.
シーン名とシーン番号を定数で管理するクラスを作成しつつシーン名とシーン番号を関連付けたSceneDataを書き出すスクリプト【Unity】
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Text; | |
using UnityEditor; | |
using UnityEditorInternal; | |
using UnityEngine; | |
using System.Collections.Generic; | |
using System.Linq; | |
/// <summary> | |
/// シーン名とシーン番号を定数で管理するクラスを作成しつつシーン名とシーン番号を関連付けたSceneDataを書き出すスクリプト | |
/// </summary> | |
public class SceneDataClassCreator : AssetPostprocessor{ | |
private const string COMMAND_NAME = "Tools/Create/Scene Data"; // コマンド名 | |
//スクリプトを作成します | |
[MenuItem(COMMAND_NAME)] | |
private static void Create() | |
{ | |
Dictionary<string, string> scenesNameDic = new Dictionary<string, string>(); | |
Dictionary<string, int> scenesNoDic = new Dictionary<string, int>(); | |
for(int i = 0; i < EditorBuildSettings.scenes.Count(); i++){ | |
string sceneName = Path.GetFileNameWithoutExtension (EditorBuildSettings.scenes [i].path); | |
scenesNameDic [sceneName] = sceneName; | |
scenesNoDic [sceneName] = i; | |
} | |
CreateSceneData (scenesNoDic); | |
ConstantsClassCreator.Create ("SceneName", "シーン名を定数で管理するクラス", scenesNameDic); | |
ConstantsClassCreator.Create ("SceneNo" , "シーン番号を定数で管理するクラス", scenesNoDic); | |
} | |
//シーン名とシーン番号を関連付けたSceneDataを書き出す | |
private static void CreateSceneData(Dictionary<string, int> scenesNoDic){ | |
//"Assets/Resources/" + "SceneData" + ".asset" | |
string dataPath = DirectoryPath.RESOURCES + FilePath.SCENE_DATA + Extension.ASSET; | |
//SceneDataを新規作成、既にあるものは削除 | |
SceneData data = ScriptableObject.CreateInstance<SceneData> (); | |
AssetDatabase.DeleteAsset (dataPath); | |
AssetDatabase.CreateAsset ((ScriptableObject)data, dataPath); | |
data.hideFlags = HideFlags.NotEditable; | |
foreach (KeyValuePair<string, int> valuePair in scenesNoDic) { | |
SceneData.Param param = new SceneData.Param (); | |
param.Name = valuePair.Key; | |
param.No = valuePair.Value; | |
data.list.Add (param); | |
} | |
//変更を反映 | |
ScriptableObject obj = AssetDatabase.LoadAssetAtPath (dataPath, typeof(ScriptableObject)) as ScriptableObject; | |
EditorUtility.SetDirty (obj); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class SceneData : ScriptableObject | |
{ | |
public List<Param> list = new List<Param> (); | |
[System.SerializableAttribute] | |
public class Param | |
{ | |
public int No; | |
public string Name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class SceneManager : SingletonMonoBehaviour<SceneManager> { | |
/* 諸々省略 */ | |
//シーン名と番号が紐づいたデータ | |
private SceneData _sceneData; | |
//================================================================================= | |
//初期化 | |
//================================================================================= | |
private void Awake (){ | |
if (this != Instance) { | |
Destroy (this); | |
return; | |
} | |
DontDestroyOnLoad (gameObject); | |
//シーン情報読み込み | |
_sceneData = Resources.Load<SceneData> (FilePath.SCENE_DATA); | |
} | |
/* 諸々省略 */ | |
//================================================================================= | |
//シーン番号と名前の変換 | |
//================================================================================= | |
/// <summary> | |
/// シーンの数を取得 | |
/// </summary> | |
public int GetSceneNum(){ | |
return _sceneData.list.Count; | |
} | |
/// <summary> | |
/// シーン名を入力して、シーンの番号を取得 | |
/// </summary> | |
public string SceneNoToSceneName(int SceneNo){ | |
string SceneName = ""; | |
for(int i = 0; i < _sceneData.list.Count; i++){ | |
if(_sceneData.list[i].No == SceneNo){ | |
SceneName = _sceneData.list [i].Name; | |
break; | |
} | |
} | |
return SceneName; | |
} | |
/// <summary> | |
/// シーン番号を入力して、シーン名を取得 | |
/// </summary> | |
public int SceneNameToSceneNo(string SceneName){ | |
int SceneNo = 0; | |
for(int i = 0; i < _sceneData.list.Count; i++){ | |
if(_sceneData.list[i].Name == SceneName){ | |
SceneNo = _sceneData.list [i].No; | |
break; | |
} | |
} | |
return SceneNo; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment