作者poorsen (poorsen)
看板GameDesign
標題Re: [請益] [程式] Unity換背景
時間Wed Aug 31 18:11:27 2011
※ 引述《kingworld (珊夜 薰)》之銘言:
: 自己覺得程式碼應該沒有問題
: (好像程式有問題的人都會這樣說)
: 是希望在同一個場景裡面
: 按下一個按鈕之後 它的圖片/背景會換成另外一張圖
: 是用javascript寫的
: 以下是程式碼
: -----------------------------------------------------------------
: var aTexture : Texture2D;
: function OnGUI() {
: if(GUI.Button(Rect(0,0,100,20),"AAA")){
: GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height), aTexture, ScaleMode.StretchToFill, true,0);
: print("AAA");
: }
: }
texture 要載入才有內容,
圖檔事先匯到 project 目錄 assets/Resources 下
把 GUITexture 附身到 GameObject, 攝影機才照得到
function OnGUI() {
if(GUI.Button(Rect(0,0,100,20),"AAA")){
var tex:Texture2D = Resources.Load("MyImageFile");
var obj : GameObject = new GameObject("tmp_obj");
obj.AddComponent("GUITexture");
obj.guiTexture.pixelInset = new Rect(0,0,300, 300);
obj.guiTexture.texture = tex;
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.24.43.123
→ kingworld:可是我不做判斷式 再將圖片放到Texture裡面是可以出現 08/31 18:28
→ kingworld:不用另外放GameObject 08/31 18:30
如果原問題是事件處理的話
那需更深入了解 unity 如何處置 OnGUI() 與事件機制
可比較這段code 跟原本差在哪
var f = false;
function OnGUI() {
var tex:Texture2D = Resources.Load("MyImageFile");
if(GUI.Button(Rect(0,0,100,20),"AAA"))
f = true;
if (f)
GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height), tex, ScaleMode.StretchToFill, true,0);
}
※ 編輯: poorsen 來自: 114.24.43.123 (08/31 20:20)
→ kingworld:兩者程式是沒有差 請問您這段程式 可以正確執行 且跑出 08/31 20:43
→ kingworld:圖片嗎? 因為我自己的程式碼 執行是無法跑出圖片的... 08/31 20:45
→ poorsen:當然可以 :) 08/31 20:48
推 kingworld:可以執行!! 為什麼?!是因為按鈕只能做運算不能做更改嗎? 08/31 20:52
→ kingworld:天啊!!!你好厲害!! 你要來教我程式嘛!XDDDDDDD 08/31 20:53
→ poorsen:這屬於unity的know-how,只能請您研讀官方文件了 08/31 21:06
→ poorsen:我並非unity專家,期待您找到答案來版上跟大家分享 :) 08/31 21:07