MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / Cast

Method Cast

Source/Engine/Utilities/VariantUtils.cs:32–96  ·  view source on GitHub ↗

Casts the generic value to a given type. Matches native Variant casting logic that favors returning null/zero value rather than error. The destination value type. The input value to cast. The result value.

(object value)

Source from the content-addressed store, hash-verified

30 /// <param name="value">The input value to cast.</param>
31 /// <returns>The result value.</returns>
32 internal static T Cast<T>(object value)
33 {
34 if (value == null)
35 return default;
36 var type = value.GetType();
37 if (type != typeof(T) && !typeof(T).IsAssignableFrom(type))
38 {
39 if (typeof(T) == typeof(Vector2))
40 {
41 if (value is Float2 asFloat2)
42 return (T)(object)new Vector2((Real)asFloat2.X, (Real)asFloat2.Y);
43 if (value is Float3 asFloat3)
44 return (T)(object)new Vector2((Real)asFloat3.X, (Real)asFloat3.Y);
45 if (value is Float4 asFloat4)
46 return (T)(object)new Vector2((Real)asFloat4.X, (Real)asFloat4.Y);
47 }
48 else if (typeof(T) == typeof(Vector3))
49 {
50 if (value is Float2 asFloat2)
51 return (T)(object)new Vector3((Real)asFloat2.X, (Real)asFloat2.Y, (Real)0);
52 if (value is Float3 asFloat3)
53 return (T)(object)new Vector3((Real)asFloat3.X, (Real)asFloat3.Y, (Real)asFloat3.Z);
54 if (value is Float4 asFloat4)
55 return (T)(object)new Vector3((Real)asFloat4.X, (Real)asFloat4.Y, (Real)asFloat4.Z);
56 }
57 else if (typeof(T) == typeof(Vector4))
58 {
59 if (value is Float2 asFloat2)
60 return (T)(object)new Vector4((Real)asFloat2.X, (Real)asFloat2.Y, (Real)0, (Real)0);
61 if (value is Float3 asFloat3)
62 return (T)(object)new Vector4((Real)asFloat3.X, (Real)asFloat3.Y, (Real)asFloat3.Z, (Real)0);
63 if (value is Vector4 asFloat4)
64 return (T)(object)new Vector4((Real)asFloat4.X, (Real)asFloat4.Y, (Real)asFloat4.Z, (Real)asFloat4.W);
65 }
66 else if (typeof(T) == typeof(Float2))
67 {
68 if (value is Vector2 asVector2)
69 return (T)(object)new Float2((float)asVector2.X, (float)asVector2.Y);
70 if (value is Vector3 asVector3)
71 return (T)(object)new Float2((float)asVector3.X, (float)asVector3.Y);
72 if (value is Vector4 asVector4)
73 return (T)(object)new Float2((float)asVector4.X, (float)asVector4.Y);
74 }
75 else if (typeof(T) == typeof(Float3))
76 {
77 if (value is Vector2 asVector2)
78 return (T)(object)new Float3((float)asVector2.X, (float)asVector2.Y, (float)0);
79 if (value is Vector3 asVector3)
80 return (T)(object)new Float3((float)asVector3.X, (float)asVector3.Y, (float)asVector3.Z);
81 if (value is Vector4 asFloat4)
82 return (T)(object)new Float3((float)asFloat4.X, (float)asFloat4.Y, (float)asFloat4.Z);
83 }
84 else if (typeof(T) == typeof(Float4))
85 {
86 if (value is Vector2 asVector2)
87 return (T)(object)new Float4((float)asVector2.X, (float)asVector2.Y, (float)0, (float)0);
88 if (value is Vector3 asVector3)
89 return (T)(object)new Float4((float)asVector3.X, (float)asVector3.Y, (float)asVector3.Z, (float)0);

Callers

nothing calls this directly

Calls 2

GetTypeMethod · 0.45
IsAssignableFromMethod · 0.45

Tested by

no test coverage detected