2013-03-30 65 views
0

在我的2D遊戲中,我需要一些OnGui元素供用戶選擇,但是,我使用的遊標是另一個ongui元素(使用kinect導航)是可能的無論如何,現在我正在使用飛機,但我會放大和縮小攝像頭,因此生病時基本上需要將它們放在屏幕上。任何想法,建議或解決方法。 這是我目前的光標。OnGui元素與其他OnGui元素進行交互

using UnityEngine; 
using System; 
using System.Collections; 

public class PillarAgent : MonoBehaviour { 

public SkeletonWrapper sw; 
public Vector3 distance; 

public float progress =0f; 

public Texture2D cursor; 
public Texture2D load; 

public Camera mainCam; 

public float startTime; 
private int roundedRestSecounds; 

// Use this for initialization 

    float differencex = 0; 
    float differencey = 0; 

void Start() { 

    distance =new Vector3(0f,0f,0f); 

} 
float translate(float value, float leftMin, float leftMax, 
     float rightMin,float rightMax) 
{ 
    float leftSpan = leftMax - leftMin; 
    float rightSpan= rightMax - rightMin; 

    float valueScaled = (value-leftMin)/(leftSpan); 
    return rightMin+(valueScaled * rightSpan); 
} 
// Update is called once per frame 
void Update() { 
    if (sw.pollSkeleton()) 
    { 
     distance.x=sw.bonePos[0,0].x - sw.bonePos[0,7].x;//5 is left shoulder 
     distance.y=sw.bonePos[0,0].y -sw.bonePos[0,7].y; 


     differencex=translate(distance.x,.6f,0,0,Screen.width); 
     differencey=translate(distance.y,-.5f,0,0,Screen.height); 
     //Debug.Log(); 

     float width = sw.bonePos[0,5].x+ sw.bonePos[0,9].x; 
     float height =sw.bonePos[0,4].y- sw.bonePos[0,0].y; 
     float heightdiv= (height/2)+sw.bonePos[0,0].y;   
    }  
} 

void OnGUI() { 
    //left top width height 
    Rect r = new Rect(differencex,differencey,80,50); 

    GUI.Label(r,cursor); 
    GUI.BeginGroup(new Rect(differencex,differencey+50,50*Mathf.Clamp01(progress),15)); 
    //Debug.Log(progress); 
    GUI.DrawTexture(new Rect(0,0,50,50),load); 
    GUI.EndGroup(); 

    transform.position =mainCam.ScreenToWorldPoint(new Vector3(differencex,Screen.height-differencey,50)); 

    //mainCam.fieldOfView()  
} 

void OnCollisionStay(Collision Other) 
{ 
    startTime+=Time.deltaTime; 

    if(Other.gameObject.GetComponent(typeof(TextControl))) 
    { 
     roundedRestSecounds=Mathf.CeilToInt(Time.time); 

     progress = Time.time *0.2f; 

     CurrentState=true; 
    } 
    else if(Other.gameObject.tag==("Scalpal")){ 


     progress = startTime *0.5f; 
     //scallpall activated 
     //  
    }   
} 

void OnCollisionExit(Collision Other){ 
    startTime =0f; 
    progress =0f;   
} 

public Boolean CurrentState{get;set;} 
} 

下一個類是基本類中,我拿起工具,目前這個代碼不工作(不知道爲什麼),但我希望做的是選擇一些工具,顯示在屏幕上,以便我可以使用它們,比如拿起油漆刷開始畫磚或者沒有。目前我在飛機上有我的工具,我希望在相機移動時隨時將它們放在屏幕上。

using UnityEngine; 
using System.Collections; 

public class SelectTool : MonoBehaviour { 

public Tools tools; 
public float startTime; 
public bool ScalpalSelected; 
public GameObject selectedTool; 

void Start() 
{ 
    tools = this.GetComponent<Tools>(); //in order to use this tools muyst be attached to the game object 
    //this is essentially saying with regards to this game object get the component named tools 
} 
void update() 
{ 

} 
void OnCollisionStay(Collision Other) 
{ 
    startTime +=Time.deltaTime; 

    if(startTime >5f){ 
     if(Other.collider.tag==("Scalpal")) 
     { 
      selectedTool = Other.collider.gameObject; 
      Debug.Log(selectedTool+" What in gods good name is:" +tools.utilities[0]); 

     } 
     else { 
      selectedTool=null; 
     } 
     if(selectedTool){ 
      for(int i=0;i<tools.utilities.Length;i++) 
      { 

      }  
     } 


      ScalpalSelected=true; 
      renderer.material.color = Color.yellow;  
    } 
} 
void OncollisionStay(Collision other){ 

    startTime = 0f; 
} 

} 
+0

你好。很難準確理解你想要做什麼。請嘗試擴展您的問題,舉例說明您遇到的問題。 –

+0

@EvilClosetMonkey對此感到遺憾,基本上我唯一能想到的方法就是完成我對光標所做的事情,即創建另一個與遊戲世界交互的對象。 –

+1

好吧,如果我明白你說的正確,你想讓你的飛機物體與相機一起移動。一個簡單的方法是將這些平面對象作爲子對象粘貼到相機中。然後當你移動相機時,他們會自動移動它 –

回答

1

從評論部分的問題,我會假設你想知道如何做到這一點:

」 ......你想讓你的飛機對象與相機一起移動.. 。「 - 史蒂文米爾斯

」謝謝@StevenMills你有沒有任何這樣做的例子? ĴBEL

儘管在給予的回答是隻手動添加的平面上的相機(一個非常簡單的,手動的方法),我會給另一種方式通過腳本來做到這一點的孩子(以或許這樣做會幫助別人無視使用此解決方案的人的可能性)。

的這個想法是創建一個腳本(因此以下被附接到所述MainCamera),將通過所有的GameObjectObject Hierarchy搜索與方法GameObject.FindGameObjectsWithTag。一旦我們將所有的GameObject與關聯的Tag相關聯,我們就可以循環訪問數組,並將所附腳本的GameObject分別添加到每個數組中。

public class ParentGameObjects : MonoBehaviour { 
    //The tag to search for on all game objects in the hierarchy 
    public String objectTag; 

    //The game objects that we will parent the main camera to 
    private GameObject[] children; 
    //It's not necessary to store references to the children but if you want to modify them at some point you will be able to 

    void Start() { 
     //Find all game objects with the tag we want 
     children = GameObject.FindGameObjectsWithTag(objectTag); 
     //Loop through all of the game objects found and parent this object's transform 
     for(int i = 0; i < children.Length; i++) { 
      children[i].transform.parent = transform; 
     } 
    } 
} 

現在,有你所要做的幾件事情對這個腳本工作:

  1. 要父其他物體裝上此腳本任何GameObject
  2. 在腳本所附的GameObjectInspector中,輸入要使用的Tag的名稱。
  3. 對於所有GameObject(s)在Hierarchy應該作爲兒童添加,分配相同的Tag

當然還有其他的事情可以做,等等,例如,而不是隻尋找一個Tag可以搜索多個但這需要一個位(不完全多)更多的工作。儘管如此,我希望這至少可以成爲有關父母如何通過腳本工作的有用信息。