예제 #1
0
        /// <summary>
        /// シーンの作成
        /// </summary>
        /// <param name="path">作成先のファイルパス</param>
        static TemporaryFileData.Data TryBuildScene(string path, out bool success)
        {
            if (AssetChecker.Exists(path))
            {
                Debug.LogFormat("Exist:{0}", path);
                success = false;
                return(default(TemporaryFileData.Data));
            }

            // フォルダ作成
            string rootFolderPath = FolderBuilder.BuildFolderSet(path);

            if (string.IsNullOrEmpty(rootFolderPath))
            {
                success = false;
                return(default(TemporaryFileData.Data));
            }

            // シーンを開く
            var templateScene = DataLoader.LoadSceneTemplate();

            EditorSceneManager.OpenScene(AssetDatabase.GetAssetPath(templateScene), OpenSceneMode.Additive);

            #warning TODO: JSONを取得してScriptDependencyからスクリプトを作成する

            var rootFolderName = rootFolderPath.Split('/').Last();

            // シーンを保存
            var scenePath = string.Format("{0}/{1}.unity", rootFolderPath, rootFolderName);
            EditorSceneManager.SetActiveScene(EditorSceneManager.GetSceneByName(templateScene.name));
            EditorSceneManager.SaveScene(EditorSceneManager.GetSceneByName(templateScene.name), scenePath);

            // プログレスバー
            EditorUtility.DisplayProgressBar(string.Format("シーン\"{0}\"の作成中...", rootFolderName), "", 0f);

            // スクリプト作成
            var scriptDependency = DataLoader.LoadScriptDependency();
            var scripts          = ScriptBuilder.BuildScripts(rootFolderPath + "/" + Config.ScriptFolder, rootFolderName, scriptDependency);

            // コンパイル終了時の処理 設定
            var data = new TemporaryFileData.Data
            {
                SceneName  = rootFolderName,
                FolderPath = rootFolderPath,
                Scripts    = scripts.ToArray(),
            };

            success = true;
            return(data);
        }
예제 #2
0
        /// <summary>
        /// シーンの構築
        /// </summary>
        static void BuildScene(TemporaryFileData.Data temporaryData)
        {
            NamePostprocessor.Initialize(temporaryData.SceneName);

            var scenePath = string.Format("{0}/{1}.unity", temporaryData.FolderPath, temporaryData.SceneName);
            var scene     = EditorSceneManager.GetSceneByName(temporaryData.SceneName);

            EditorSceneManager.SetActiveScene(scene);

            // オブジェクトを作成
            var scriptDependency = DataLoader.LoadScriptDependency();
            var gameObjectDict   = GameObjectBuilder.BuildGameObjects(scriptDependency)
                                   .ToDictionary(gameObject => gameObject.name, g => g);

            var scriptDict    = temporaryData.Scripts.ToDictionary(script => script.RawComponentName, script => script);
            var componentDict = new Dictionary <string, Component>();

            // オブジェクトへスクリプトをアタッチ
            foreach (var dependencyData in scriptDependency.DataList)
            {
                var gameObject = gameObjectDict[dependencyData.RawGameObjectName];

                // dependencyに定義されているコンポーネントをアタッチ
                foreach (var componentData in dependencyData.ComponentDataList)
                {
                    var rawComponentName = componentData.RawComponentName;
                    var component        = scriptDict[rawComponentName].Script.GetClass();
                    var addComponent     = gameObject.AddComponent(component);
                    componentDict.Add(rawComponentName, addComponent); // 追加したコンポーネントを登録
                }
            }

            // コンポーネントの参照関係の設定
            foreach (var dependencyData in scriptDependency.DataList)
            {
                foreach (var componentData in dependencyData.ComponentDataList)
                {
                    foreach (var refData in componentData.ReferenceList)
                    {
                        // 参照先GameObject
                        var refGameObject = gameObjectDict[refData.RawGameObjectName];

                        // 参照先コンポーネント名
                        var refComponentName = NamePostprocessor.Rename(refData.RawComponentName);

                        // リフレクション経由で設定
                        var refComponent = refGameObject.GetComponent(refComponentName);
                        var component    = componentDict[componentData.RawComponentName];

                        var flags  = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
                        var fields = component.GetType().GetFields(flags).Where(f => f != null);

                        var field = fields.FirstOrDefault(f => f.FieldType.ToString().Contains(refComponentName));
                        if (field != null)
                        {
                            // 値の設定
                            field.SetValue(component, refComponent);
                        }
                    }
                }
            }

            // GameObjectの名前修正
            foreach (var item in gameObjectDict)
            {
                item.Value.name = NamePostprocessor.Rename(item.Value.name);
            }

            // シーンアセット 保存
            EditorSceneManager.SaveScene(scene, scenePath);
            EditorSceneManager.CloseScene(scene, true);
            EditorUtility.ClearProgressBar();

            // 作成したシーンをハイライトする
            EditorApplication.delayCall += () =>
                                           EditorApplication.delayCall += () =>
            {
                Debug.LogFormat("Create: {0}", temporaryData.FolderPath);
                EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath(scenePath, typeof(UnityEngine.Object)));
            };
        }