簡體   English   中英

如何將GUI Texture按鈕鏈接到Unity3d中的腳本

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM