Exemple #1
0
    public static void GroupToPrefab(string path)
    {
        XmlDocument doc = new XmlDocument();

        try {
            doc.Load(path);
        }
        catch { }

        GameObject            tempPivot = new GameObject();
        List <B2U_ParentList> pList     = new List <B2U_ParentList>();

        XmlNode     root       = doc.DocumentElement;
        XmlNodeList Objects    = root.SelectNodes("Object");
        string      prefabPath = root.SelectSingleNode("Path").InnerText;

        foreach (XmlNode Obj in Objects)
        {
            string     prefab_path = Obj.SelectSingleNode("Prefab").InnerText;
            string     loc         = Obj.SelectSingleNode("Position").InnerText;
            string     rot         = Obj.SelectSingleNode("Rotation").InnerText;
            string     sca         = Obj.SelectSingleNode("Scale").InnerText;
            string     name        = Obj.SelectSingleNode("Name").InnerText;
            GameObject element     = AssetDatabase.LoadAssetAtPath <GameObject>(prefab_path);
            GameObject objTemp     = PrefabUtility.InstantiatePrefab(element) as GameObject;

            objTemp.name             = name;
            objTemp.transform.parent = tempPivot.transform;

            objTemp.transform.position   = parseVector3(loc);
            objTemp.transform.localScale = parseVector3(sca);
            Vector3 FRot = parseVector3(rot);

            objTemp.transform.rotation = new Quaternion();
            objTemp.transform.Rotate(new Vector3(FRot[0] * -1, 0, 0), Space.World);
            objTemp.transform.Rotate(new Vector3(0, FRot[2] * -1, 0), Space.World);
            objTemp.transform.Rotate(new Vector3(0, 0, FRot[1] * -1), Space.World);

            B2U_ParentList TempDataObj = new B2U_ParentList();
            TempDataObj.obj        = objTemp;
            TempDataObj.parentName = Obj.SelectSingleNode("Parent").InnerText;
            pList.Add(TempDataObj);
        }

        // Configure Parents
        for (int k = 0; k < pList.Count; k++)
        {
            B2U_ParentList    Data       = pList[k];
            GameObject        ObjSource  = Data.obj;
            string            dest       = Data.parentName;
            List <GameObject> staticList = new List <GameObject>();

            foreach (B2U_ParentList obj in pList)
            {
                if (Data.parentName == obj.obj.name)
                {
                    ObjSource.transform.parent = obj.obj.transform;
                }
            }
        }

        // Save in Project
        PrefabUtility.SaveAsPrefabAsset(tempPivot, prefabPath);
        GameObject.DestroyImmediate(tempPivot);
    }
