Ejemplo n.º 1
0
        /// <summary>
        /// シーン内のスクリプト作成を行う
        /// </summary>
        public static IEnumerable <MonoScriptData> BuildScripts(string folderPath, string folderName, ScriptDependency dependency)
        {
            var datas = dependency.DataList
                        .SelectMany(data => data.ComponentDataList);

            var classNameList = new List <string>();

            foreach (var data in datas)
            {
                NamePostprocessor.Initialize(folderName); // 設定

                var className = data.RawComponentName;
                className = NamePostprocessor.Rename(className); // リネーム
                className = NameCorrector.CorrectNameIfInvalid(className);

                if (classNameList.Contains(className))
                {
                    Debug.LogErrorFormat("クラス名重複 : className = {0}", className);
                    continue;
                }
                classNameList.Add(className);

                var templateName = data.TemplateName;
                var scriptName   = string.Format("{0}.cs", className);
                var script       = CreateScriptAsset(folderPath, folderName, scriptName, templateName);

                yield return(new MonoScriptData
                {
                    Script = script,
                    RawComponentName = data.RawComponentName
                });
            }
        }
Ejemplo n.º 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)));
            };
        }