将Vector3转换为Vector2的最有效,最快的方法是什么?
铸件:
Vector2 vector2 = (Vector2)vector3;
初始化一个新的Vector2:
Vector2 vector2 = new Vector2(vector3.x, vector3.y);
还是我不知道另一种方法?
将Vector3转换为Vector2的最有效,最快的方法是什么?
铸件:
Vector2 vector2 = (Vector2)vector3;
初始化一个新的Vector2:
Vector2 vector2 = new Vector2(vector3.x, vector3.y);
还是我不知道另一种方法?
Answers:
Vector3 v3 = Vector3.one;
Vector2 v2 = v3;
Vector3可以隐式转换为Vector2(z被丢弃)。
http://docs.unity3d.com/ScriptReference/Vector2-operator_Vector3.html
如果必须进行大量转换,则可能必须更改使用向量的方式。进行两项测试,并对它们进行计时,以查看哪种最适合您。
测试更新: 由于您询问了哪一个最快,因此我在Unity中创建了一个运行10000000次转换的测试。在这种情况下,似乎初始化版本最快。但是,您应该始终使用适合自己环境的测试,因此我建议您在游戏中运行自己的测试。
TestConvertByOperation 10000000实例:0.2714049s
TestConvertByCasting 10000000实例:0.286027s
TestConvertByInitializing千万实例:0.1458781s
using UnityEngine;
public class TestVector3Conversion : MonoBehaviour
{
readonly int iterations = 10000000;
Vector3 testVector = new Vector3(3f, 14f, 42f);
void Start()
{
Debug.Log(string.Format("TestConvertByOperation {0} instances: {1}s", iterations, TestConvertByOperation()));
Debug.Log(string.Format("TestConvertByCasting {0} instances: {1}s", iterations, TestConvertByCasting()));
Debug.Log(string.Format("TestConvertByInitializing {0} instances: {1}s", iterations, TestConvertByInitializing()));
}
float TestConvertByOperation()
{
var timeStart = Time.realtimeSinceStartup;
for (int i = 0; i < iterations; i++)
{
Vector2 v2 = testVector;
}
return Time.realtimeSinceStartup - timeStart;
}
float TestConvertByCasting()
{
var timeStart = Time.realtimeSinceStartup;
for (int i = 0; i < iterations; i++)
{
Vector2 v2 = (Vector2)testVector;
}
return Time.realtimeSinceStartup - timeStart;
}
float TestConvertByInitializing()
{
var timeStart = Time.realtimeSinceStartup;
for (int i = 0; i < iterations; i++)
{
Vector2 v2 = new Vector2(testVector.x, testVector.y);
}
return Time.realtimeSinceStartup - timeStart;
}
}
y
。将a转换Vector3
为a时Vector2
,几乎总是需要x
and z
,而不是x
and y
。
根据我的测试,最好的方法是自己手动分配其值。
Vector2 vector2;
vector2.x = vector3.x;
vector2.y = vector3.y;
这是我从Mattias扩展过来的结果。
TestConvertByOperation 10000000实例:0.3220527s
TestConvertByCasting 10000000实例:0.3226218s
TestConvertBy初始化10000000个实例:0.1916729s
TestConvertByManualAssign 10000000实例:0.09500527s
using UnityEngine;
namespace myTest
{
public class test: MonoBehaviour
{
readonly int iterations = 10000000;
Vector3 testVector = new Vector3(3f, 14f, 42f);
void Start()
{
Debug.Log(string.Format("TestConvertByOperation {0} instances: {1}s", iterations, TestConvertByOperation()));
Debug.Log(string.Format("TestConvertByCasting {0} instances: {1}s", iterations, TestConvertByCasting()));
Debug.Log(string.Format("TestConvertByInitializing {0} instances: {1}s", iterations, TestConvertByInitializing()));
Debug.Log(string.Format("TestConvertByManualAssign {0} instances: {1}s", iterations, TestConvertByManualAssign()));
}
float TestConvertByOperation()
{
var timeStart = Time.realtimeSinceStartup;
Vector2 v2;
for (int i = 0; i < iterations; i++)
{
v2 = testVector;
}
return Time.realtimeSinceStartup - timeStart;
}
float TestConvertByCasting()
{
var timeStart = Time.realtimeSinceStartup;
Vector2 v2;
for (int i = 0; i < iterations; i++)
{
v2 = (Vector2)testVector;
}
return Time.realtimeSinceStartup - timeStart;
}
float TestConvertByInitializing()
{
var timeStart = Time.realtimeSinceStartup;
Vector2 v2;
for (int i = 0; i < iterations; i++)
{
v2 = new Vector2(testVector.x, testVector.y);
}
return Time.realtimeSinceStartup - timeStart;
}
float TestConvertByManualAssign()
{
var timeStart = Time.realtimeSinceStartup;
Vector2 v2;
for (int i = 0; i < iterations; i++)
{
v2.x = testVector.x;
v2.y = testVector.y;
}
return Time.realtimeSinceStartup - timeStart;
}
}
}
请注意,我使用统一版本5.6.5进行了测试