Example #1
0
        /// <summary>Creates a primitive and attaches it to the model.</summary>
        /// <remarks>
        /// For <see cref="PrimitiveType.Cylinder"/> Z and Y axis will be swapped to make Z "the length". The primitive will
        /// have a collider of the appropriate type.
        /// </remarks>
        /// <param name="type">The type of the primitive.</param>
        /// <param name="meshScale">
        /// The scale to bring all the mesh vertices to. The scale is applied on the mesh, i.e. it's
        /// applied on the vertices, not the transform.
        /// </param>
        /// <param name="material">The material to use for the primitive.</param>
        /// <param name="parent">The parent transform to attach the primitive to.</param>
        /// <returns>The game object of the new primitive.</returns>
        /// <seealso href="https://docs.unity3d.com/ScriptReference/GameObject.CreatePrimitive.html">
        /// Unity3D: GameObject.CreatePrimitive</seealso>
        /// <seealso href="https://docs.unity3d.com/ScriptReference/Material.html">Unity3D: Material
        /// </seealso>
        public static GameObject CreatePrimitiveWithCollider(
            PrimitiveType type, Vector3 meshScale, Material material, Transform parent)
        {
            var primitive = GameObject.CreatePrimitive(type);

            Hierarchy.MoveToParent(primitive.transform, parent);
            primitive.GetComponent <Renderer>().material = material;

            // Make object's Z axis its length. For this rotate around X axis.
            var meshRotation =
                type == PrimitiveType.Cylinder ? Quaternion.Euler(90, 0, 0) : Quaternion.identity;

            TranslateMesh(primitive, rotation: meshRotation, scale: meshScale);
            return(primitive);
        }