PersistentTrails.CraftLoader.assembleCraft C# (CSharp) Method

assembleCraft() public static method

public static assembleCraft ( string craftName, bool collidersOn ) : GameObject
craftName string
collidersOn bool
return UnityEngine.GameObject
        public static GameObject assembleCraft(string craftName, bool collidersOn)
        {
            GameObject craft = new GameObject();
            Utilities.debug.debugMessage("asembling craft " + craftName);
            List<PartValue> pvList;
            //List<PartValue> pvList = getParts(FlightGlobals.ActiveVessel, true); // load the craft file here into a partValue list
            try
            {
                pvList = loadCraftFromFile(craftName);
            }
            catch
            {
                throw new FileNotFoundException("error loading craft from file", craftName);
            }
            foreach (PartValue pv in pvList)
            {
                //Debug.Log("pv.partName is " + pv.partName);
                pv.model.SetActive(true);
                //Debug.Log("pv.model exists");
                pv.model.transform.parent = craft.transform;
                pv.model.transform.localPosition = pv.position;
                pv.model.transform.localRotation = pv.rotation;
                if (pv.scale > 7f) pv.scale /= 10f;
                if (pv.scale > 7f) pv.scale /= 10f; // twice to catch both 0.01 scale parts, and 0.1 scales. Gotta find a better way. Need to read the part cfg scale
                pv.model.transform.localScale = new Vector3(pv.scale, pv.scale, pv.scale);
                //Debug.Log("Part: " + pv.partName + "Scale: " + pv.scale + "/" + pv.model.transform.localScale);
                //Debug.Log("Part: " + pv.position);
                //Debug.Log("Part: " + pv.rotation);
                //Debug.Log("Part: " + pv.scale);
            }
            setColliderStateInChildren(craft, collidersOn);
            setLightStateInChildren(craft, false);
            return craft;
        }

Usage Example

        public ReplayWindow(Track track) : base("Replay Track: " + track.TrackName)
        {
            bool loadCraft = true;

            if (loadCraft)
            {
                try
                {
                    ghost = CraftLoader.assembleCraft(Utilities.CraftPath + track.VesselName + ".crf", track.ReplayColliders); // --- add the craft file listed in the path, or selected from a menu ---
                }
                catch
                {
                    //Debug.Log("ERROR LOADING CRF, FALLING BACK TO SPHERE");
                    loadCraft = false;
                }
            }

            if (!loadCraft)
            {
                Mesh sphere = MeshFactory.createSphere();
                ghost = MeshFactory.makeMeshGameObject(ref sphere, "Track playback sphere");
                ghost.transform.localScale = new Vector3(track.ConeRadiusToLineWidthFactor * track.LineWidth, track.ConeRadiusToLineWidthFactor * track.LineWidth, track.ConeRadiusToLineWidthFactor * track.LineWidth);
                //ghost.collider.enabled = false;
                ghost.GetComponent <Renderer>().material = new Material(Shader.Find("KSP/Emissive/Diffuse"));
                ghost.GetComponent <Renderer>().material.SetColor("_EmissiveColor", track.LineColor);
            }

            behaviour = ghost.AddComponent <ReplayBehaviour>();
            behaviour.initialize(track, ghost);
            behaviour.enabled = true;

            this.windowPos = new Rect(600f, 50f, 300f, 100f);
        }