Unity3D中的小知识点杂记

1:方法

1.1 Quaternion.Euler

欧拉角旋转,按照Z,X,Y的顺序旋转

1.2 Quaternion.Angle

static float Angle(Quaternion a, Quaternion b);
获得两个四元数之间的夹角,结果是两个z轴之间的夹角

1.3 Quaternion.AngleAxis

static Quaternion AngleAxis(float angle, Vector3 axis);
经过测试,为某个物体设置围绕具体axis旋转的角度后,其余轴的旋转值会被设置为0.即使物体原始状态下其余轴的旋转值不为0.
angle可以设置为任意值,为负值时,表现在inspector面板上的物体旋转角度会被换算为正值(如-5会被换算为355)。若angle超过+-360°,也会被换算为[0,360)

1.4 Object.FindObjectsOfType

static Object[] FindObjectsOfType(Type type);
Returns a list of all active loaded objects of Type type.
//这个函数是递归的,可以获得子物体。获得的是整个场景中某种类型的物体,若物体上有某个脚本(如abc.cs),通过GameObject.FindObjectsOfType(typeof(abc)) 也可以获得到改物体

1.5 获得当前场景名称

编辑器模式下:Path.GetFileName(EditorApplication.currentScene)
EditorApplication.currentScene获得的是诸如Assets/XXX/XXX.unity这种路径。使用Path.GetFileName获得名称和后缀.unity
运行模式下:Application.loadedLevelName获得的只是场景名称,无后缀

1.6 获得机器唯一标识符

SystemInfo.deviceUniqueIdentifier
这个对于ios设备,ios7之前获取的才是唯一的,具体参见文档。
SystemInfo可以获得很多关于设备的参数信息。

3:清空console面板的输出

以下代码对4.3.4有效
public static void ClearLog()
{
var assembly = Assembly.GetAssembly(typeof(UnityEditor.ActiveEditorTracker));//注意Assembly需要使用using System.Reflection
var type = assembly.GetType(“UnityEditorInternal.LogEntries”);
var method = type.GetMethod(“Clear”);
method.Invoke(new object(), null);
}
适用于运行时、适用于Editor
http://answers.unity3d.com/questions/10580/editor-script-how-to-clear-the-console-output-wind.html

平台依赖编译

https://docs.unity3d.com/Documentation/Manual/PlatformDependentCompilation.html
结合#if #elif #else #endif及||使用
运行时平台
RuntimePlatform
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)

Rich Text

Unity Manual->Advanced 支持对GUI中的字体和Debug中的字符,支持的tag有

通过代码执行菜单项

EditorApplication.ExecuteMenuItem(“GameObject/Create Other/Cube”)

Color

程序中Color范围为[0,1]

碰撞设置:

1,通过Edit->ProjectSettings->Physics
来设置全局的碰撞矩阵。
代码中也可以使用Physics.IgnoreLayerCollision来设置具体两个layer之间的碰撞关系。
static void IgnoreLayerCollision(int layer1, int layer2, bool ignore = true);
如:
Physics.IgnoreLayerCollision(9, 12, true);
这里的int型的layer1,layer2对应的就是Layer的Index,不需要进行位运算

忽略两个collider之间的碰撞,可以使用方法Physics.IgnoreCollision()

Editor模式下,运行,Update堵塞

void Update()
{
    Debug.Log(Time.time);
}

打印结果:
"Update打印结果"

这也是为什么在编辑器模式下写诸如transform.translate之类的移动函数的时候,一开始会出现跳跃的原因了。

解决方法,默认obj为不可见,程序中通过设置obj的Active为true,可以解决。
另外可以使用FixedUpdate

Awake执行还是不执行?

脚本附着到gameobject上。
当脚本的enable为false,gameobject active为true,运行游戏后,脚本的awake函数依旧执行。
当脚本的enable为true,gameobject active为false,运行游戏后,脚本的awake函数不执行。