Skip to content

Dev Unity

Valentin Noves edited this page Jul 25, 2020 · 3 revisions

Create object

Create a new object on the scene and rename it to "MeshImport"

BIMSocket logo

Add a Mesh Filter

Add mesh filter to the "MeshImport"object

BIMSocket logo

Add a Mesh Renderer

Add mesh Renderer to the "MeshImport"object

BIMSocket logo

Add a Custom Script

Add a custom script and rename it to "MeshImporter"

BIMSocket logo

This is the script that will import/export things from/to to Firebase

[RequireComponent(typeof(MeshFilter))]
public class MeshImporter : MonoBehaviour
{
    public static FirebaseFirestore DB;
    UnityEngine.Mesh mesh;
    Vector3[] vertices;
    int[] triangles;
    private Rootobject importModel;
    Vector3 getPos;
    GameObject Main;
    List<Vector3> postionElements = new List<Vector3>();

    // Start is called before the first frame update
    void Start()
    {
        //Connect to Firebase
        DB = FirebaseFirestore.GetInstance(FirebaseApp.DefaultInstance);    
        FBModelImport();
    }

    public async void FBModelImport()
    {
        Rootobject result = new Rootobject();
        try
        {
            DocumentReference docRef = DB.Collection("models").Document("Walls");    
            var listener = docRef.Listen(snapshot =>
            {
                if (snapshot.Exists)
                {
                    var st = Newtonsoft.Json.JsonConvert.SerializeObject(snapshot.ToDictionary());
                    importModel = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(st);
                    //If the element already exists remove it
                    var gameObjects = SceneManager.GetActiveScene().GetRootGameObjects();
                    foreach(GameObject go in gameObjects)
                    {
                        if(go.name == importModel._object.name)
                        {
                            Destroy(go);
                        }
                    }
                    CreateMaterials.create(importModel.materials);
                    //Create main Game object
                    GameObject MainObj = new GameObject(importModel._object.name);           
                    //Create geometries
                    List<GameObject> createdGeom = Geometries.create(importModel.geometries);
                    //Create Childrens and assign them to Main Object
                    ObjChildrens.create(importModel._object.children, MainObj, createdGeom);
                    Main = MainObj;

                    postionElements = Utils.currentposition(MainObj);
                }
            });
        }
        catch
        {
            
        }

    }

Clone this wiki locally