天天看點

Application類

1.四 個資料檔案路徑

void Start()
{
    //4 個資料檔案路徑
    Debug.Log("_Application_DataPath.cs:");

    //傳回程式的資料檔案所在檔案夾的相對路徑(隻讀)
    Debug.Log("dataPath: " + Application.dataPath.ToString());

    //傳回一個持久化資料存儲目錄的相對路徑(隻讀)
    Debug.Log("persistentDataPath: " + Application.persistentDataPath.ToString());

    //傳回流資料的緩存目錄的相對路徑(隻讀)
    Debug.Log("streamingAssetsPath: " + Application.streamingAssetsPath.ToString());

    //傳回一個臨時資料的緩存目錄的相對路徑(隻讀)
    Debug.Log("temporaryCachePath: " + Application.temporaryCachePath.ToString());
}           

2.關卡

void Start()
{
    Debug.Log("_Application_Level.cs:");

    //傳回目前場景索引值
    Debug.Log("loadedLevel: " + Application.loadedLevel.ToString());

    //傳回目前場景的名字
    Debug.Log("loadedLevelName: " + Application.loadedLevelName.ToString());

    //傳回可被加載場景的數量
    Debug.Log("levelCount: " + Application.levelCount.ToString());

    //加載關卡銷毀所有物體
    Application.LoadLevel(0);

    //Application.LoadLevelAsync異步加載關卡, 将新關卡加載到目前場景
    Application.LoadLevelAsync(0);

    //加載新的場景,但目前的場景不會被銷毀。
    Application.LoadLevelAdditive(0);

    //在背景異步累加關卡,也就是說在背景非同步加載新的場景,但目前的場景不會被銷毀。
    Application.LoadLevelAdditiveAsync(0);

    /*
        網絡流媒體播放器 (Streaming Web Player) 允許場景 0 (Scene 0) 完成加載後立刻開始播放網頁遊戲。如果遊戲有 10 個關卡,開始播放第 1 個關卡前讓播放器等待并下載下傳第 2-10 個關卡的所有資源并沒有太大意義。釋出網絡流媒體播放器(Streaming Web Player) 時,必須下載下傳的資源将按照它們在場景 (Scene) 檔案中的順序來進行排列。場景 0 (Scene 0) 中包含的所有資源下載下傳完成後,網絡播放器 (Streaming Web Player) 開始播放。
        簡言之,網絡流媒體播放器 (Streaming Web Player) 将讓玩家玩遊戲的速度比以前更快。
        唯一需要做的事就是檢查,確定想要加載的下個關卡在加載前已完成流式處理。
        一般來說,在非流媒體播放器中,使用者使用下列代碼加載關卡:
        Application.LoadLevel("levelName");
        在網絡流媒體播放器 (Streaming Web Player) 中,必須先檢查關卡是否完成流式處理。該步驟通過 CanStreamedLevelBeLoaded() 函數完成。
        */
    Application.CanStreamedLevelBeLoaded(0);

    //如果想要向玩家顯示關卡資料流的進展情況,如做一個進度條或其他表現形式,可通路 GetStreamProgressForLevel()進一步了解。
    Application.GetStreamProgressForLevel(0);

    //解除安裝與給定的場景相關的所有對象
    Application.UnloadLevel(0);
}           

3.截屏

//U3D自帶的截圖,有點卡。此方法在web模式下無效。每幀隻執行一次,同一幀中隻有最後一次調用才生效。
Application.CaptureScreenshot(filename);
//網上的
void GetCapture()
{
        RenderTexture renderTex = Camera.main.targetTexture;
        int width = renderTex.width;
        int height = renderTex.height;
        Texture2D tex = new Texture2D(renderTex.width, renderTex.height, TextureFormat.ARGB32, false);
        tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        tex.Compress(false);//對螢幕緩存進行壓縮
        RenderTexture.active = renderTex;
        tex.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
        tex.Apply();
        RenderTexture.active = null;
        byte[] imagebytes = tex.EncodeToPNG();//轉化為png圖
        File.WriteAllBytes(Application.dataPath + "/screencapture.png", imagebytes);//存儲png圖
}           

4.系統資訊,程式資訊,U3D資訊

//傳回目前運作平台
Debug.Log("platform: " + Application.platform.ToString());

//目前運作平台是否為一個已知的控制台平台
Debug.Log("isConsolePlatform:" + Application.isConsolePlatform.ToString());

//目前運作平台是否為一個已知的移動平台
Debug.Log("isMobilePlatform:" + Application.isMobilePlatform.ToString());

//傳回目前場景是否在運作
Debug.Log("isPlaying: " + Application.isPlaying.ToString());

//傳回目前場景是否處于UNITY編輯模拟
Debug.Log("isEditor: " + Application.isEditor.ToString());

//公司名字。。。
Debug.Log("companyName:" + Application.companyName.ToString());

//産品名字。。。
Debug.Log("productName:" + Application.productName.ToString());

//應用程式建立後被改變則傳回true,否則傳回false。  被改變指的是什麼。。。。
//Application.genuineCheckAvailable 應與 Application.genuine 一并使用,以确認應用程式的完整性可得到實際驗證。通路 Application.genuine 屬性是一項相當昂貴的操作,不應用于幀更新期間或其他時序要求嚴格的代碼。
Debug.Log("genuine:" + Application.genuine.ToString());
Debug.Log("genuineCheckAvailable:" + Application.genuineCheckAvailable.ToString());

//是否激活U3D
Debug.Log("HasProLicense" + Application.HasProLicense().ToString());

