關(guān)于unity的log網(wǎng)上也有很多介紹,下面的只是使用的一點(diǎn)心得:
using UnityEngine;
using System.Collections;
public class TestLogger : MonoBehaviour {
public static int Test()
{
Debug.Log("t");
return 1;
}
public static void Log1(int o)
{
#if TestLog
Debug.Log(o);
#endif
}
[System.Diagnostics.Conditional("TestLog")]
public static void Log2(int o)
{
Debug.Log(o);
}
void Start () {
Log1(Test());
Log2(Test());
}
}
如果宏定義中加了TestLog,那么會(huì)輸出兩遍t和1,如果不加宏定義那么Log1執(zhí)行時(shí)會(huì)輸出一次t,而Log2不會(huì)執(zhí)行,這就是Conditional的好處。
還有一個(gè)是關(guān)于移進(jìn)移出Camera視域來達(dá)到隱藏顯示效果,下面是我在Camera為正交相機(jī)size為1且Camera在panel的(0,0,0)處所實(shí)現(xiàn)的代碼:
/// <summary>
/// 移進(jìn)移出顯示/隱藏
/// </summary>
/// <param name="trans">camera所在層級(jí)的panel.transform</param>
/// <param name="go">被操作的物體</param>
/// <param name="activeFlag">顯示/隱藏</param>
public static void ChangeActive(Transform trans,GameObject go,bool activeFlag)
{
if (!go.activeSelf)
{
go.SetActive(true);
}
float distance = Math.Abs(trans.position.x-go.transform.position.x);
if (distance<=Screen.width)
{
go.transform.position += new Vector3(Screen.width, 0, 0);
}
else
{
go.transform.position -= new Vector3(Screen.width, 0, 0);
}
}
雖然達(dá)到了效果,但是被大佬教育了,不同的坐標(biāo)系混用,而且不通用,應(yīng)該用viewport的位置,Camera有一些函數(shù)screenpointtoray之類的,要把Screen坐標(biāo)轉(zhuǎn)到World的,了解一些3D空間:模型空間、世界空間、投影空間、屏幕空間,看來以后得多學(xué)習(xí)一些圖形學(xué)基礎(chǔ)知識(shí)了==
還有一點(diǎn)就是通常把GetComponent的引用緩存起來而不是用到的時(shí)候再調(diào),這一點(diǎn)可以參考這個(gè)