Beispiel #1
0
 public TutorialText(string s, int level, LevelTiming appear, LevelTiming disappear)
 {
     Text            = s;
     Level           = level;
     AppearTiming    = appear;
     DisappearTiming = disappear;
 }
    static void CreateTimingObj()
    {
        GameObject o = Create("LevelTiming", "", "", true);

        if (o != null)
        {
            LevelTiming bc = o.AddComponent <LevelTiming>();
        }
    }
    public void Serialize(ref ByteStream levelBits, GameObject excludeRoot = null)
    {
        // Make sure that we have our map components.
        EnsureMapComponentsLoaded();

        meshManager.Clear();

        var scene = new LevelScene();

        scene.root.name = scene.name = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
        int id = 0;

        int.TryParse(RenderSettings.skybox.name, out id);
        if (id < 5)
        {
            id = 11;
        }
        scene.skyboxId = id;

        // Grab a copy of the world...
        var rootObjects = UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects();

        foreach (GameObject r in rootObjects)
        {
            if (r == excludeRoot)
            {
                continue;
            }

            OptimizeLevelGeometry(r);

            HandleSun(r, scene);
            HandlePreviewCamera(r, scene);

            var item = SerializeGameObject(r, true);
            if (item != null)
            {
                scene.root.children.Add(item);
            }
        }

        meshManager.FinalizeProxies();

        // Now generate some static GOs and add to the scene. We generate
        // separate GOs for renderable, collidable or both geometry.
        foreach (var pm in meshManager.Proxies)
        {
            var staticGo = new GameObject();

            staticGo.name = "StaticMesh_" + (pm.material != null ? pm.material.name : "Invisible");

            var mf = staticGo.AddComponent <MeshFilter>();
            mf.sharedMesh = pm.mesh;

            if ((pm.type & ProxyMeshInfo.MeshTypes.IsVisual) != 0)
            {
                var mr = staticGo.AddComponent <MeshRenderer>();
                mr.material = pm.material;

                // Need to actually set these values to get lightmapping...
                mr.lightmapIndex       = 0;
                mr.lightmapScaleOffset = new Vector4(1, 1, 0, 0);

                staticGo.name += "_R";
            }

            if ((pm.type & ProxyMeshInfo.MeshTypes.IsCollider) != 0)
            {
                staticGo.AddComponent <MeshCollider>();

                staticGo.name += "_MC";
            }

            var levelRoot = SerializeGameObject(staticGo);
            if (levelRoot != null)
            {
                scene.root.children.Add(levelRoot);
            }

            levelRoot.mesh.Flags |= LevelMesh.MeshFlags.IsStatic;

            GameObject.DestroyImmediate(staticGo);
        }

        // Do final propagation of max vertex counts.
        foreach (var c in scene.root.children)
        {
            if (c.LargestChildVertexCount > scene.root.LargestChildVertexCount)
            {
                scene.root.LargestChildVertexCount = c.LargestChildVertexCount;
            }
        }

        // Actually write to stream.
        Profiler.BeginSample("Serialize");

        var sh = new SerializerHelper();

        sh.Stream = new ByteStream();

        // Write header info - Marble it Up Level v5
        // v1 - initial format
        // v2 - added lightmaps
        // v3 - added level timing
        // v4 - added map hash
        // v5 - added map author
        sh.Stream.WriteByte((byte)'m');
        sh.Stream.WriteByte((byte)'u');
        sh.Stream.WriteByte((byte)'l');
        sh.Stream.WriteByte((byte)'6');

        if (GameObject.Find("StartPad") == null)
        {
            sh.Stream.WriteByte((byte)'M'); // Multiplayer
            sh.Stream.WriteByte((byte)'1'); // TODO - Encode MP Mode support
        }
        else
        {
            sh.Stream.WriteByte((byte)'S'); // Singleplayer
            sh.Stream.WriteByte((byte)'0'); // Unused MP Mode support
        }

        float       silver = 45, gold = 20, diamond = 15;
        LevelTiming lt = GameObject.FindObjectOfType <LevelTiming>();

        if (lt != null)
        {
            silver  = (float)(Math.Truncate(100 * lt.SilverTime) / 100f);
            gold    = (float)(Math.Truncate(100 * lt.GoldTime) / 100f);
            diamond = (float)(Math.Truncate(100 * lt.DiamondTime) / 100f);
        }
        sh.Stream.WriteSingle(silver);
        sh.Stream.WriteSingle(gold);
        sh.Stream.WriteSingle(diamond);

        string author = "";

        if (lt != null)
        {
            author = lt.Author;
        }
        sh.Stream.WriteString(author);

        PlayModifiers phym       = GameObject.FindObjectOfType <PlayModifiers>();
        string        physParams = "";

        if (phym != null)
        {
            physParams = phym.ToJSON();
        }
        sh.Stream.WriteString(physParams);

        var hashStream = new SerializerHelper();

        hashStream.Write(scene);
        string hash = "000-" + UnityEngine.Random.Range(0, int.MaxValue);

        using (System.Security.Cryptography.SHA1CryptoServiceProvider sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider())
        {
            hash = Convert.ToBase64String(sha1.ComputeHash(hashStream.Stream.Buffer));
        }
        sh.Stream.WriteString(hash);

        // And write the scene.
        sh.Write(scene);

        // Also, write the lightmaps.
        SerializeLightmaps(ref sh);

        levelBits = sh.Stream;

        // If you want to enable compression of level files, this is
        // where to do it.
        //levelBits.Buffer = SerializerHelper.Compress(levelBits.Buffer);

        levelSize = levelBits.Position / 1024;
        Debug.Log("Serialized level, length = " + levelSize + "kb");

        Profiler.EndSample();
    }