【发布时间】:2016-11-27 06:58:28
【问题描述】:
我正在使用统一制作用户交互式选择系统,其中有表示各种产品的按钮,并且当用户单击产品时,产品名称将以系统顺序显示在另一个下方。我使用了 OnGUI() 函数来显示产品名称。但是在我的输出中,所有的名字都被叠加打印出来了。
我使用静态变量 i(最初定义为 0)增加了 GUI.label 的 y 位置。我尝试在每次单击时增加 i 的值并将其添加到 GUI.label 的 y 位置。现在,当我单击第二个按钮时,第一个按钮标签和第二个按钮标签都会移动到新坐标。
using UnityEngine;
using UnityEngine.EventSystems;// 1
using UnityEngine.UI;
``public class Example : MonoBehaviour, IPointerClickHandler // 2
// ... And many more available!
{
SpriteRenderer sprite;
Color target = Color.red;
int a=200,b=100;
public GUIText textObject;
public bool showGUI;
public int s=0;
public static int i=0;
void Awake()
{
sprite = GetComponent<SpriteRenderer>();
}
void test()
{
i = i + 20;
OnGUI();
}
void Update()
{
if (sprite)
sprite.color = Vector4.MoveTowards(sprite.color, target, Time.deltaTime * 10);
}
public void OnPointerClick(PointerEventData eventData) // 3
{
showGUI = true;
Debug.Log(gameObject.name);
target = Color.blue;
PlayerPrefs.SetString ("Display", gameObject.name);
s = 1;
test ();
}
void OnGUI()
{
if (s == 1) {
GUI.color = Color.red;
GUIStyle myStyle = new GUIStyle (GUI.skin.GetStyle ("label"));
myStyle.fontSize = 20;
GUI.Label (new Rect (a, b, 100f, 10f), "");
if (showGUI) {
//GUI.Box (new Rect (a,b+i, 300f, 100f), "");
GUI.Label (new Rect (a, b + i, 300f, 100f), gameObject.name, myStyle);
s = 0;
}
}
}
}
【问题讨论】:
标签: c# user-interface button unity3d unity5