[英]How to link a GUI Texture button to a script in Unity3d
我需要一些帮助来制作自定义GUI纹理按钮,在Unity3d for Android中触摸3d对象时启动脚本。
我有一个GUI按钮,可以在按下时旋转3D模型。 程序创建按钮后,我得到的以下(C#)脚本工作正常:
using UnityEngine;
using System.Collections;
public class RotateButtonBheaviour : MonoBehaviour {
private bool mIsRotated = false;
private bool mMustRotate = false;
private GameObject Cube;
// Use this for initialization
void Start () {
Cube = GameObject.Find ("MarkerObject");
}
// Update is called once per frame
void Update () {
if (mMustRotate && !mIsRotated) {
//Rotate all models 45 degrees around X
if (Cube != null) {
GameObject modelUnderChipsTrackable = Cube.transform.GetChild(0).gameObject;
modelUnderChipsTrackable.transform.RotateAround(new Vector3(1,0,0), Mathf.PI/3);
}
mIsRotated = true;
mMustRotate = false;
}
public Texture btnTexture1;
void OnGUI() {
if (!btnTexture1) {
Debug.LogError("Please assign a texture on the inspector");
return;
}
if (GUI.Button(new Rect(10, 10, 120, 30), btnTexture1))
if (!mIsRotated) {
mMustRotate = true;
}
我希望代替代码中的按钮来添加我的自定义GUI纹理按钮(名为OrbitBtn),以便在触摸时启动围绕其轴旋转的3d模型。
请给我一些有关如何实现该功能的建议。 预先感谢大家的回答。
只需更改btnTexture1纹理,否则就可以单击并拖放图像文件到脚本中,因为您已将btnTexture1声明为public。
或者,如果您想在javascript中做同样的事情,这里是示例代码。
if(GUI.Button(Rect(Screen.width*.85,Screen.height*0,width,width),Resources.Load("pause") as Texture))
{
//Button press action
}
因此,在此示例代码中,我正在将图像(暂停*)加载到按钮上。 确保图像在资产文件夹中的“资源”文件夹中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.