我试图复现一篇文章里的使unity里的物体指定角度旋转,但是我导入了脚本后,回到unity页面就会报错
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
/// <summary>
/// 获取到旋转的正确数值
/// </summary>
/// <param name="transform"></param>
/// <returns></returns>
public Vector3 GetInspectorRotationValueMethod(Transform transform)
{
// 获取原生值
System.Type transformType = transform.GetType();
PropertyInfo m_propertyInfo_rotationOrder = transformType.GetProperty("rotationOrder", BindingFlags.Instance | BindingFlags.NonPublic);
object m_OldRotationOrder = m_propertyInfo_rotationOrder.GetValue(transform, null);
MethodInfo m_methodInfo_GetLocalEulerAngles = transformType.GetMethod("GetLocalEulerAngles", BindingFlags.Instance | BindingFlags.NonPublic);
object value = m_methodInfo_GetLocalEulerAngles.Invoke(transform, new object[] { m_OldRotationOrder });
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;
}
回到unity页面后报错为Assets\NewBehaviourScript.cs(11,1): error CS0106: The modifier 'public' is not valid for this item
通过询问原文作者,他的运行是没有问题的,同时我也只是新建了cube,甚至没有绑定,如果程序也没有错误,那么是哪里有问题呢