Exemple #2
0
    public static void SceneToPrefab(string path)
    {
        XmlDocument doc = new XmlDocument();

        try {
            doc.Load(path);
        }
        catch {
            Debug.Log("B2U Process: Scene is not Valid");
        }
        XmlNode root = doc.DocumentElement;

        XmlNode Parameters = root.SelectSingleNode("Parameters");

        // Cria a cena
        GameObject ScenePivot = new GameObject();

        ScenePivot.name = Parameters.SelectSingleNode("Name").InnerText;

        // Objetos da Cena e seu parentesco
        List <B2U_ParentList> pList = new List <B2U_ParentList>();

        // Importa Objetos
        XmlNodeList Objects = root.SelectSingleNode("Objects").ChildNodes;

        foreach (XmlNode Obj in Objects)
        {
            string prefab_path = Obj.SelectSingleNode("Prefab").InnerText;
            string loc         = Obj.SelectSingleNode("Position").InnerText;
            string rot         = Obj.SelectSingleNode("Rotation").InnerText;
            string sca         = Obj.SelectSingleNode("Scale").InnerText;
            string nameObj     = Obj.SelectSingleNode("Name").InnerText;

            // Overwrited per Object Settings
            string sta   = Obj.SelectSingleNode("Static").InnerText;
            string tag   = Obj.SelectSingleNode("Tag").InnerText;
            string layer = Obj.SelectSingleNode("Layer").InnerText;

            GameObject element = AssetDatabase.LoadAssetAtPath <GameObject>(prefab_path);
            GameObject objTemp = PrefabUtility.InstantiatePrefab(element) as GameObject;

            objTemp.name             = nameObj;
            objTemp.transform.parent = ScenePivot.transform;

            objTemp.transform.position   = parseVector3(loc);
            objTemp.transform.localScale = parseVector3(sca);
            Vector3 FRot = parseVector3(rot);

            objTemp.transform.rotation = new Quaternion();
            objTemp.transform.Rotate(new Vector3(FRot[0] * -1, 0, 0), Space.World);
            objTemp.transform.Rotate(new Vector3(0, FRot[2] * -1, 0), Space.World);
            objTemp.transform.Rotate(new Vector3(0, 0, FRot[1] * -1), Space.World);

            if (sta != "Keep")
            {
                if (sta == "Static")
                {
                    objTemp.isStatic = true;
                }
                else
                {
                    objTemp.isStatic = false;
                }
            }

            // Check if Layer is Valid and Set
            if (layer != "")
            {
                int layeridx = LayerMask.NameToLayer(layer);
                objTemp.layer = ((layeridx >= 0) ? layeridx : 0);

                // Check if Tag is Valid and Set
                if (tag != "")
                {
                    for (int j = 0; j < UnityEditorInternal.InternalEditorUtility.tags.Length; j++)
                    {
                        if (UnityEditorInternal.InternalEditorUtility.tags[j].Contains(tag))
                        {
                            objTemp.tag = tag;
                            break;
                        }
                    }
                }
            }

            B2U_ParentList TempDataObj = new B2U_ParentList();
            TempDataObj.obj        = objTemp;
            TempDataObj.parentName = Obj.SelectSingleNode("Parent").InnerText;
            pList.Add(TempDataObj);
        }

        // Importa Lampadas
        XmlNodeList Lamps = root.SelectSingleNode("Lamps").ChildNodes;

        foreach (XmlNode Lamp in Lamps)
        {
            string nameLamp  = Lamp.SelectSingleNode("Name").InnerText;
            string locLamp   = Lamp.SelectSingleNode("Position").InnerText;
            string rotLamp   = Lamp.SelectSingleNode("Rotation").InnerText;
            string colorLamp = Lamp.SelectSingleNode("Color").InnerText;
            string powerLamp = Lamp.SelectSingleNode("Power").InnerText;
            string typeLamp  = Lamp.SelectSingleNode("Type").InnerText;

            string     objNameLamp = B2U_Importer.prefixPrefab + nameLamp;
            GameObject objLamp     = new GameObject();
            objLamp.AddComponent <Light>();
            objLamp.name             = objNameLamp;
            objLamp.transform.parent = ScenePivot.transform;

            objLamp.transform.position = parseVector3(locLamp);
            Vector3 FRotLamp = parseVector3(rotLamp);

            objLamp.transform.rotation = Quaternion.identity;
            //Fix Light Default Rotation in Unity
            objLamp.transform.Rotate(new Vector3(90, 0, 0), Space.World);

            objLamp.transform.Rotate(new Vector3(FRotLamp[0] * -1, 0, 0), Space.World);
            objLamp.transform.Rotate(new Vector3(0, FRotLamp[2] * -1, 0), Space.World);
            objLamp.transform.Rotate(new Vector3(0, 0, FRotLamp[1] * -1), Space.World);

            Color Col;
            ColorUtility.TryParseHtmlString(colorLamp, out Col);
            objLamp.GetComponent <Light>().color = Col;
            float power = float.Parse(powerLamp.Replace(".", ","));
            objLamp.GetComponent <Light>().intensity = power;
            objLamp.GetComponent <Light>().range     = Mathf.Sqrt(1 / power);

            if (typeLamp == "POINT")
            {
                objLamp.GetComponent <Light>().type = LightType.Point;
            }
            if (typeLamp == "SPOT")
            {
                string SpotSize = Lamp.SelectSingleNode("SpotSize").InnerText;
                objLamp.GetComponent <Light>().spotAngle = float.Parse(SpotSize.Replace(".", ","));
                objLamp.GetComponent <Light>().type      = LightType.Spot;
            }
            if (typeLamp == "SUN")
            {
                objLamp.GetComponent <Light>().type = LightType.Directional;
            }

            B2U_ParentList TempDataObj = new B2U_ParentList();
            TempDataObj.obj        = objLamp;
            TempDataObj.parentName = Lamp.SelectSingleNode("Parent").InnerText;
            pList.Add(TempDataObj);
        }

        // Importa Cameras
        XmlNodeList Cams = root.SelectSingleNode("Cameras").ChildNodes;

        foreach (XmlNode Cam in Cams)
        {
            string nameCam = Cam.SelectSingleNode("Name").InnerText;
            string locCam  = Cam.SelectSingleNode("Position").InnerText;
            string rotCam  = Cam.SelectSingleNode("Rotation").InnerText;
            string projCam = Cam.SelectSingleNode("Projection").InnerText;
            string fovCam  = Cam.SelectSingleNode("Fov").InnerText;
            string nearCam = Cam.SelectSingleNode("Near").InnerText;
            string farCam  = Cam.SelectSingleNode("Far").InnerText;
            string sizeCam = Cam.SelectSingleNode("Size").InnerText;

            string     objNameCam = B2U_Importer.prefixPrefab + nameCam;
            GameObject objCam     = new GameObject();
            objCam.AddComponent <Camera>();
            objCam.name = objNameCam;

            objCam.transform.parent   = ScenePivot.transform;
            objCam.transform.position = parseVector3(locCam);
            Vector3 FrotCam = parseVector3(rotCam);

            objCam.transform.rotation = new Quaternion(); //Quaternion.EulerRotation(0, 0, 0);
            objCam.transform.rotation = Quaternion.Euler(90 - FrotCam[0], FrotCam[2] * -1, FrotCam[1] * -1);

            float vFov = Mathf.Atan(Mathf.Tan(Mathf.Deg2Rad * float.Parse(fovCam.Replace(".", ",")) / 2) / objCam.GetComponent <Camera>().aspect) * 2;
            vFov *= Mathf.Rad2Deg;

            objCam.GetComponent <Camera>().fieldOfView      = vFov;
            objCam.GetComponent <Camera>().nearClipPlane    = float.Parse(nearCam.Replace(".", ","));
            objCam.GetComponent <Camera>().farClipPlane     = float.Parse(farCam.Replace(".", ","));
            objCam.GetComponent <Camera>().orthographicSize = float.Parse(sizeCam.Replace(".", ",")) * 0.28f;

            if (projCam == "PERSP")
            {
                objCam.GetComponent <Camera>().orthographic = false;
            }
            else
            {
                objCam.GetComponent <Camera>().orthographic = true;
            }

            B2U_ParentList TempDataObj = new B2U_ParentList();
            TempDataObj.obj        = objCam;
            TempDataObj.parentName = Cam.SelectSingleNode("Parent").InnerText;
            pList.Add(TempDataObj);
        }

        // Configure Parents
        for (int k = 0; k < pList.Count; k++)
        {
            B2U_ParentList    Data       = pList[k];
            GameObject        ObjSource  = Data.obj;
            string            dest       = Data.parentName;
            List <GameObject> staticList = new List <GameObject>();

            foreach (B2U_ParentList obj in pList)
            {
                if (Data.parentName == obj.obj.name)
                {
                    ObjSource.transform.parent = obj.obj.transform;
                }
            }
        }

        // Save in Project
        string dir        = Path.GetDirectoryName(path);
        string nameExt    = Path.GetFileName(path);
        int    fileExtPos = nameExt.LastIndexOf(".");
        string name       = nameExt.Substring(0, fileExtPos);

        string prefabPath = dir + "/" + name + ".prefab";

        PrefabUtility.SaveAsPrefabAsset(ScenePivot, prefabPath);
        GameObject.DestroyImmediate(ScenePivot);
    }