首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Unity游戏编程

Unity游戏编程
EN

Stack Overflow用户
提问于 2012-04-13 21:37:46
回答 1查看 1.1K关注 0票数 0

我在我的场景中有一个GUI文本对象,我想让它显示我为这部剧留下的剩余生命。由于某些原因,我似乎不能让它工作。我有下面的代码,有人能帮帮我吗?!

代码语言:javascript
运行
复制
// 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");
}
EN

回答 1

Stack Overflow用户

发布于 2012-04-14 17:49:33

试试下面的代码。通过游戏对象->创建其他->图形用户界面文本来创建GUIText对象。现在将其拖动到检查器面板中以下脚本的playerLives字段中。应该能行得通。

代码语言:javascript
运行
复制
// 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");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10141906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档