2011-10-04 48 views
0

我終於找到了一個腳本,可以在IOS項目中使用GUI按鈕。我正在使用Unity3d遊戲引擎。我對JavaScript按鈕和動畫有點熟悉,但對C#並不熟悉。我的問題不知道是編寫這個函數,當觸摸按鈕時,它將在C#按鈕腳本中播放排隊的動畫。下面是IOS按鈕腳本的副本,然後是我必須播放排隊動畫的代碼。C#GUI紋理按鈕腳本

using UnityEngine; 
using System.Collections; 

public enum Btn 
{ 
    normal, 
    hover, 
    armed 
} 

[System.Serializable] // Required so it shows up in the inspector 
public class ButtonTextures 
{ 
    public Texture normal=null; 
    public Texture hover=null; 
    public Texture armed=null; 
    public ButtonTextures() {} 

    public Texture this [ButtonState state] 
    { 
     get 
     { 
      switch(state) 
      { 
       case ButtonState.normal: 
        return normal; 
       case ButtonState.hover: 
        return hover; 
       case ButtonState.armed: 
        return armed; 
       default: 
        return null; 
      } 
     } 
    } 
} 


[RequireComponent(typeof(GUITexture))] 
[AddComponentMenu ("GUI/Button")]  
public class GuiButton : MonoBehaviour 
{ 
    public GameObject messagee; 
    public string message = ""; 
    public string messageDoubleClick = ""; 
    public ButtonTextures textures; 

    protected int state = 0; 
    protected GUITexture myGUITexture; 

    private int clickCount = 1; 
    private float lastClickTime = 0.0f; 
    static private float doubleClickSensitivity = 0.5f; 

    protected virtual void SetButtonTexture(ButtonState state) 
    { 
     if (textures[state] != null) 
     { 
      myGUITexture.texture = textures[state]; 
     } 
    } 

    public virtual void Reset() 
    { 
     messagee = gameObject; 
     message = ""; 
     messageDoubleClick = ""; 
    } 

    public bool HitTest(Vector2 pos) 
    { 
     return myGUITexture.HitTest(new Vector3(pos.x, pos.y, 0)); 
    } 

    public virtual void Start() 
    { 
     myGUITexture = GetComponent(typeof(GUITexture)) as GUITexture; 
     SetButtonTexture(ButtonState.normal); 
    } 

    public virtual void OnMouseEnter() 
    { 
     state++; 
     if (state == 1) 
     SetButtonTexture(ButtonState.hover); 
    } 

    public virtual void OnMouseDown() 
    { 
     state++; 
     if (state == 2) 
      SetButtonTexture(ButtonState.armed); 
    } 

    public virtual void OnMouseUp() 
    { 
     if (Time.time - lastClickTime <= doubleClickSensitivity) 
     { 
      ++clickCount; 
     } 
     else 
     { 
      clickCount = 1; 
     } 

     if (state == 2) 
     { 
      state--; 
      if (clickCount == 1) 
      { 
       if (messagee != null && message != "") 
       { 
        messagee.SendMessage(message, this); 
       } 
      } 
      else 
      { 
       if (messagee != null && messageDoubleClick != "") 
       { 
        messagee.SendMessage(messageDoubleClick, this); 
       } 
      } 
     } 
     else 
     { 
      state --; 
      if (state < 0) 
       state = 0; 
     } 
     SetButtonTexture(ButtonState.normal); 
     lastClickTime = Time.time; 
    } 

    public virtual void OnMouseExit() 
    { 
     if (state > 0) 
      state--; 
     if (state == 0) 
      SetButtonTexture(ButtonState.normal); 
    } 

#if (UNITY_IPHONE || UNITY_ANDROID) 
    void Update() 
    { 
     int count = Input.touchCount; 
     for (int i = 0; i < count; i++) 
     { 
      Touch touch = Input.GetTouch(i); 
      if (HitTest(touch.position)) 
      { 
       if (touch.phase == TouchPhase.Ended || touch.phase ==  TouchPhase.Canceled) 
       { 
        SetButtonTexture(ButtonState.normal); 
       } 
       else 
       { 
        SetButtonTexture(ButtonState.armed); 
       } 
       if (touch.phase == TouchPhase.Began) 
       { 
        if (touch.tapCount == 1) 
       { 
        if (messagee != null && message != "") 
        { 
         messagee.SendMessage(message, this); 
        } 
        } 
        else if (touch.tapCount == 2) 
        { 
         if (messagee != null && messageDoubleClick != "") 
         { 
          messagee.SendMessage(messageDoubleClick, this); 
         } 
        } 
       } 
       break; 
      } 
     } 
    } 
#endif 
} 

大部分這似乎處理按鈕狀態,其中我的觸摸按鈕只有一個狀態,這是'正常'。是否應該刪除引用'懸停'和武裝?我也在控制檯中發現錯誤, 「找不到類型或命名空間」Button State「。您是否缺少using指令或程序集引用?」

爲C#GUI按鈕來播放該代碼排隊動畫我想插入內容是這樣的:

using UnityEngine; 
using System.Collections; 

public class example : MonoBehaviour { 
    void Update() { 
     if (Input.GetButtonDown("Btn")) 
      animation.PlayQueued("shoot", QueueMode.PlayNow); 

    } 
} 

我想排隊動畫劇本片斷; Input.GetButtondown ....將更改爲

void Update() {  
     if(Input.GetTouch("Btn")) 
     animation.PlayQueued("shoot", QueueMode.PlayNow); 
} 

並插入到GUI Button腳本的第148行左右。如果可以,請幫助我,我是 感覺下來。認真!任何幫助重新格式化這個腳本將不勝感激 和作爲模板,因爲我有兩個其他的GUI按鈕來設置。它可能會問很多問題,或者什麼是天堂?

尊敬,

數碼d

模擬人, 在數字世界

回答

0

好了,有很多的腳本怎麼回事,而是讓你穿上這似乎是設計」不得不修改它。 你需要做的是轉到要播放動畫的對象,並在其上創建一個函數,以便在點擊按鈕時調用。

因此,像(如果你喜歡,你可以做到這一點的JS):

public class ShootyMan : MonoBehaviour { 
    public void Shoot() { 
    animation.PlayQueued("shoot", QueueMode.PlayNow); 
    } 
} 

現在,看看在檢查的按鈕。我不確定您是否熟悉Unity中的消息傳遞方式,但基本上按鈕會向「消息」發送「消息」。 (即它將在​​附加到消息的任何腳本上調用名稱消息的函數)。 使用「拍攝」功能將「消息」設置爲遊戲對象。並將消息設置爲字符串「拍攝」。

+0

謝謝!我同意!發生的事情太多了。我只需要一個基本的腳本來放置不同的GUI按鈕。我會試試這個。 –