OnGui元素与其他OnGui元素交互
本文关键字:OnGui 元素 交互 其他 | 更新日期: 2023-09-27 18:06:33
在我的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;
}
}
从问题的注释部分,我假设您想知道如何做到这一点:
"。。。你想让你的平面物体和相机一起移动"-Steven Mills
"谢谢你@StevenMills你有这样的例子吗"j bel
虽然评论中提供的答案是手动将平面添加为相机的子级(一种非常简单的手动方法(,但我将通过脚本提供另一种方法来实现这一点(鉴于这可能会帮助其他人,而忽略其他人使用此解决方案的可能性(。
这样做的目的是创建一个脚本(因此在附加到MainCamera
之后(,该脚本将使用GameObject.FindGameObjectsWithTag方法搜索Object Hierarchy
中的所有GameObject
。一旦我们拥有了所有带有关联Tag
的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;
}
}
}
现在,为了让这个脚本工作,你必须做一些事情:
- 将此脚本附加到要作为其他对象的父对象的任何
GameObject
- 在脚本附加到的
GameObject
的Inspector
中,输入要使用的Tag
的名称 - 对于
Hierarchy
中应添加为子级的所有GameObject
,分配相同的Tag
当然,你还可以做其他事情,比如,不要只搜索一个Tag
,而是可以搜索多个,但这需要更多的工作。尽管如此,我希望这至少能为人们提供有用的信息,让他们了解如何通过脚本进行育儿。