示例#1
0
    // run importer
    private unsafe void RunImport()
    {
        // adjust global settings
        Time.fixedDeltaTime            = 0.005f;
        PlayerSettings.runInBackground = true;
        if (enableRemote)
        {
            QualitySettings.vSyncCount = (noVSync ? 0 : 1);
        }
        else
        {
            QualitySettings.vSyncCount = 1;
        }

        // disable active cameras
        Camera[] activecam = FindObjectsOfType <Camera>();
        foreach (Camera ac in activecam)
        {
            ac.gameObject.SetActive(false);
        }

        // get filename only (not path or extension)
        int i1 = modelFile.LastIndexOf('/');
        int i2 = modelFile.LastIndexOf('.');

        if (i1 >= 0 && i2 > i1)
        {
            fileName = modelFile.Substring(i1 + 1, i2 - i1 - 1);
        }
        else
        {
            throw new System.Exception("Unexpected model file format");
        }

        // initialize plugin and load model
        MJP.Initialize();
        MJP.LoadModel(modelFile);

        // get model sizes
        MJP.TSize size;
        MJP.GetSize(&size);

        // save binary model
        MakeDirectory("Assets", "StreamingAssets");
        MJP.SaveMJB("Assets/StreamingAssets/" + fileName + ".mjb");

        // import textures
        if (size.ntexture > 0 && importTexture)
        {
            MakeDirectory("Assets", "Textures");
            ImportTextures(size.ntexture);
        }

        // import materials
        if (size.nmaterial > 0)
        {
            MakeDirectory("Assets", "Materials");
            ImportMaterials(size.nmaterial);
        }

        // create root, destroy old if present
        root = GameObject.Find("MuJoCo");
        if (root != null)
        {
            DestroyImmediate(root);
        }
        root = new GameObject("MuJoCo");
        if (root == null)
        {
            throw new System.Exception("Could not create root MuJoCo object");
        }

        // add camera to root
        AddCamera();

        // import renderable objects under root
        ImportObjects(size.nobject);

        // attach script to root
        if (enableRemote)
        {
            // add remote
            MJRemote rem = root.GetComponent <MJRemote>();
            if (rem == null)
            {
                rem = root.AddComponent <MJRemote>();
            }
            rem.modelFile  = fileName + ".mjb";
            rem.tcpAddress = tcpAddress;
            rem.tcpPort    = tcpPort;

            // destroy simulate if present
            if (root.GetComponent <MJSimulate>())
            {
                DestroyImmediate(root.GetComponent <MJSimulate>());
            }
        }
        else
        {
            // add simulate
            MJSimulate sim = root.GetComponent <MJSimulate>();
            if (sim == null)
            {
                sim = root.AddComponent <MJSimulate>();
            }
            sim.modelFile = fileName + ".mjb";

            // destroy remote if present
            if (root.GetComponent <MJRemote>())
            {
                DestroyImmediate(root.GetComponent <MJRemote>());
            }
        }

        // close plugin
        MJP.Close();
    }