如何在GUI纹理按钮链接到Unity3d脚本 [英] How to link a GUI Texture button to a script in Unity3d

查看:211
本文介绍了如何在GUI纹理按钮链接到Unity3d脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助来使自定义GUI纹理按钮,一个3D对象上启动脚本,当它在Unity3d触摸为Android。

I need some help to make a custom GUI Texture button, start a script on an 3d object when it is touched in Unity3d for android.

我有一个GUI按钮旋转的pressed 3D模型。下面(C#)脚本,我正在O.K.当progrmaticaly创建按钮:

I have a GUI button to rotate a 3d model on pressed. The following (C#) script, I got is working O.K. when the button is created progrmaticaly:

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;
        }

我想,而不是在code中的按钮来添加我的自定义GUI纹理按钮(名为OrbitBtn),所以触摸,启动3D模型围绕其旋转的斧头

I want instead of the button in the code to add my custom GUI Texture button (named OrbitBtn), so on touch, to start the 3d model rotating around its axe.

请,给我如何实现这一功能的一些建议。谢谢大家提前为你的答案。

Please, give me some advice on how to achieve that functionality. Thank you all in advance for your answers.

推荐答案

只是改变纹理btnTexture1,或者您也可以点击和拖放图像文件到你的脚本,因为你已经宣布btnTexture1为public。

Just change the texture btnTexture1, or you can click and drag-drop image files to your scripts, since you have declared btnTexture1 as public.

或者,如果你想要做同样的在JavaScript这里是样本code。

Or if you want to do the same in javascript here is the sample Code.

if(GUI.Button(Rect(Screen.width*.85,Screen.height*0,width,width),Resources.Load("pause") as Texture))
{

//Button press action
}

因此​​,在这个code样品,我到按钮加载图像(暂停。*)。请确保你有一个在你的资源文件夹名为资源文件夹内的图像。

So in this sample code, I am loading a image (pause.*) on to the button. Make sure that you have the image inside a folder called "Resources" in your asset folder.

这篇关于如何在GUI纹理按钮链接到Unity3d脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