Re: [請益] [程式] Unity換背景
※ 引述《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
→
08/31 18:28, , 1F
08/31 18:28, 1F
→
08/31 18:30, , 2F
08/31 18:30, 2F
如果原問題是事件處理的話
那需更深入了解 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)
→
08/31 20:43, , 3F
08/31 20:43, 3F
→
08/31 20:45, , 4F
08/31 20:45, 4F
→
08/31 20:48, , 5F
08/31 20:48, 5F
推
08/31 20:52, , 6F
08/31 20:52, 6F
→
08/31 20:53, , 7F
08/31 20:53, 7F
→
08/31 21:06, , 8F
08/31 21:06, 8F
→
08/31 21:07, , 9F
08/31 21:07, 9F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 3 篇):