public override void OnInspectorGUI() { DrawDefaultInspector(); GUILayout.Space(30); Vox myScript = (Vox)target; if (GUILayout.Button("Select File")) { myScript.file = EditorUtility.OpenFilePanel("Select Vox file", "", "vox"); } GUILayout.Space(30); if (GUILayout.Button("Reload")) { myScript.Reload(); } }
GameObject readXmlNode(XmlNode xmlNode, GameObject parent, string fileName) { GameObject go = new GameObject(xmlNode.Name); if (parent != null) { go.transform.parent = parent.transform; } if (xmlNode.NodeType == XmlNodeType.Comment) { CommentElement comment = go.AddComponent <CommentElement>(); go.transform.parent = parent.transform; comment.comment = xmlNode.InnerText; } else if (xmlNode.Name == "vehicle") { Vehicle tag = go.AddComponent <Vehicle>(); go.transform.parent = parent.transform; attachGameObjectProperties(tag, xmlNode); go.name = "<" + xmlNode.Name + " " + tag.teardownName + ">"; tag.sound = readString(xmlNode, "sound"); tag.spring = readFloat(xmlNode, "spring"); tag.acceleration = readFloat(xmlNode, "acceleration"); tag.antiroll = readFloat(xmlNode, "antiroll"); tag.damping = readFloat(xmlNode, "damping"); tag.friction = readFloat(xmlNode, "friction"); tag.difflock = readFloat(xmlNode, "difflock"); tag.steerassist = readFloat(xmlNode, "steerassist"); tag.topspeed = readFloat(xmlNode, "topspeed"); tag.driven = readBool(xmlNode, "driven", false); tag.antispin = readFloat(xmlNode, "antispin"); tag.soundVolume = readFloat(xmlNode, "soundvolume"); go.transform.localRotation = Quaternion.Euler(tag.rotation); go.transform.localPosition = tag.position; if (tag.scale > 0) { go.transform.localScale = go.transform.localScale * tag.scale; } addToParent(parent, tag); } else if (xmlNode.Name == "wheel") { Wheel tag = go.AddComponent <Wheel>(); go.transform.parent = parent.transform; attachTransformProperties(tag, xmlNode); go.name = "<" + xmlNode.Name + " " + tag.teardownName + ">"; tag.steer = readFloat(xmlNode, "steer"); tag.drive = readFloat(xmlNode, "drive"); tag.travel = readVec2(xmlNode, "travel"); go.transform.localRotation = Quaternion.Euler(tag.rotation); go.transform.localPosition = tag.position; if (tag.scale > 0) { go.transform.localScale = go.transform.localScale * tag.scale; } addToParent(parent, tag); } else if (xmlNode.Name == "spawnpoint") { SpawnPoint tag = go.AddComponent <SpawnPoint>(); go.transform.parent = parent.transform; attachTransformProperties(tag, xmlNode); go.name = "<" + xmlNode.Name + " " + tag.teardownName + ">"; go.transform.localRotation = Quaternion.Euler(tag.rotation); go.transform.localPosition = tag.position; if (tag.scale > 0) { go.transform.localScale = go.transform.localScale * tag.scale; } addToParent(parent, tag); } else if (xmlNode.Name == "location") { Location tag = go.AddComponent <Location>(); go.transform.parent = parent.transform; attachTransformProperties(tag, xmlNode); go.name = "<" + xmlNode.Name + " " + tag.teardownName + ">"; go.transform.localRotation = Quaternion.Euler(tag.rotation); go.transform.localPosition = tag.position; if (tag.scale > 0) { go.transform.localScale = go.transform.localScale * tag.scale; } addToParent(parent, tag); } else if (xmlNode.Name == "group") { Group tag = go.AddComponent <Group>(); go.transform.parent = parent.transform; attachTransformProperties(tag, xmlNode); go.name = "<" + xmlNode.Name + " " + tag.teardownName + ">"; go.transform.localRotation = Quaternion.Euler(tag.rotation); go.transform.localPosition = tag.position; if (tag.scale > 0) { go.transform.localScale = go.transform.localScale * tag.scale; } addToParent(parent, tag); } else if (xmlNode.Name == "rope") { Rope tag = go.AddComponent <Rope>(); go.transform.parent = parent.transform; attachTransformProperties(tag, xmlNode); go.name = "<" + xmlNode.Name + " " + tag.teardownName + ">"; go.transform.localRotation = Quaternion.Euler(tag.rotation); go.transform.localPosition = tag.position; tag.strength = readFloat(xmlNode, "strength"); tag.slack = readFloat(xmlNode, "slack"); addToParent(parent, tag); } else if (xmlNode.Name == "screen") { Screen tag = go.AddComponent <Screen>(); go.transform.parent = parent.transform; attachTransformProperties(tag, xmlNode); go.name = "<" + xmlNode.Name + " " + tag.teardownName + ">"; go.transform.localRotation = Quaternion.Euler(tag.rotation); go.transform.localPosition = tag.position; tag.size = readVec2(xmlNode, "size"); tag.isEnabled = readBool(xmlNode, "enabled", false); tag.interactive = readBool(xmlNode, "interactive", false); tag.emissive = readFloat(xmlNode, "emissive"); tag.color = readColor(xmlNode, "color"); tag.resolution = readVec2(xmlNode, "resolution"); tag.bulge = readVec2(xmlNode, "bulge"); tag.script = readString(xmlNode, "script"); if (tag.scale > 0) { go.transform.localScale = go.transform.localScale * tag.scale; } addToParent(parent, tag); } else if (xmlNode.Name == "vox") { Vox tag = go.AddComponent <Vox>(); go.transform.parent = parent.transform; attachGameObjectProperties(tag, xmlNode); tag.file = readString(xmlNode, "file").Replace("LEVEL", getLevelFolder(fileName));; tag.voxObject = readString(xmlNode, "object"); tag.dynamic = readBool(xmlNode, "prop", false); go.transform.localRotation = Quaternion.Euler(tag.rotation); go.transform.localPosition = tag.position; tag.Reload(); if (tag.scale > 0) { go.transform.localScale *= tag.scale; } addToParent(parent, tag); } else if (xmlNode.Name == "joint") { TeardownJoint tag = go.AddComponent <TeardownJoint>(); go.transform.parent = parent.transform; attachTransformProperties(tag, xmlNode); go.name = "<" + xmlNode.Name + " " + tag.teardownName + ">"; tag.type = (TeardownJoint.Type)Enum.Parse(typeof(TeardownJoint.Type), readString(xmlNode, "type", "ball"), false); tag.size = readFloat(xmlNode, "size"); tag.rotspring = readFloat(xmlNode, "rotspring"); tag.rotstrength = readFloat(xmlNode, "rotstrength"); tag.limits = readVec2(xmlNode, "limits"); go.transform.localRotation = Quaternion.Euler(tag.rotation); go.transform.localPosition = tag.position; if (tag.scale > 0) { go.transform.localScale = go.transform.localScale * tag.scale; } addToParent(parent, tag); } else if (xmlNode.Name == "voxbox") { VoxBox tag = go.AddComponent <VoxBox>(); go.transform.parent = parent.transform; attachGameObjectProperties(tag, xmlNode); go.name = "<" + xmlNode.Name + " " + tag.teardownName + ">"; tag.color = readColor(xmlNode, "color"); tag.dynamic = readBool(xmlNode, "prop", false); go.transform.localRotation = Quaternion.Euler(tag.rotation); go.transform.localPosition = tag.position; GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.parent = go.transform; cube.transform.localPosition = new Vector3(0.5f, 0.5f, -0.5f); cube.transform.localRotation = Quaternion.identity; cube.GetComponent <MeshRenderer>().sharedMaterial = new Material(Shader.Find("Standard")); go.transform.localScale = tag.size / 10; if (tag.scale > 0) { go.transform.localScale = go.transform.localScale * tag.scale; } addToParent(parent, tag); } else if (xmlNode.Name == "body") { Body tag = go.AddComponent <Body>(); go.transform.parent = parent.transform; attachGameObjectProperties(tag, xmlNode); go.name = "<" + xmlNode.Name + " " + tag.teardownName + ">"; tag.dynamic = readBool(xmlNode, "dynamic", false); go.transform.localRotation = Quaternion.Euler(tag.rotation); go.transform.localPosition = tag.position; if (tag.scale > 0) { go.transform.localScale = go.transform.localScale * tag.scale; } addToParent(parent, tag); } else if (xmlNode.Name == "water") { go.name = "<" + xmlNode.Name + ">"; Water tag = go.AddComponent <Water>(); go.transform.parent = parent.transform; attachTransformProperties(tag, xmlNode); go.name = "<" + xmlNode.Name + " " + tag.teardownName + ">"; go.transform.localRotation = Quaternion.Euler(tag.rotation); go.transform.localPosition = tag.position; if (tag.scale > 0) { go.transform.localScale = go.transform.localScale * tag.scale; } addToParent(parent, tag); } else if (xmlNode.Name == "environment") { TeardownEnvironment tag = go.AddComponent <TeardownEnvironment>(); go.transform.parent = parent.transform; attachGeneralProperties(tag, xmlNode); go.name = "<" + xmlNode.Name + " " + tag.teardownName + ">"; tag.skyboxrot = readFloat(xmlNode, "skyboxrot"); tag.sunBrightness = readFloat(xmlNode, "sunBrightness"); tag.sunFogScale = readFloat(xmlNode, "sunFogScale"); tag.sunDir = readVec3(xmlNode, "sunDir"); tag.sunColorTint = readColor(xmlNode, "sunColorTint"); addToParent(parent, tag); } else if (xmlNode.Name == "scene") { Scene tag = go.AddComponent <Scene>(); go.transform.parent = parent.transform; attachGeneralProperties(tag, xmlNode); go.name = "<" + xmlNode.Name + " " + tag.teardownName + ">"; tag.shadowVolume = readVec3(xmlNode, "shadowVolume"); tag.version = readInt(xmlNode, "version"); addToParent(parent, tag); } else if (xmlNode.Name == "boundary") { Boundary tag = go.AddComponent <Boundary>(); attachGeneralProperties(tag, xmlNode); go.transform.parent = parent.transform; go.name = "<" + xmlNode.Name + " " + tag.teardownName + ">"; addToParent(parent, tag); } else if (xmlNode.Name == "script") { Script tag = go.AddComponent <Script>(); attachGeneralProperties(tag, xmlNode); go.transform.parent = parent.transform; tag.file = readString(xmlNode, "file"); go.name = "<" + xmlNode.Name + " " + tag.teardownName + " " + tag.file + ">"; addToParent(parent, tag); } else if (xmlNode.Name == "instance") { Instance tag = go.AddComponent <Instance>(); go.transform.parent = parent.transform; attachGeneralProperties(tag, xmlNode); tag.file = readString(xmlNode, "file"); go.name = "<" + xmlNode.Name + " " + tag.teardownName + " " + tag.file + ">"; if (!fileName.Contains("create") && tag.file.StartsWith("LEVEL")) { Debug.LogError("Your source file has no create folder in its path but an instance is referencing the level folder. Cannot import!"); } else if (!tag.file.StartsWith("LEVEL")) { GameObject xmlRoot = readXML(tag.file); xmlRoot.GetComponentInChildren <Prefab>().gameObject.transform.transform.parent = go.transform; DestroyImmediate(xmlRoot); } else { GameObject xmlRoot = readXML(tag.file.Replace("LEVEL", getLevelFolder(fileName))); xmlRoot.GetComponentInChildren <Prefab>().gameObject.transform.transform.parent = go.transform; DestroyImmediate(xmlRoot); } go.transform.localRotation = Quaternion.Euler(tag.rotation); go.transform.localPosition = tag.position; if (tag.scale > 0) { go.transform.localScale = go.transform.localScale * tag.scale; } addToParent(parent, tag); } else if (xmlNode.Name == "prefab") { Prefab tag = go.AddComponent <Prefab>(); go.transform.parent = parent.transform; attachGameObjectProperties(tag, xmlNode); go.name = "<" + xmlNode.Name + " " + tag.teardownName + ">"; addToParent(parent, tag); } else if (xmlNode.Name == "vertex") { Vertex tag = go.AddComponent <Vertex>(); go.transform.parent = parent.transform; attachGeneralProperties(tag, xmlNode); tag.pos = readVec2(xmlNode, "pos"); go.name = "<" + xmlNode.Name + " " + tag.teardownName + ">"; go.transform.localPosition = new Vector3(tag.pos.x, 0, tag.pos.y); addToParent(parent, tag); } else if (xmlNode.Name == "light") { Light tag = go.AddComponent <Light>(); go.transform.parent = parent.transform; attachGeneralProperties(tag, xmlNode); attachTransformProperties(tag, xmlNode); go.name = "<" + xmlNode.Name + " " + tag.teardownName + ">"; tag.penumbra = readFloat(xmlNode, "penumbra"); tag.unshadowed = readFloat(xmlNode, "unshadowed"); tag.angle = readFloat(xmlNode, "angle"); tag.glare = readFloat(xmlNode, "glare"); tag.color = readColor(xmlNode, "color"); tag.type = (Light.Type)Enum.Parse(typeof(Light.Type), readString(xmlNode, "type", "area"), false); go.transform.localRotation = Quaternion.Euler(tag.rotation); go.transform.localPosition = tag.position; if (tag.scale > 0) { go.transform.localScale = go.transform.localScale * tag.scale; } addToParent(parent, tag); } foreach (XmlNode child in xmlNode.ChildNodes) { GameObject childGameObject = readXmlNode(child, go, fileName); } return(go); }