Fw: [問題] 請問unity的C#問題

看板GameDesign (遊戲設計)作者 (咬咬嚼嚼猴子口味)時間11年前 (2013/12/24 13:55), 編輯推噓2(208)
留言10則, 4人參與, 最新討論串1/1
※ [本文轉錄自 C_Sharp 看板 #1IkI6PlJ ] 作者: euph (咬咬嚼嚼猴子口味) 看板: C_Sharp 標題: [問題] 請問unity的C#問題 時間: Tue Dec 24 13:54:29 2013 C#新手魯蛇一位 最近在用C#開發Unity的遊戲 有兩個問題想請問一下 首先問題一 是最近遇到一個在Unity裡切換Sence時保留物件的問題 只知道用DontDestroyOnLoad去保留物件 可是不知道切換了Sence之後要如何去取這個物件裡的東西 另外問題二 在研究的上面的問題的時候參考了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; 感謝鄉民前輩 :) -- 樂於當宅男 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.118.185.164 ※ 發信站: 批踢踢實業坊(ptt.cc) ※ 轉錄者: euph (219.118.185.164), 時間: 12/24/2013 13:55:01

12/24 14:31, , 1F
問題1簡易解法:GameObject.Find or FindWithTag
12/24 14:31, 1F

12/24 14:32, , 2F
或是自己寫個singleton把ref保留下來
12/24 14:32, 2F

12/24 14:33, , 3F
問題2:你可以去研究一下static的意義和singleton模式
12/24 14:33, 3F

12/24 16:05, , 4F
感謝樓上 那請問一下ScriptableObject是不是和Singleton類似?
12/24 16:05, 4F

12/24 17:05, , 5F
不一樣,ScriptableObject是可以存在硬碟中的檔案形式
12/24 17:05, 5F

12/24 17:06, , 6F
讓你可以把他從硬碟中讀回來,用來存資料為主
12/24 17:06, 6F

12/30 13:10, , 7F
其實問題2他還是有加到實體物件上,他是動態產生跟加入的
12/30 13:10, 7F

12/30 13:12, , 8F
new GameObject("Game...")<==產生gameobject
12/30 13:12, 8F

12/30 13:13, , 9F
AddComponent<GameStateManager>()<==將script掛到物件
12/30 13:13, 9F

12/31 15:47, , 10F
1不需要singleton, 他本身已經dontDestroy了, 所以存ref就可以
12/31 15:47, 10F
文章代碼(AID): #1IkI6sHc (GameDesign)
文章代碼(AID): #1IkI6sHc (GameDesign)