Re: [問題] 請問unity的C#問題
※ 引述《euph (咬咬嚼嚼猴子口味)》之銘言:
: C#新手魯蛇一位 最近在用C#開發Unity的遊戲
: 有兩個問題想請問一下
: 首先問題一 是最近遇到一個在Unity裡切換Sence時保留物件的問題
: 只知道用DontDestroyOnLoad去保留物件
: 可是不知道切換了Sence之後要如何去取這個物件裡的東西
沒用過Unity沒辦法回答你。
: 另外問題二 在研究的上面的問題的時候參考了FB SDK for Unity的範例
: https://github.com/fbsamples/friendsmash-unity
: 其中有一段Code有點有看沒有懂
: 在 public class GameStateManager : MonoBehaviour裡面
: 這個GameStateManager沒有追加到GameObject實體的物件上
: 卻可以直接在MainMenu裡被拿來用
: 魯蛇小弟實在不懂其中道理 想請問一下這是什麼樣的技巧??
: 我想可能是跟下方的Code有關係 還請C#高手能幫我解惑一下
: delegate GameStateManager InstanceStep();
: static InstanceStep init = delegate()
: {
: GameObject container = new GameObject("GameStateManagerManager");
: instance = container.AddComponent<GameStateManager>();
: instance.lives = StartingLives;
: instance.score = StartingScore;
: instance.highScore = null;
: current = then;
: return instance;
: };
: static InstanceStep then = delegate() { return instance; };
: static InstanceStep current = init;
: 感謝鄉民前輩 :)
特別去看了一下
MainMenu裡面用的是GameStartManager裡面的靜態(static)屬性,所以不用new。
在訪問那些靜態屬性時,他會建立(或取用)唯一的個體(instance)。
細節可以google "Singleton Pattern",看起來是。
但明明有更簡單的寫法,為什麼要特別用函數指標(delegate)做延遲建構,這我就不明白
另外,看起來獨體建構也沒有擋多執行緒,可能不需要吧。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.135.179.10
※ 編輯: Litfal 來自: 220.135.179.10 (12/26 02:05)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):