Esempio n. 1
0
    void SaveLinesInScene(string fileName)
    {
        string          pathToSave = Path.Combine(Application.persistentDataPath, string.Concat(fileName, ".dat"));
        BinaryFormatter formatter  = new BinaryFormatter();

        try
        {
            FileStream   file         = File.Open(pathToSave, FileMode.Create);
            GameObject[] linesInScene = GameObject.FindGameObjectsWithTag("Line");

            if (linesInScene.Length > 0)
            {
                LineRendererData data = new LineRendererData();
                data.lines = new List <SingleLineRendererData>();

                for (int i = 0; i < linesInScene.Length; i++)
                {
                    SingleLineRendererData lineData = null;

                    if (linesInScene[i] != null)
                    {
                        lineData = LineRendererToData(linesInScene[i].GetComponent <LineRenderer>());
                    }

                    if (lineData != null)
                    {
                        data.lines.Add(lineData);
                    }
                }
                if (data.lines.Count > 0 && file != null)
                {
                    formatter.Serialize(file, data);
                }
            }
            file.Close();
        }
        catch (Exception e)
        {
            Debug.Log(e.ToString());
            GameObject.Find("Text").GetComponent <UpdateWorldMappingStatus>().ChangeTextTo(e.ToString());
        }
    }
Esempio n. 2
0
    void LoadLinesFromFile(string pathText)
    {
        string           pathToLoad = Path.Combine(Application.persistentDataPath, pathText);
        LineRendererData data       = null;

        if (File.Exists(pathToLoad))
        {
            try
            {
                using (FileStream file = File.Open(pathToLoad, FileMode.Open))
                {
                    if (file != null)
                    {
                        BinaryFormatter formatter = new BinaryFormatter();
                        data = (LineRendererData)formatter.Deserialize(file);
                    }
                }

                if (data == null)
                {
                    return;
                }

                foreach (SingleLineRendererData line in data.lines)
                {
                    if (line != null)
                    {
                        GameObject go = new GameObject();

                        go.transform.position   = line.position;
                        go.transform.rotation   = line.rotation;
                        go.transform.localScale = line.scale;

                        go.tag = "Line";

                        go.AddComponent <LineRenderer>();

                        go.GetComponent <LineRenderer>().startColor        = line.startColor;
                        go.GetComponent <LineRenderer>().endColor          = line.startColor;
                        go.GetComponent <LineRenderer>().startWidth        = line.startWidth;
                        go.GetComponent <LineRenderer>().endWidth          = line.endWidth;
                        go.GetComponent <LineRenderer>().numCornerVertices = line.cornerVertices;
                        go.GetComponent <LineRenderer>().numCapVertices    = line.cornerVertices;
                        go.GetComponent <LineRenderer>().sharedMaterial    = allMaterials.materials[(int)line.material];
                        go.GetComponent <LineRenderer>().positionCount     = line.points.Count;
                        go.GetComponent <LineRenderer>().textureMode       = LineTextureMode.Tile;
                        go.GetComponent <LineRenderer>().receiveShadows    = false;
                        go.GetComponent <LineRenderer>().useWorldSpace     = false;
                        for (int i = 0; i < line.points.Count; i++)
                        {
                            go.GetComponent <LineRenderer>().SetPosition(i, line.points[i]);
                        }

                        go.AddComponent <UnityARUserAnchorComponent>();

                        Instantiate(go, go.transform.position, go.transform.rotation);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.Log(e.ToString());
                GameObject.Find("Text").GetComponent <UpdateWorldMappingStatus>().ChangeTextTo(e.ToString());
            }
        }
    }