Unity 小技巧积累

作者: zcwfeng | 来源:发表于2017-09-01 18:20 被阅读92次
  • 网络判断
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);

    }

相关文章

  • Unity 小技巧积累

    网络判断 挂载脚本后,有的脚本左侧有复选框,有的没有。原因是没有写Start函数。这种脚本启动不会自动调用。 Ed...

  • Unity优化技巧集合

    知乎作者:Mack Unity优化技巧(上)Unity优化技巧(中)Unity优化技巧(下)

  • [Unity] unity小技巧

    大家使用Unity的经验都有多长呢?Unity编辑器中还有这样的隐藏功能你知道吗?本系列文章为大家介绍Unity使...

  • unity小技巧

    1.同时按下Ctrl 键 和鼠标左键弹出预览窗口。 ​​来自新浪微博:雨松MOMO 2.被忽略的注释// TODO...

  • 平时积累小技巧

    1、style样式累加 2、自动在主轴上居中: margin:auto; 会让盒子在主轴自动居中 3、

  • 语文积累小技巧

    大多数孩子在平时学习过程中都忽略了“朗读”这个重要的环节。语文教育的老前辈,叶圣陶先生曾经说过:“吟咏的时候,对于...

  • Unity小技巧汇总

    一、纹理拉伸 解决圆角图形,拉伸变形的问题;可以将大图改成小图,减少包的大小。   我们期望的渲染效果(进度两端都...

  • Python基本数据类型

    Python补充02 Python小技巧 在这里列举一些我使用Python时积累的小技巧。这些技巧是我在使用Pyt...

  • Unity优化相关文章汇总

    Unity优化的工具#韩宇飞Unity优化技巧(上) 关于获取组件GetComponent的效率问题Unity 脚...

  • 【转】unity使用小技巧4

    前面给大家分享了三篇unity技巧,今天继续给大家分享第四篇unity技巧,希望可以帮到大家,欢迎补充~技巧一在f...

网友评论

    本文标题:Unity 小技巧积累

    本文链接:https://www.haomeiwen.com/subject/gidkjxtx.html