【问题标题】:OnGui elements interacting with other OnGui elementsOnGui 元素与其他 OnGui 元素交互
【发布时间】:2013-04-11 03:26:17
【问题描述】:

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

}

【问题讨论】:

  • 你好。很难准确地理解您要做什么。请尝试扩展您的问题,提供您遇到问题的代码示例。
  • @EvilClosetMonkey 对此感到抱歉,基本上我能想到的唯一方法就是用光标做我所做的事情,那就是创建另一个与游戏世界交互的对象。
  • 好吧,如果我理解你所说的正确的话,你希望你的平面对象与相机一起移动。一种简单的方法是将这些平面对象作为子对象粘贴到相机中。然后当你移动相机时,它们会自动随之移动
  • 谢谢@StevenMills 你有什么可以做到这一点的例子吗?
  • 在 unity 中,在查看 Hierarchy 窗口时,只需将平面拖放到主摄像机上即可。然后,您应该会在相机旁边看到一个小箭头符号,它会打开其中的所有子对象的列表。如果您仍然不确定,请尝试在 youtube 上查找,应该有一个视频显示此内容。

标签: c# unity3d kinect


【解决方案1】:

从评论部分到问题,我假设您想知道如何做到这一点:

“...您希望平面对象与相机一起移动...” - Steven Mills

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

虽然 cmets 中提供的答案是手动将平面添加为相机的子级(一种非常简单的手动方法),但我将通过脚本提供另一种方法(在考虑到这可能会帮助其他人忽略使用此解决方案的可能性)。

这样做的想法是创建一个脚本(因此附加到MainCamera),它将使用方法GameObject.FindGameObjectsWithTag 搜索Object Hierarchy 中的所有GameObject。一旦我们拥有了所有的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. 对于应作为子级添加的 Hierarchy 中的所有 GameObject(s),分配相同的 Tag

当然,您还可以做其他事情,例如,不仅可以搜索一个Tag,还可以搜索多个,但这需要更多的工作(不完全是)。尽管如此,我希望这至少对某些人来说是有用的信息,了解如何通过脚本进行育儿。

【讨论】:

    猜你喜欢
    • 2015-01-12
    • 2021-08-19
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多