Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 与其他OnGui元素交互的OnGui元素_C#_Unity3d_Kinect - Fatal编程技术网

C# 与其他OnGui元素交互的OnGui元素

C# 与其他OnGui元素交互的OnGui元素,c#,unity3d,kinect,C#,Unity3d,Kinect,在我的2d游戏中,我希望有许多OnGui元素供用户选择,但是,我使用的光标是另一个OnGui元素(使用kinect导航),这是可能的,目前我使用的是平面,但我将放大和缩小相机,因此我基本上需要它们连接到屏幕上。任何想法、建议或解决办法。 这是当前我的光标 using UnityEngine; using System; using System.Collections; public class PillarAgent : MonoBehaviour { public SkeletonWra

在我的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;
}

}
使用UnityEngine;
使用系统集合;
公共类选择工具:MonoBehavior{
公共工具;
公共浮动开始时间;
公营部门选举产生;
公共游戏对象选择工具;
void Start()
{
tools=this.GetComponent();//要使用此工具,必须将muyst附加到游戏对象
//这实质上是说,对于这个游戏对象,获取名为tools的组件
}
无效更新()
{
}
void OnCollisionStay(碰撞其他)
{
startTime+=Time.deltaTime;
如果(开始时间>5f){
if(Other.collider.tag==(“头皮”))
{
选择工具=Other.collider.gameObject;
Debug.Log(selectedTool+“好名字是:”+tools.utilities[0]);
}
否则{
selectedTool=null;
}
如果(选择工具){

对于(int i=0;i从评论部分到问题,我假设您想知道如何做到这一点:

“…您希望您的平面对象与摄影机一起移动…”-史蒂文·米尔斯


“谢谢@StevenMills你有什么例子可以证明这一点吗?”j bel说

虽然在评论中给出的答案是只需手动添加平面作为摄影机的子对象(一种非常简单的手动方法),但我将给出另一种通过脚本实现的方法(考虑到这可能会帮助其他人摆脱使用此解决方案的可能性)

这样做的目的是创建一个脚本(从而将以下内容附加到
MainCamera
)这将使用该方法搜索
对象层次结构中的所有
GameObject
。一旦我们拥有了所有
GameObject
,以及相关的
标记
,我们就可以在数组中循环,并将附加脚本的
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;
        }
    }
}
公共类ParentGameObjects:MonoBehavior{
//要在层次结构中的所有游戏对象上搜索的标记
公共字符串objectTag;
//我们将作为主摄影机父对象的游戏对象
私有游戏对象[]子对象;
//没有必要存储对子对象的引用,但是如果您想在某个时候修改它们,您可以
void Start(){
//找到所有带有我们想要的标签的游戏对象
children=GameObject.FindGameObjectsWithTag(objectTag);
//循环浏览找到的所有游戏对象,并将此对象的变换设置为父对象
for(int i=0;i
现在,要使此脚本正常工作,您必须做以下几件事:

  • 将此脚本附加到任何要作为其他对象父对象的
    GameObject
  • 在脚本附加到的
    GameObject
    Inspector
    中,输入要使用的
    标记的名称
  • 对于
    层次结构中应添加为子级的所有
    游戏对象
    ,分配相同的
    标记

  • 当然,您还可以做其他事情,例如,不要只搜索一个
    标记
    可以搜索多个标记,但这需要一点(不太多)还有更多的工作。尽管如此,我希望这至少能对一些人提供有用的信息,让他们了解如何通过脚本进行育儿工作。

    您好。很难准确理解您想做什么。请尝试进一步回答您的问题,给出您遇到问题的代码示例。@EvilClosetMonkey对此表示抱歉,实际上只有嗯,我想实现这一点的方法是用光标创建另一个与游戏世界交互的对象。如果我理解正确,你希望你的平面对象与相机一起移动。一个简单的方法是将这些平面对象作为子对象粘贴到相机中。然后当你移动相机时,它们会自动随之移动hank you@StevenMills你有什么例子可以这样做吗?在unity中,在查看层次窗口时,只需将平面拖放到主相机上。然后你会看到相机旁边有一个小箭头符号,它会打开相机内所有子对象的列表。如果你仍然不确定,试着在youtube上看看,应该有一个视频在某处显示这一点。