【问题标题】:Unity Game ProgrammingUnity 游戏编程
【发布时间】:2012-04-25 21:08:48
【问题描述】:

我的场景中有一个 GUI 文本对象,我希望它显示我为该剧留下的剩余生命。由于某种原因,我似乎无法让它工作。我有下面的代码,有人可以帮我吗?!

// the sound to play when the player is shot
public var shotSound:AudioClip;

// the number of lives
public var lives:int = 3;


/**
    Player has been shot
*/
function Shot () 
{
    // play the shot audio clip
    audio.PlayOneShot(shotSound);

    // reduce lives
    lives--;

    // reload the level if no lives left
    if (lives == 0)
    {
        // destroy the crosshair
        Destroy(GetComponent(CrossHair));

        // add the camera fade (black by default)
        iTween.CameraFadeAdd();

        // fade the transparency to 1 over 1 second and reload scene once complete
        iTween.CameraFadeTo(iTween.Hash("amount", 1, "time", 1, "oncomplete", "ReloadScene", "oncompletetarget", gameObject));
    }
}


/**
    Reload the scene
*/ 
function ReloadScene()
{
    // reload scene
    Application.LoadLevel("MainMenu");
}

【问题讨论】:

  • 怎么不行?详细信息会帮助那些想要提供帮助的人。
  • 场景上的GUI文字基本不会更新。它保持它设置的通用文本。我在更新函数中使用了 (guiText.text = "Lives Remaining: "+lives;) 代码,并使其成为 GUI 文本的一个组件,但它似乎不起作用?
  • 您是否有机会发布您尝试过的一些(任何)代码?我什至没有在这段代码中看到update(),也没有看到对guiText 的任何引用。也许我遗漏了一些东西,但我认为如果有人要帮助这个,我需要更多信息。
  • 代码中没有更新功能,我需要把它放进去然后把 (guiText.text = "Lives Remaining: "+lives;) 放进去吗?我想我确实尝试过,但它似乎没有用。

标签: user-interface text unity3d


【解决方案1】:

试试下面的代码。通过转到 GameObject->Create Other->GUI Text 创建 GUIText 对象。现在将它拖到检查器面板中下面脚本的 playerLives 字段。它应该可以工作。

// the sound to play when the player is shot
public var shotSound:AudioClip;

public var GUIText playerLives;

// the number of lives
public var lives:int = 3;

function OnGUI ()
{
    playerLives.Text = lives.ToString();
}
/**
    Player has been shot
*/
function Shot () 
{
    // play the shot audio clip
    audio.PlayOneShot(shotSound);

    // reduce lives
    lives--;

    // reload the level if no lives left
    if (lives == 0)
    {
        // destroy the crosshair
        Destroy(GetComponent(CrossHair));

        // add the camera fade (black by default)
        iTween.CameraFadeAdd();

        // fade the transparency to 1 over 1 second and reload scene once complete
        iTween.CameraFadeTo(iTween.Hash("amount", 1, "time", 1, "oncomplete", "ReloadScene", "oncompletetarget", gameObject));
    }
}


/**
    Reload the scene
*/ 
function ReloadScene()
{
    // reload scene
    Application.LoadLevel("MainMenu");
}

【讨论】:

    猜你喜欢
    • 2017-08-15
    • 1970-01-01
    • 2011-07-07
    • 2021-10-29
    • 2019-02-10
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多