Example #1
0
        public static ChildPiece [] ExtractInstances(string instances)
        {
            List <string> splitLayer = Data.GameData.SplitString(instances, new string [] { "\n" });

            //string[] splitLayer = instances.Split(new string [] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
            ChildPiece[] childPieces = new ChildPiece [splitLayer.Count];
            if (splitLayer.Count > 0)
            {
                for (int i = 0; i < splitLayer.Count; i++)
                {
                    ChildPiece    childPiece = new ChildPiece();
                    List <string> splitChild = Data.GameData.SplitString(splitLayer [i], new string [] { ",\t" });
                    //string[] splitChild = splitLayer[i].Split(new string [] { ",\t" }, StringSplitOptions.RemoveEmptyEntries);
                    //0:packname, 1:childname, 2:xpos, 3:ypos, 4:zpos, 5:xrot, 6:yrot, 7:zrot, 8:xscl, 9:yscl, 10:zscl, 11:mat1|mat2|mat3, 12:destroyedBehavior\n\r
                    childPiece.PackName   = splitChild [0];
                    childPiece.ChildName  = splitChild [1];
                    childPiece.Position   = Vector3.zero;
                    childPiece.Position.x = float.Parse(splitChild [2]);
                    childPiece.Position.y = float.Parse(splitChild [3]);
                    childPiece.Position.z = float.Parse(splitChild [4]);
                    childPiece.Rotation   = Vector3.zero;
                    childPiece.Rotation.x = float.Parse(splitChild [5]);
                    childPiece.Rotation.y = float.Parse(splitChild [6]);
                    childPiece.Rotation.z = float.Parse(splitChild [7]);
                    childPiece.Scale      = Vector3.zero;
                    childPiece.Scale.x    = float.Parse(splitChild [8]);
                    childPiece.Scale.y    = float.Parse(splitChild [9]);
                    childPiece.Scale.z    = float.Parse(splitChild [10]);
                    childPiece.Materials  = new string[0];
                    if (splitChild.Count > 11)
                    {
                        childPiece.DestroyedBehavior = Int32.Parse(splitChild [11]);
                    }
                    childPieces [i] = childPiece;
                    splitChild.Clear();
                    splitChild = null;
                }
            }
            splitLayer.Clear();
            splitLayer = null;
            return(childPieces);
        }
Example #2
0
        public void CreateFoundation()
        {
            if (!Manager.IsAwake <Mods> ())
            {
                Manager.WakeUp <Mods> ("__MODS");
            }
            Mods.Get.Editor.InitializeEditor(true);

            if (!Manager.IsAwake <Structures> ())
            {
                Manager.WakeUp <Structures> ("Frontiers_Structures");
            }
            Structures.Get.Initialize();

            if (!Manager.IsAwake <WorldItems> ())
            {
                Manager.WakeUp <WorldItems> ("Frontiers_WorldItems");
            }
            WorldItems.Get.Initialize();

            StructureTemplate template = null;

            if (Mods.Get.Editor.LoadMod <StructureTemplate> (ref template, "Structure", StructureBuilder.GetTemplateName(name)))
            {
                GameObject foundation = gameObject.FindOrCreateChild("__TEMP_FOUNDATION").gameObject;
                foreach (StructureLayer staticLayer in template.Exterior.StaticStructureLayers)
                {
                    string prefabName = staticLayer.PrefabName.Trim().ToLower();
                    if (prefabName.Contains("foundation"))
                    {
                        StructurePackPrefab prefab = null;
                        if (Structures.Get.PackStaticPrefab(staticLayer.PackName, staticLayer.PrefabName, out prefab))
                        {
                            List <ChildPiece> staticPieces = null;
                            if (StructureTemplate.ExtractChildPiecesFromLayer(ref staticPieces, staticLayer.Instances))
                            {
                                //ChildPiece[] staticPieces = StructureTemplate.ExtractChildPiecesFromLayer(staticLayer.Instances);
                                for (int i = 0; i < staticPieces.Count; i++)
                                {
                                    ChildPiece piece = staticPieces [i];
                                    GameObject instantiatedPrefab = UnityEditor.PrefabUtility.InstantiatePrefab(prefab.Prefab) as GameObject;
                                    //instantiate a new prefab - keep it as a prefab!
                                    instantiatedPrefab.name             = prefab.Prefab.name;
                                    instantiatedPrefab.transform.parent = foundation.transform;
                                    instantiatedPrefab.tag   = staticLayer.Tag;
                                    instantiatedPrefab.layer = staticLayer.Layer;
                                    //put it in the right place
                                    instantiatedPrefab.transform.localPosition = piece.Position;
                                    instantiatedPrefab.transform.localRotation = Quaternion.identity;
                                    instantiatedPrefab.transform.Rotate(piece.Rotation);
                                    instantiatedPrefab.transform.localScale = piece.Scale;

                                    Material[] variationsArray = null;
                                    if (staticLayer.Substitutions != null && staticLayer.Substitutions.Count > 0)
                                    {
                                        MeshRenderer pmr = prefab.MRenderer;
                                        variationsArray = pmr.sharedMaterials;
                                        string newMaterialName = string.Empty;
                                        for (int j = 0; j < variationsArray.Length; j++)
                                        {
                                            if (staticLayer.Substitutions.TryGetValue(variationsArray [j].name, out newMaterialName))
                                            {
                                                Material sharedMaterial = null;
                                                if (Structures.Get.SharedMaterial(newMaterialName, out sharedMaterial))
                                                {
                                                    variationsArray [j] = sharedMaterial;
                                                }
                                            }
                                        }
                                        instantiatedPrefab.GetComponent <Renderer>().materials = variationsArray;
                                    }
                                }
                                staticPieces.Clear();
                                staticPieces = null;
                            }
                        }
                    }
                }
            }
        }