// 應用安裝方式
/*  Unknown                     應用程式安裝模式未知。
    Application.Store           通過網上商店安裝的應用。
    Application.DeveloperBuild  應用程式安裝,通過開發者模式。
    Application.Adhoc           通過臨時分布安裝的應用。  好像是通過廣告還是什麼鬼。。
    Application.Enterprise      應用程式安裝,通過企業分布。
    Application.Editor          應用程式運作在編輯模式。
    */
Debug.Log("installMode" + Application.installMode.ToString());

// 網絡連接配接方式
/* NotReachable                     無網絡連接配接
    ReachableViaCarrierDataNetwork  網絡通過營運商資料網絡
    ReachableViaLocalAreaNetwork        網絡通過WiFi或電纜
    */
Debug.Log("internetReachability" + Application.internetReachability.ToString());

//打開一個網站
Application.OpenURL("www.baidu.com");

//結束程式
Application.Quit();

//當應用程式在背景運作時,該應用程式應該運作嗎?
Debug.Log("runInBackground" + Application.runInBackground.ToString());

//  沙盒類型。
/*  Unknown         未知
    NotSandboxed    不運作在沙盒環境中
    Sandboxed       運作在沙盒環境中
    SandboxBroken   運作在破碎的沙盒環境中,  這又是什麼鬼
*/
Debug.Log("sandboxType" + Application.sandboxType.ToString());

//  堆棧跟蹤日志記錄選項。
/*
    None        沒有堆棧跟蹤将輸出到日志。
    ScriptOnly  隻有管理堆棧跟蹤将輸出。
    Full        本地和托管堆棧跟蹤将被記錄。
    */
Debug.Log("stackTraceLogType:" + Application.stackTraceLogType.ToString());

//系統語言
Debug.Log("systemLanguage:" + Application.systemLanguage.ToString());

/*  訓示遊戲在指定的幀速率下進行渲染。
    設定targetFrameRate為-1(預設值)使單機遊戲盡可能快的渲染且網頁遊戲呈現在50-60幀/秒,取決于平台。
    注意,設定targetFrameRate不保證幀速率。由于平台的詳情,可能會有波動,或遊戲可能無法達到幀速率,因為電腦太慢。
    如果垂直同步設定品質目标幀率設定,是不容忽視的,和VBLANK間隔代替。在qualitysettings的vblankcount屬性可以被用來限制幀率的螢幕重新整理率(每秒60幀畫面可以通過設定vblankcount為2限30 fps)
    在編輯器中targetFrameRate被忽略。
    */
Debug.Log("targetFrameRate" + Application.targetFrameRate.ToString());

//U3D版本
Debug.Log("unityVersion" + Application.unityVersion);

//應用程式版本
Debug.Log("version" + Application.version);

//此方法用于注冊一個委托來調用日志信
//Application.logMessageReceived += null;
//Application.logMessageReceivedThreaded += null;
//Application.logMessageReceived += null
//日志的輸出資訊
string outPut = "";
//堆棧跟蹤資訊
string stack = "";
//日志類型
string logType = "";

int tp = 0;
void Update()
{
    if (tp == 0)
    {
        Debug.Log("outPut: " + outPut);
        Debug.Log("stack: " + stack);
        Debug.Log("logType: " + logType);
        Debug.Log("tp: " + tp.ToString());
    }
}

void OnEnable()
{
    Application.logMessageReceived += MyCallBack;
}

void OnDisable()
{
    Application.logMessageReceived -= MyCallBack;
}

void MyCallBack(string logString, string stackStrace, LogType type)
{
    outPut = logString;
    stack = stackStrace;
    logType = type.ToString();
}

//應用程式版本
Debug.Log("version" + Application.version);

//獨特的雲項目辨別。每一個項目都是獨一無二的
Debug.Log("cloudProjectId" + Application.cloudProjectId.ToString());

//在運作時傳回應用程式包辨別符。
Debug.Log("bundleIdentifier" + Application.bundleIdentifier.ToString());

//背景加載線程優先級。
Debug.Log("backgroundLoadingPriority" + Application.backgroundLoadingPriority.ToString());

//CancelQuit用法, Application.isShowingSplashScreen
public float showSplashTimeout = 2.0F;
private bool allowQuitting = false;
void Awake()
{
    DontDestroyOnLoad(this.gameObject);
}
void OnApplicationQuit()
{
    if (Application.loadedLevelName.ToLower() != "finalsplash")
        StartCoroutine("DelayedQuit");

    if (!allowQuitting)
        Application.CancelQuit();
}
IEnumerator DelayedQuit()
{
    Application.LoadLevel("finalsplash");
    yield return new WaitForSeconds(showSplashTimeout);
    allowQuitting = true;
    Application.Quit();
}           

5.WebPlayer

//application.absoluteurl和application.srcvalue讓你發現如果你的unityweb資料檔案被移動到另一個位置或是被連結到其它地方。你可能想保護你的資料檔案以防止盜版。
Debug.Log("absoluteURL" + Application.absoluteURL.ToString());
Debug.Log("srcValue" + Application.srcValue.ToString());

//在網頁中調用一個函數。
Application.ExternalCall(functionName, params);

//調用網頁中的腳本函數。
Application.ExternalEval(script.functionName);

//檢查使用者是否已授權使用網絡攝像頭或麥克風。
Application.HasUserAuthorization(UserAuthorization.Microphone | UserAuthorization.WebCam);

//我們從主網絡流下載下傳了多少位元組(隻讀)。
Debug.Log("streamedBytes" + Application.streamedBytes.ToString());

//訓示是否webplayer的安全模式啟用。
Debug.Log("webSecurityEnabled" + Application.webSecurityEnabled.ToString());

//不知道什麼鬼。。。
Debug.Log("webSecurityHostUrl" + Application.webSecurityHostUrl.ToString());           

————>U3D版本為5.2.1f1

繼續閱讀