void EndOfScript(MMD.PMD.PMDFormat format)
    {
        AssetDatabase.Refresh();

        this.mesh      = null;
        this.materials = null;
        this.bones     = null;
        this.pmd       = null;
    }
    // PMDファイル読み込み
    void LoadPMDFile()
    {
        string       path = AssetDatabase.GetAssetPath(this.pmd);
        BinaryReader bin  = this.LoadFile(this.pmd, path);

        MMD.PMD.PMDFormat format = MMD.PMD.PMDLoader.Load(bin, obj, path);
        BurnUnityFormatForPMD(format);
        bin.Close();
    }
    // PMDファイルをUnity形式に変換
    void BurnUnityFormatForPMD(MMD.PMD.PMDFormat format)
    {
        format.fst         = this.fst;
        obj                = new GameObject(format.name);
        format.caller      = obj;
        format.shader_type = this.shader_type;

        MMD.PMD.PMDConverter conv = new MMD.PMD.PMDConverter();

        this.mesh      = conv.CreateMesh(format);                       // メッシュの生成・設定
        this.materials = conv.CreateMaterials(format);                  // マテリアルの生成・設定
        this.bones     = conv.CreateBones(format);                      // ボーンの生成・設定

        // バインドポーズの作成
        conv.BuildingBindpose(format, this.mesh, this.materials, this.bones);
        obj.AddComponent <Animation>();         // アニメーションを追加

        MMDEngine engine = obj.AddComponent <MMDEngine>();

        // IKの登録
        if (this.use_ik)
        {
            engine.ik_list = conv.EntryIKSolver(format, this.bones);
        }

        // 剛体関連
        if (this.rigidFlag)
        {
            try
            {
                this.rigids = conv.CreateRigids(format, bones);
                conv.AssignRigidbodyToBone(format, this.bones, this.rigids);
                conv.SetRigidsSettings(format, this.bones, this.rigids);
                conv.SettingJointComponent(format, this.bones, this.rigids);

                // 非衝突グループ
                List <int>[] ignoreGroups = conv.SettingIgnoreRigidGroups(format, this.rigids);
                int[]        groupTarget  = conv.GetRigidbodyGroupTargets(format, rigids);

                MMDEngine.Initialize(engine, groupTarget, ignoreGroups, this.rigids);
            }
            catch { }
        }

#if UNITY_4_0
        AvatarSettingScript avt_setting = new AvatarSettingScript(format.caller);
        avt_setting.SettingAvatar();
#endif

        var window = LoadedWindow.Init();
        window.Text = format.head.comment;

        CreatePrefab(format);
        EndOfScript(format);
    }
Beispiel #4
0
        /// <summary>
        /// プレファブを作成する
        /// </summary>
        /// <param name='shader_type'>シェーダーの種類</param>
        /// <param name='use_rigidbody'>剛体を使用するか</param>
        /// <param name='animation_type'>アニメーションタイプ</param>
        /// <param name='use_ik'>IKを使用するか</param>
        /// <param name='scale'>スケール</param>
        /// <param name='is_pmx_base_import'>PMX Baseでインポートするか</param>
        public void CreatePrefab(PMDConverter.ShaderType shader_type, bool use_rigidbody, PMXConverter.AnimationType animation_type, bool use_ik, float scale, bool is_pmx_base_import)
        {
            GameObject game_object;
            string     prefab_path;

            if (is_pmx_base_import)
            {
                //PMX Baseでインポートする
                //PMXファイルのインポート
                PMX.PMXFormat pmx_format = null;
                try {
                    //PMX読み込みを試みる
                    pmx_format = PMXLoaderScript.Import(file_path_);
                } catch (System.FormatException) {
                    //PMXとして読み込めなかったら
                    //PMDとして読み込む
                    PMD.PMDFormat pmd_format = PMDLoaderScript.Import(file_path_);
                    pmx_format = PMXLoaderScript.PMD2PMX(pmd_format);
                }
                header_ = pmx_format.header;
                //ゲームオブジェクトの作成
                game_object = PMXConverter.CreateGameObject(pmx_format, use_rigidbody, animation_type, use_ik, scale);

                // プレファブパスの設定
                prefab_path = pmx_format.meta_header.folder + "/" + pmx_format.meta_header.name + ".prefab";
            }
            else
            {
                //PMXエクスポーターを使用しない
                //PMDファイルのインポート
                PMD.PMDFormat pmd_format = null;
                try {
                    //PMX読み込みを試みる
                    PMX.PMXFormat pmx_format = PMXLoaderScript.Import(file_path_);
                    pmd_format = PMXLoaderScript.PMX2PMD(pmx_format);
                } catch (System.FormatException) {
                    //PMXとして読み込めなかったら
                    //PMDとして読み込む
                    pmd_format = PMDLoaderScript.Import(file_path_);
                }
                header_ = PMXLoaderScript.PMD2PMX(pmd_format.head);

                //ゲームオブジェクトの作成
                bool use_mecanim = PMXConverter.AnimationType.LegacyAnimation == animation_type;
                game_object = PMDConverter.CreateGameObject(pmd_format, shader_type, use_rigidbody, use_mecanim, use_ik, scale);

                // プレファブパスの設定
                prefab_path = pmd_format.folder + "/" + pmd_format.name + ".prefab";
            }
            // プレファブ化
            PrefabUtility.CreatePrefab(prefab_path, game_object, ReplacePrefabOptions.ConnectToPrefab);

            // アセットリストの更新
            AssetDatabase.Refresh();
        }
    void CreatePrefab(MMD.PMD.PMDFormat format)
    {
        Object prefab = PrefabUtility.CreateEmptyPrefab(format.folder + "/" + format.name + ".prefab");

#if UNITY_4_0 || UNITY_3_5
        // 4.0からPrefabUtilityを使えとのこと
        //   3.5.6f4でもコンソールで警告がでていたので3.5もこっち
        PrefabUtility.ReplacePrefab(format.caller, prefab);
#elif UNITY_3_4
        EditorUtility.ReplacePrefab(format.caller, prefab);
#endif
    }