【发布时间】: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 上查找,应该有一个视频显示此内容。