常用方法:这个方法是网上使用频率较高的,但是适用于unity2018以上,unity2017使用这个代码打包会报错
public Vector3 GetInspectorRotationValueMethod(Transform mTransform)
{
// 获取原生值
Type transformType = mTransform.GetType();
PropertyInfo m_propertyInfo_rotationOrder = transformType.GetProperty("rotationOrder", BindingFlags.Instance | BindingFlags.NonPublic);
object m_OldRotationOrder = m_propertyInfo_rotationOrder.GetValue(mTransform, null);
MethodInfo m_methodInfo_GetLocalEulerAngles = transformType.GetMethod("GetLocalEulerAngles", BindingFlags.Instance | BindingFlags.NonPublic);
object value = m_methodInfo_GetLocalEulerAngles.Invoke(mTransform, new object[] { m_OldRotationOrder });
//Debug.Log("反射调用GetLocalEulerAngles方法获得的值:" + value.ToString());
string temp = value.ToString();
//将字符串第一个和最后一个去掉
temp = temp.Remove(0, 1);
temp = temp.Remove(temp.Length - 1, 1);
//用‘,’号分割
string[] tempVector3;
tempVector3 = temp.Split(',');
//将分割好的数据传给Vector3
Vector3 vector3 = new Vector3(float.Parse(tempVector3[0]), float.Parse(tempVector3[1]), float.Parse(tempVector3[2]));
return vector3;
}
新的解决方案,不是最好的,但是适用于unity2017,且打包后不会报错:
private Vector3 GetInpectorEulers(Transform mTransform)
{
Vector3 angle = mTransform.eulerAngles;
float x = angle.x;
float y = angle.y;
float z = angle.z;
if (Vector3.Dot(mTransform.up, Vector3.up) >= 0f)
{
if (angle.x >= 0f && angle.x <= 90f)
{
x = angle.x;
}
if (angle.x >= 270f && angle.x <= 360f)
{
x = angle.x - 360f;
}
}
if (Vector3.Dot(mTransform.up, Vector3.up) < 0f)
{
if (angle.x >= 0f && angle.x <= 90f)
{
x = 180 - angle.x;
}
if (angle.x >= 270f && angle.x <= 360f)
{
x = 180 - angle.x;
}
}
if (angle.y > 180)
{
y = angle.y - 360f;
}
if (angle.z > 180)
{
z = angle.z - 360f;
}
Vector3 vector3 = new Vector3(Mathf.Round(x), Mathf.Round(y), Mathf.Round(z));
//Debug.Log(" Inspector Euler: " + Mathf.Round(x) + " , " + Mathf.Round(y) + " , " + Mathf.Round(z));
return vector3;
}