Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


implicit operator Vector4

Converts a Vector3 to a Vector4.
Read time 2 minutesLast updated 4 days ago

Definition

  • Type: Operator
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule

implicit operator Vector4(Vector)

Converts a Vector3 to a Vector4.
public static implicit operator Vector4(Vector3 v)

Parameters

The Vector3 to convert.

Returns

Type

Description

Vector4

Remarks

Vector3s can be implicitly converted to Vector4 (w is set to zero in the result).

Examples

using UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour{ void Start() { // color as a Vector3 (red, green, blue) Vector3 color = new Vector3(1.0f, 0.25f, 0f); // This converts the Vector3 to a Vector4 with the w parameter set to 0 Vector4 colorWithOpacity = color; // set the opacity in the w parameter colorWithOpacity.w = 0.75f; }}

implicit operator Vector3(Vector)

Converts a Vector4 to a Vector3.
public static implicit operator Vector3(Vector4 v)

Parameters

The Vector4 to convert.

Returns

Type

Description

Vector3

Remarks

An implicit conversion of a Vector4 to a Vector3. The Vector4.w is discarded.

Examples

using UnityEngine;public class Example : MonoBehaviour{ void Start() { // Create and display a Vector4. Vector4 vector4; vector4 = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); Debug.Log("Vector4: " + vector4); // Convert the Vector4 into a Vector3. Vector3 vector3; vector3 = vector4; // The 4.0f is not copied into the Vector3. // Show (1.0f, 2.0f, 3.0f). Debug.Log("Vector3: " + vector3); }}

implicit operator Vector4(Vector)

Converts a Vector2 to a Vector4.
public static implicit operator Vector4(Vector2 v)

Parameters

The Vector2 to convert.

Returns

Type

Description

Vector4

Remarks

Vector2s can be implicitly converted to Vector4 (z and w are set to zero in the result).

Examples

using UnityEngine;using System.Collections.Generic;public class ExampleScript : MonoBehaviour{ void Start() { Renderer renderer = GetComponent<Renderer>(); // Shader vectors are always defined as Vector4. // The value here is converted to a Vector4 from a Vector2. renderer.material.SetVector("_SomeVariable", Vector2.one); }}

implicit operator Vector2(Vector)

Converts a Vector4 to a Vector2.
public static implicit operator Vector2(Vector4 v)

Parameters

The Vector4 to convert.

Returns

Type

Description

Vector2

Remarks

An implicit conversion of a Vector4 to a Vector2. Vector4.z and Vector4.w are discarded.

Examples

using UnityEngine;public class Example : MonoBehaviour{ void Start() { // Create and display a Vector4. Vector4 vector4; vector4 = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); Debug.Log("Vector4: " + vector4); // Convert the Vector4 into a Vector2. Vector2 vector2; vector2 = vector4; // The values 3.0f and 4.0f are not copied into the Vector2. // Show (1.0f, 2.0f). Debug.Log("Vector2: " + vector2); }}