0

在我的 2d 游戏中,我希望有许多 OnGui 元素供用户选择,但是,我使用的光标是另一个 ongui 元素(使用 kinect 导航),这是可能的,目前我正在使用飞机但我会放大和缩小相机,所以基本上需要将它们连接到屏幕上。任何想法、建议或解决方法。这是目前我的光标。

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;
}

}
4

1 回答 1

1

从评论部分到问题,我假设您想知道如何执行此操作:

“......你希望你的平面物体与相机一起移动......” - 史蒂文米尔斯

“谢谢@StevenMills,你有什么例子可以说明如何做到这一点吗?” 杰贝尔

虽然评论中提供的答案是手动将飞机添加为相机的子项(一种非常简单的手动方法),但我将通过脚本提供另一种方法(鉴于这可能会帮助其他人忽略某人使用此解决方案的可能性)。

这样做的想法是创建一个脚本(因此被附加到),它将使用GameObject.FindGameObjectsWithTag方法MainCamera搜索所有GameObject的。一旦我们拥有了所有相关的,我们就可以遍历数组并将附加脚本的父级添加到每个。Object HierarchyGameObjectTagGameObject

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. Attach this script to any GameObject that you want to parent other objects to.
  2. In the Inspector of the GameObject that the script is attached to, enter in the name of the Tag you want to use.
  3. For all GameObject(s) in the Hierarchy that should be added as children, assign the same Tag.

Of course there are other things you can do like, for example, instead of only searching for one Tag be able to search for multiple but that requires a bit (not exactly much) more work. Nonetheless, I hope this will at least be useful information to someone on how parenting works via scripting.

于 2013-04-10T14:57:25.657 回答