Search

㊗ 新的一年,祝福讀者們 2020 新年新希望!! ◆ Welcome to Laird Studio! 歡迎蒞臨萊爾德工作室! ◎ Android Studio 基礎教學籌備中,敬請期待! ☏ 對網站有任何問題或建議,都非常歡迎使用留言板或至 Facebook 粉絲團發訊息,讓我們知道您的想法 (੭ु´ ᐜ `)੭ु

2016年7月11日 星期一

[ 客座文章 ] [ Unity ] 屬於工程師的專用工具箱




◆ 此篇客座文章由 ARKAI Studio 投稿,介紹各種有關 Unity 的實用工具,幫助讀者們學習如何更有效率的利用 Unity 撰寫程式,敬請大家多加參考利用!









屬於工程師的專用工具箱(一):便捷工具類別(Utility.cs)

2016-03-25  k79k06k02k  Tool, Unity

原文出處:http://wp.me/p7hYyg-2f



在製作遊戲中,一定會有一些非常常用的程式碼,像是建立物件(Instantiate)、讀取資料(Resources.Load)…..等等,通常會把這些常用語法集中在一個工具類別中,並把類別中的方法宣告為Static,供別的類別、專案使用,會讓整個語法更加簡潔易讀






分類

大致上可以分成幾類

系統

安全

UGUI

GameObject

Transform

Sprite

Time

Format

資源讀取

其他



工具類別(Utility.cs)

以下是筆者累積的語法,往後會繼續更新,用Struct做大分類



呼叫方式

Utility.[分類].[方法]

Utility.GameObjectRelate.ClearChildren(gameObject.transform);


程式碼範例 ( From GitHub )



後記

雖然不是很完善,但是程式是需要不斷累積的,也歡迎各位使用留言提供更多好用的語法,我會在更新上去,打造屬於我們大家的工具吧 !!


另外推薦一個Unity寶庫,就是有名的 Unity Wiki,裡面有許多Scripts、Shaders、Particles、小工具…..等等資源,有空一定要前往看看~~







屬於工程師的專用工具箱(二):常用工具(Singleton、Debug過濾器)

2016-03-25  k79k06k02k  Tool, Unity

原文出處:http://wp.me/p7hYyg-2P



這次要跟大家分享Singleton、Debug Filter工具,Singleton是資料共享工具,能快速的存取、管理跨Script的資料,Debug Filter是 Debug 訊息過濾器,在遊戲上線後一定會想關閉所有的Debug 訊息,以提升遊戲品質與效能



Singleton

在遊戲中一定會有共用資料,而這些資料想要在多個 Scripts 中去做控制、修改,這時我們通常會使用Singleton(單例模式),簡單來就是使用一個 Static 的類別做控管資料,進而方便的讀取共享資料。



Singleton 工具

Singleton.cs   (C#)

/**********************************************************
// Author   : K.(k79k06k02k)
// FileName : Singleton.cs
// Reference: http://wiki.unity3d.com/index.php/Singleton
**********************************************************/

public class Singleton where T : class,new()
{
    private static T _instance;

    private static object _lock = new object();

    public static T Instance
    {
        get
        {
            //多執行緒
            lock (_lock)
            {
                if (_instance == null)
                {
                    _instance = new T();
                }

                return _instance;
            }
        }
    }
}



實作方式(類別直接繼承)

TestClass.cs   (C#)

using UnityEngine;

public class TestClass : Singleton
{
    public void Hello()
    {
        Debug.Log("Hello");
    }
}



呼叫方式

C#

TestClass.Instance.Hello();



Singleton 工具 (繼承 MonoBehaviour 版)

SingletonObject.cs   (C#)

/**********************************************************
// Author   : K.(k79k06k02k)
// FileName : SingletonObject.cs
// Reference: http://wiki.unity3d.com/index.php/Singleton
**********************************************************/
using UnityEngine;

public class SingletonObject : MonoBehaviour where T : MonoBehaviour
{
                  private static T _instance;

                  private static object _lock = new object();

                  public static T Instance
                  {
                                    get
                                    {
            //判斷物件是否已刪除
                                                      if (applicationIsQuitting) {
                                                                        Debug.LogWarning("[Singleton] Instance '"+ typeof(T) +
                                                                                          "' already destroyed on application quit." +
                                                                                          " Won't create again - returning null.");
                                                                        return null;
                                                      }

            //多執行緒
            lock (_lock)
                                                      {
                                                                        if (_instance == null)
                                                                        {
                                                                                          _instance = (T) FindObjectOfType(typeof(T));

                    //是否已經建立出物件
                                                                                          if ( FindObjectsOfType(typeof(T)).Length > 1 )
                                                                                          {
                                                                                                            Debug.LogError("[Singleton] Something went really wrong " +
                                                                                                                              " - there should never be more than 1 singleton!" +
                                                                                                                              " Reopenning the scene might fix it.");
                                                                                                            return _instance;
                                                                                          }

                                                                                          if (_instance == null)
                                                                                          {
                                                                                                            GameObject singleton = new GameObject();
                                                                                                            _instance = singleton.AddComponent();
                                                                                                            singleton.name = "(singleton) "+ typeof(T).ToString();

                        //始終不刪除(EX:換場景時不刪除)
                                                                                                            DontDestroyOnLoad(singleton);

                                                                                                            Debug.Log("[Singleton] An instance of " + typeof(T) +
                                                                                                                              " is needed in the scene, so '" + singleton +
                                                                                                                              "' was created with DontDestroyOnLoad.");
                                                                                          } else {
                                                                                                            Debug.Log("[Singleton] Using instance already created: " +
                                                                                                                              _instance.gameObject.name);
                                                                                          }
                                                                        }

                                                                        return _instance;
                                                      }
                                    }
                  }

                  private static bool applicationIsQuitting = false;
                  ///





                  /// When Unity quits, it destroys objects in a random order.
                  /// In principle, a Singleton is only destroyed when application quits.
                  /// If any script calls Instance after it have been destroyed,
                  ///   it will create a buggy ghost object that will stay on the Editor scene
                  ///   even after stopping playing the Application. Really bad!
                  /// So, this was made to be sure we're not creating that buggy ghost object.
                  ///

                  public void OnDestroy () {
                                    applicationIsQuitting = true;
                  }
}


使用時機

如果是純資料(存取Resources、遊戲表格資料),建議使用 Singleton.cs 因為沒有必要繼承MonoBehaviour,省下開銷。



Debug過濾器(Debug Filter)

在開發中會有很多地方使用到 Debug.Log 印出資料,在遊戲上線後要關閉 Debug.Log 就會變得非常麻煩,於是我們就會使用程式碼進行集中控制

TestClass.cs   (C#)

public class TestClass
{
    public static void Log(object message)
    {
#if Debug
        Debug.Log(message);
#endif
    }
}



使用方式

程式中要 Show Debug Log 的地方都呼叫此方法

將 File -> Build Settings -> Scripting Define Symbols  新增 Debug 字串

在遊戲上線後將 Scripting Define Symbols 移除 Debug 字串,Debug Log 就不會顯示



問題

雖然達成了 Debug Log 排除功能,但在我們點選 Unity Console 中的 Debug 訊息兩下,想要知道 Log 是 Script中 哪一行時,就都會指定到集中管理的方法中,這樣造成了Debug上的不方便,還要自己根據 Stack 去詢是哪一行



改進

這時可以使用自行新增 Debug Class 與 Conditional 的方式,改進上述Debug中的不方便,程式碼是參考以下網站

Matt的拜神紀錄–關掉unity3d Debug.Log



DebugFilter工具

DebugFilter.cs   (C#)

/**********************************************************
// FileName : DebugFilter.cs
// Reference: http://liangyenchen.blogspot.tw/2015/09/unity3d-debuglog.html
**********************************************************/
#if DebugFilter

using System.Diagnostics;
using UnityEngine;

public static class Debug
{
    [Conditional("DebugFilter")]
    public static void Break() { }
    [Conditional("DebugFilter")]
    public static void ClearDeveloperConsole() { }
    [Conditional("DebugFilter")]
    public static void DebugBreak() { }
    [Conditional("DebugFilter")]
    public static void DrawLine(Vector3 start, Vector3 end) { }
    [Conditional("DebugFilter")]
    public static void DrawLine(Vector3 start, Vector3 end, Color color) { }
    [Conditional("DebugFilter")]
    public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration) { }
    [Conditional("DebugFilter")]
    public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration, bool depthTest) { }
    [Conditional("DebugFilter")]
    public static void DrawRay(Vector3 start, Vector3 dir) { }
    [Conditional("DebugFilter")]
    public static void DrawRay(Vector3 start, Vector3 dir, Color color) { }
    [Conditional("DebugFilter")]
    public static void DrawRay(Vector3 start, Vector3 dir, Color color, float duration) { }
    [Conditional("DebugFilter")]
    public static void DrawRay(Vector3 start, Vector3 dir, Color color, float duration, bool depthTest) { }

    [Conditional("DebugFilter")]
    public static void Log(object message) { }
    [Conditional("DebugFilter")]
    public static void Log(object message, UnityEngine.Object context) { }
    [Conditional("DebugFilter")]
    public static void LogError(object message) { }
    [Conditional("DebugFilter")]
    public static void LogError(object message, UnityEngine.Object context) { }
    [Conditional("DebugFilter")]
    public static void LogException(System.Exception exception) { }
    [Conditional("DebugFilter")]
    public static void LogException(System.Exception exception, UnityEngine.Object context) { }
    [Conditional("DebugFilter")]
    public static void LogWarning(object message) { }
    [Conditional("DebugFilter")]
    public static void LogWarning(object message, UnityEngine.Object context) { }
}
#endif


使用方式

將 File -> Build Settings -> Scripting Define Symbols  新增 DebugFilter 字串,這樣 Debug Log 就會排除了



後記

這次分享了 Singleton 與 DebugFilter 工具,在開發中會更加方便、快速、簡潔,透過以上程式碼,相信大家一定會根據自己的需求製作出更加方便的工具



參考資料

Unity Wiki Singleton

Matt的拜神紀錄–關掉unity3d Debug.Log

MSDN Conditional









屬於工程師的專用工具箱():編輯工具(Editor Tools)

2016-04-26  k79k06k02k  Tool, Unity

原文出處:http://wp.me/p7hYyg-3Z



此次要介紹的是編輯器工具,能提升編輯效率、省時間,進而更專注在開發上



分類

AssetBundle 相關

建立檔案相關

搜索相關

Prefab 相關

UGUI 相關

視窗相關

顯示相關



程式碼範例 (From GitHub)



AssetBundle 相關

AssetBundle 分析 (AssetBundle Analyze)

使用方式:選擇任一 AssetBundle 檔案(暫定附檔名abbin) 可進行分析



AssetBundle 觀察器 (AssetBundle Watch)

注意:只適用 Unity 5 新的 AssetBundle 系統



使用方式:分為單一選擇顯示” 全部顯示” 兩種,根據檔案所設定的AssetBundle Name 進行分組顯示,能清楚的看到目前哪些檔案在哪個Bundle





AssetBundle 建立 (AssetBundle Build)

注意:只適用 Unity 5 新的 AssetBundle 系統



使用方式:選擇打包平台後建立



AssetBundle 建立所有平台 (AssetBundle Build All Platform)

注意:只適用 Unity 5 新的 AssetBundle 系統



使用方式:選擇後自動建立所有平台AssetBundle (程式中可進行調整平台列表)



印出目前所有AssetBundle Name (AssetBundle Show All Name)

注意:只適用 Unity 5 新的 AssetBundle 系統



使用方式:選擇後會在 Console View 印出目前所有AssetBundle Name




建立檔案相關

建立資料夾 (Make Project Folders)

使用方式:選擇要建立的資料夾



搜索相關

物件尋找器 (Finder)

使用方式:輸入要尋找的字串後,可選擇是否要加入Scene的,或是加入Prefab物件,Filters 可以選擇過濾類型




Prefab 相關

Prefab 工具 (Prefab Tool)

使用方式:分為建立” 同意修改(Apply)” 兩種,這樣就可以方便的操作多個Prefab (P.S. 目前Unity中不能同時操作多個Prefab)








UGUI 相關

UGUI 工具 (UGUI Tool)

使用方式:

1. Anchors 對齊至四角 (Anchors to Corners)
2. 四角對齊至 Anchors (Corners to Anchors)
3. 根據 Anchors 位置水平翻轉圖像 (Mirror Horizontally Around Anchors)
4. 根據 Parent 的中心點水平翻轉 Anchors 圖像 (Mirror Horizontally Around Parent Center)
5. 根據 Anchors 位置垂直翻轉圖像 (Mirror Vertically Around Anchors)
6. 根據 Parent 的中心點垂直翻轉 Anchors 圖像 (Mirror Vertically Around Parent Center)




視窗相關

場景切換器 (Scene Watcher)

使用方式:先在  Unity → File → Build Settings 中加入場景後,點選場景名稱即可切換場景




顯示相關

Hierarchy 中顯示物件 Icon



Hierarchy 中顯示 Canvas Sprite Sorting Order,並可以進行調整




Inspector Transform Component 下顯示 PositionRotateScale  Reset 按鈕




後記

以上就是這次分享的 Editor 工具,相信能讓開發更便捷更快速,有了這些範例,就可以根據不同情況做出編輯工具,從而提升開發效率、分工更明確。



參考資料

Unity Wiki









屬於工程師的專用工具箱(四):推薦 Plugin [免費篇]

2016-05-02 k79k06k02k Tool, Unity

原文出處:http://wp.me/p7hYyg-6j



挖寶吧,Asset Store 中有一些免費且好用的工具,讓我們一起來看看有哪些吧



DOTween (補間動畫)

簡介:可以對3D物件、2D物件、UI甚至是Audio、Material、Light等等,快速製作補間動畫的工具
需求版本:Unity 3.5.7 或 更高版本

icon_unity asset store




Instant Screenshot (螢幕截圖)



簡介:可以透過 Camera 截取高畫質影像,且可以去背
需求版本:Unity 4.5.5 或 更高版本

icon_unity asset store



Sorting View (Sprite排序管理)



簡介:顯示目前所有Sprite 物件的 Sorting Layer 與 Order in Layer,且可以進行修改
需求版本:Unity 4.6.3 或 更高版本

icon_unity asset store



Log Viewer (Log 顯示)

簡介:在手機上方便顯示 Log、設備資訊、反應時間等等資訊
需求版本:Unity 4.0.0 或 更高版本

icon_unity asset store




A* Pathfinding Project Free (A* 巡路系統免費版)


簡介:方便快速的製作出巡路種類、路徑、方式
需求版本:Unity 5.1.0 或 更高版本

  官方下載頁面  




Cinema Themes 2 (攝影機效果)



簡介:快速套用 懷舊、夜晚、海灘、北極…等等,攝影機效果
需求版本:Unity 4.6.1 或 更高版本

icon_unity asset store



Ruler 2D (2D對齊、尺標、網格工具)


簡介:在場景中顯示尺標、自定義網格、多物體對齊、分散對齊等等…
需求版本:Unity 5.3.4 或 更高版本

icon_unity asset store




以上就是這次的免費 Plugin,歡迎各位提供更多實用的Plugin