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);

    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容