Exemple #1
0
    void parseLevelFile()
    {
        StreamReader reader = File.OpenText(Application.dataPath + "/Data/1.txt");
        JSONObject   j      = new JSONObject(reader.ReadToEnd());
        JSONObject   target = j.GetField("game");

        foreach (JSONObject jsonObj in target.list)
        {
            GameObject obj = Instantiate(cellPrefab, Vector3.zero, Quaternion.identity) as GameObject;
            obj.transform.SetParent(targetContainer);
            obj.transform.localScale = Vector3.one;
            RectTransform reTrans = obj.GetComponent <RectTransform> ();
            reTrans.anchoredPosition = new Vector2(jsonObj.GetField("x").f, jsonObj.GetField("y").f);
            reTrans.eulerAngles      = new Vector3(0f, 0f, jsonObj.GetField("rot").f);
            PuzzleCell pc = obj.GetComponent <PuzzleCell> ();
            pc.SetType((PuzzleCell.CellType)Enum.Parse(typeof(PuzzleCell.CellType), jsonObj.GetField("type").str));
            cells.Add(pc);
        }

        JSONObject objects = j.GetField("objects");

        foreach (JSONObject jsonObj in objects.list)
        {
            GameObject obj = Instantiate(cellBlockPrefab, Vector3.zero, Quaternion.identity) as GameObject;
            obj.transform.SetParent(cellBlockContainer);
            obj.transform.localScale    = Vector3.one;
            obj.transform.localPosition = Vector3.zero;

            JSONObject tmp = jsonObj.GetField("obj");
            int        i   = 0;
            foreach (JSONObject jsonObjFigure in tmp.list)
            {
                GameObject obj1 = Instantiate(cellPrefab, Vector3.zero, Quaternion.identity) as GameObject;
                obj1.transform.SetParent(obj.transform);
                obj1.transform.localScale = Vector3.one;
                obj1.name = i.ToString();
                RectTransform rcTrans = obj1.GetComponent <RectTransform> ();
                rcTrans.anchoredPosition = new Vector2(jsonObjFigure.GetField("x").f, jsonObjFigure.GetField("y").f);
                rcTrans.eulerAngles      = new Vector3(0f, 0f, jsonObjFigure.GetField("rot").f);
                PuzzleCell pc = obj1.GetComponent <PuzzleCell> ();
                pc.img.color = cellColor;
                PuzzleCell.CellType t = (PuzzleCell.CellType)Enum.Parse(typeof(PuzzleCell.CellType), jsonObjFigure.GetField("type").str);
                pc.SetType(t);
                obj.GetComponent <PuzzleCellBlock> ().cells.Add(new PuzzleCellBlock.PuzzleCellBlockType(t, obj1.GetComponent <RectTransform> ()));
                pc.enabled = false;
                i++;
            }
        }
    }
Exemple #2
0
    void parseLevelMass()
    {
        float xPos   = 0f;
        float yPos   = 0f;
        float offset = cellPrefab.GetComponent <RectTransform>().rect.width;

        for (int i = 0; i < ySize; i++)
        {
            for (int j = 0; j < xSize; j++)
            {
                xPos += offset / 4f;
                PuzzleCell.CellType cellType = levelMass[i, j].type;
                if (cellType == PuzzleCell.CellType.none)
                {
                    continue;
                }

                float      rot = levelMass[i, j].angle;
                GameObject obj = Instantiate(cellPrefab, Vector3.zero, Quaternion.identity) as GameObject;
                obj.transform.SetParent(targetContainer);
                obj.transform.localScale = Vector3.one;
                RectTransform reTrans = obj.GetComponent <RectTransform> ();
                reTrans.anchoredPosition = new Vector2(xPos, yPos);
                reTrans.eulerAngles      = new Vector3(0f, 0f, rot);
                PuzzleCell pc = obj.GetComponent <PuzzleCell> ();
                pc.SetType(cellType);
                cells.Add(pc);
            }
            xPos  = 0f;
            yPos -= offset / 4f;
        }
        targetContainer.anchoredPosition = new Vector2(targetContainer.anchoredPosition.x - (xSize * offset / 8f), targetContainer.anchoredPosition.y + (ySize * offset / 8f));

        for (int i = 0; i < levelMassItems.Count; i++)
        {
            GameObject obj = Instantiate(cellBlockPrefab, Vector3.zero, Quaternion.identity) as GameObject;
            obj.transform.SetParent(cellBlockContainer);
            obj.transform.localScale    = Vector3.one;
            obj.transform.localPosition = Vector3.zero;
            xPos = -((offset / 4f) * 2f);
            yPos = offset / 4f;
            for (int k = 0; k < 3; k++)
            {
                for (int d = 0; d < 3; d++)
                {
                    xPos += offset / 4f;
                    if (levelMassItems[i][k, d].type == PuzzleCell.CellType.none)
                    {
                        continue;
                    }
                    GameObject obj1 = Instantiate(cellPrefab, Vector3.zero, Quaternion.identity) as GameObject;
                    obj1.transform.SetParent(obj.transform);
                    obj1.transform.localScale = Vector3.one;
                    obj1.name = k.ToString() + d.ToString();
                    RectTransform rcTrans = obj1.GetComponent <RectTransform> ();
                    rcTrans.anchoredPosition = new Vector2(xPos, yPos);
                    rcTrans.eulerAngles      = new Vector3(0f, 0f, levelMassItems[i][k, d].angle);
                    PuzzleCell pc = obj1.GetComponent <PuzzleCell> ();
                    pc.img.color = cellColor;
                    pc.SetType(levelMassItems[i][k, d].type);
                    obj.GetComponent <PuzzleCellBlock> ().cells.Add(new PuzzleCellBlock.PuzzleCellBlockType(levelMassItems[i][k, d].type, obj1.GetComponent <RectTransform> ()));
                    pc.enabled = false;
                }
                xPos  = -((offset / 4f) * 2f);
                yPos -= offset / 4f;
            }
        }
    }