2016年12月13日 星期二

Unity_使用PlayerPrefs



使用PlayerPrefs(內容儲存於app)


可以想成APP自己有一個儲存資料的空間,可以用來存取一些簡單的資料
例如 設定值 畫質 影子 ...等遊戲常用設置
找不到資料就會用設定值

注意在行動平台上,清除快取會造成PlayerPrefs資料消失。

        private void Save()
        {
            PlayerPrefs.SetInt("Key1", 1);
            PlayerPrefs.SetString("Key2", "Steven");
            PlayerPrefs.SetFloat("key3", 5.5f);
        }

        private void Load()
        {
            int nInt = PlayerPrefs.GetInt("Key1");
            string sString = PlayerPrefs.GetString("Key2");
            float fNum = PlayerPrefs.GetFloat("Key3");

            Debug.Log("nInt: " + nInt.ToString() + ", sString: " + sString + ", fNum: " + fNum.ToString());
        }



刪除指令: PlayerPrefs.DeleteAll();

Unity_StartCoroutine使用



StartCoroutine的使用
簡單說就像是額外開一個Thread(但其實還是在主Thread裡面),不會影響到主線程的進度

傳回值設成IEnumerator就可以被使用




延遲三秒(實用)(放在回傳值函數是IEnumerator的方法中):
yield return new WaitForSeconds(3);


等待畫面的禎數都跑完後再執行
yield return new WaitForEndOfFrame ();


停止xxxCoroutine

StopCoroutine("DoSomething");
   
停止全部Coroutine
StopAllCoroutines();

Unity_Button(按鈕)



Button事件

第一種寫法(這種手機可以直接判斷的到)
使用介面寫引用事件:
(用這方法只能運用到點擊)
1.建立一個Script,寫一個方法
2.將該Script放入某個Game Object(或是直接從UI中建立一個Event System
3.套用Standalone Input Module這個Component(Force Module Active要勾選
4.ButtonOn Click套用剛剛放入的Game Object和從Click中選擇要使用的方法

第二種寫法(這種手機可以用進入Button和離開Button範圍的方式來達成長按應用)
使用事件系統來控制:
(可以控制到:進入Button範圍、點擊、離開Button範圍)
這個功能寫在要運用的物件下(例如Button物件下綁一個Script),不要想說寫在背景物件再判斷
IPointerClickHandler - OnPointerClick 點擊。
IPointerEnterHandler - OnPointerEnter
進入。
IPointerExitHandler - OnPointerExit
離開。
IPointerDownHandler - OnPointerDown
按下。
IPointerUpHandler - OnPointerUp
彈起。
IDragHandler - OnDrag 拖動。
寫個Script,Button直接套用及可
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;// 使用事件系統
 
public class Ugui_test_Event : MonoBehaviour, IPointerEnterHandler 
,IPointerClickHandler ,IPointerExitHandler
{
 
    public void OnPointerEnter (PointerEventData eventData) 
    {
        Debug.Log ("Enter ###.");
    }
    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log ("Click ###.");
    }
    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log ("Exit ###.");
    }
 
}

隱藏寫法:使用Rx