Ejemplo n.º 1
0
        /// <summary>
        /// Set the visuals of <pararef name="partObject"/> to use <paramref name="meshDetails"/>.
        /// </summary>
        /// <param name="partObject">The part object</param>
        /// <param name="meshDetails">The mesh details.</param>
        /// <remarks>
        /// Adds multiple children to <paramref name="partObject"/> when <paramref name="meshDetails"/>
        /// contains multiple mesh objects.
        /// </remarks>
        protected virtual void SetMesh(ShapeComponent partObject, MeshCache.MeshDetails meshDetails)
        {
            // Clear all children as a hard reset.
            foreach (Transform child in partObject.GetComponentsInChildren <Transform>())
            {
                if (child.gameObject != partObject.gameObject)
                {
                    child.parent = null;
                    GameObject.Destroy(child.gameObject);
                }
            }

            // Add children for each mesh sub-sub-part.
            int subPartNumber = 0;

            foreach (Mesh mesh in meshDetails.FinalMeshes)
            {
                GameObject partMesh = new GameObject();
                partMesh.name = string.Format("sub-part{0}", subPartNumber);
                partMesh.transform.localPosition = meshDetails.LocalPosition;
                partMesh.transform.localRotation = meshDetails.LocalRotation;
                partMesh.transform.localScale    = meshDetails.LocalScale;

                MeshFilter filter = partMesh.AddComponent <MeshFilter>();
                filter.sharedMesh = mesh;

                MeshRenderer renderer = partMesh.AddComponent <MeshRenderer>();
                renderer.material       = meshDetails.Material;
                renderer.material.color = partObject.Colour;
                partMesh.transform.SetParent(partObject.transform, false);

                ++subPartNumber;
            }
        }