Unity 小技巧積累

  • 網絡判斷
NetworkReachability網絡可達性
NetworkReachability.ReachableViaCarrierDataNetwork 通過運營商數據網絡可達
NetworkReachability.ReachableViaLocalAreaNetwork 通過局域網絡可達(wifi)

//判斷當前的網絡是否是wifi或者有線連接

if(Application.internetReachability != NetworkReachability.NotReachable){

    xxxx;

}
  • 掛載腳本后,有的腳本左側有復選框,有的沒有。原因是沒有寫Start函數。這種腳本啟動不會自動調用。

  • Editor settings 的mode設置
    最好設置成text類型而不是mix 二進制類型。因為多人開發有可能這里沖突,二進制導致無法解決查找二進制文件的沖突。

  • 常見的幾種聲明腳本屬性方式

    [AddComponentMenu("UI/Slidershow", 39)]          //添加菜單
    [ExecuteInEditMode]                             //編輯模式下可執行
    [DisallowMultipleComponent]                     //不可重復
    [RequireComponent(typeof(RectTransform))]       //依賴于RectTransform組件
  • 腳本處理順序
同一個物體上的腳本,后添加上去的腳本先執行
  • 平臺預處理
using UnityEngine;  
using System.Collections;  
  
public class Recompile : MonoBehaviour  
{  
  
    private string platform = string.Empty;  
    // Use this for initialization  
    void Start()  
    {  
        DebugPlatformMesaage();  
    }  
  
    void DebugPlatformMesaage()  
    {  
 
#if UNITY_EDITOR  
        platform = "hi,大家好,我是在unity編輯模式下";  
#elif UNITY_XBOX360  
       platform="hi,大家好,我在XBOX360平臺";  
#elif UNITY_IPHONE  
       platform="hi,大家好,我是IPHONE平臺";  
#elif UNITY_ANDROID  
       platform="hi,大家好,我是ANDROID平臺";  
#elif UNITY_STANDALONE_OSX  
       platform="hi,大家好,我是OSX平臺";  
#elif UNITY_STANDALONE_WIN  
       platform="hi,大家好,我是Windows平臺";  
#endif  
        Debug.Log("Current Platform:" + platform);  
    }  
} 
  • 避免不小心修改腳本的變量和設置
    如,項目里面聲明變量,這樣這些腳本的屬性值,就不會出現在面板中,隱藏掉
        [HideInInspector] public int stars;
    [HideInInspector] public bool goalAchieved = false;
    [HideInInspector]public static int maxLevels = 40;
    [HideInInspector]public static int maxPacks = 4;
    [HideInInspector]public static bool soundEnabled;
    [HideInInspector]public GameObject genericDialog;
    [HideInInspector]public static int hintCount;
    [HideInInspector]public static int rewardsCount = 0,rewardsLimit=3;
    [HideInInspector]public static int ADMOB = 1, CHARTBOOST=2;
  • GameManager
  • DontDestroy
  • Application
  • QualitySettings
  • SenceManager
    場景跳轉
SceneManager.LoadScene("model");  
  • 退出應用
  1. 彈出提示框退出
      void OnApplicationQuit(){
        GameManager.instance = null;
    }
  1. 退出
        void OnApplicationQuit(){
        GameManager.instance = null;
    }
  • 跳轉google play 分享
void ShareLink() {
        string bodyString = "";
        string subjectString = "New Android Game";
        //Refernece of AndroidJavaClass class for intent
        AndroidJavaClass intentClass = new AndroidJavaClass ("android.content.Intent");
        //Refernece of AndroidJavaObject class for intent
        AndroidJavaObject intentObject = new AndroidJavaObject ("android.content.Intent");
        //call setAction method of the Intent object created
        intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
        //set the type of sharing that is happening
        intentObject.Call<AndroidJavaObject>("setType", "text/plain");
        //add data to be passed to the other activity i.e., the data to be sent
        intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_SUBJECT"), subjectString);
        bodyString = "I play this new cool puzzle game - https://play.google.com/store/apps/details?id=" +packageName;
        intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"),bodyString);
        //get the current activity
        AndroidJavaClass unity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
        //start the activity by sending the intent data
        currentActivity.Call ("startActivity", intentObject);

    }

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容