【问题标题】:How do I play an animation for twenty-seven seconds or more and load a scene in unity3d如何播放 27 秒或更长时间的动画并在 unity3d 中加载场景
【发布时间】:2016-09-07 08:30:58
【问题描述】:

现在我已经设置了脚本,可以在玩家健康状况不佳时切换场景。我想要的是在 28 秒后首先播放死亡动画而不是场景加载。我将触发死亡动画。 die 动画剪辑是 26.0 。加载场景将是游戏结束场景。游戏结束场景需要加载 28.0 或 29.0 。他们周围的某个地方。这是我的代码:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

[System.Serializable]
[RequireComponent(typeof(SpriteDatabase))]
public class Healthbar : MonoBehaviour {


    public int fontSize;

    public static int playersHealth;

    public int health;
    int healthNormalized;
    GameObject player;

    Image frame;
    Image bar;

    public int displayCritical;
    public int displayRedBar;
    public int displayYellowBar;
    public string healthMessage;
    public string criticalMessage = "Critical";
    public string playerTag;

    Text Message;
    Text Critical;

    public bool showHealthValue;
    public bool showCritical;

    public string sceneToLoad = "T";

    SpriteDatabase sd;

    public Theme chosenTheme;
    public FontNames chosenFont;

    int myTheme;
    int myFontTheme;

    public enum Positioning {
        TopLeft,
        TopRight,
        BottomLeft,
        BottomRight
    }

    [HideInInspector]
    public bool alive = true;

    //For demo purposes, store player's initial transform (so later it can be respawned there)
    Vector3 startPos;

    //used to choose between left or right alignment
    public Positioning positioning;

    //On Start, assign SpriteDatabse class to 'sd'. (Note: That class can never be missing due to the dependency system)
    //It then runs Debugger() (find it below.) It checks whether the required sprites are assigned in the inspector, etc.
    //Then, it builds hierarchy for GUI (find below)
    void Start(){
        sd = GetComponent<SpriteDatabase>();
        fontSize = Mathf.Clamp(fontSize, 5, 30);
        Debugger();
        BuildHierarchy();
        startPos = player.transform.position;
    }


    //Converts health integer to float value and updates it every frame.
    //Keeps the GUI bar (image) fill amount value synchronized with the health value.
    //Note: healthNormalized cuts the number so that it's on a 100 scale like in every game (it's basically the percentage)
    void FixedUpdate(){

        if (player) {
            if (alive) {
/*
                if (healthNormalized <= 0) {
                    alive = false;
                    die();  
                }
*/
                healthNormalized = health/10;
                //Converts health value to a float (range 0-1) so it can be used for image.fillamount
                float healthValue = health * 0.001f;
                healthValue = Mathf.Clamp(healthValue, 0, 1);

                //Checks if it's time to turn the bar color to red or yellow (replace the sprite basically)
                CheckForBarColor();

                bar.fillAmount = healthValue;
            }

            DisplayText();

        }
        else
            player = GameObject.FindGameObjectWithTag("Player");
    }

    void DisplayText(){
        if (showHealthValue)
            Message.text = healthMessage + ": " + healthNormalized.ToString();
        if (healthNormalized <= displayCritical && alive && showCritical) {
            Critical.enabled = true;
        }
        else
            Critical.enabled = false;
    }

    //Called by every object affecting player's health.
    //Class that calls it: ApplyDamage
    //See that for more info on how to use it!
    public void ModifyHealth(int amount) {
        if (alive)
            health = health - amount;

        if  (health <= 0) {
            Debug.Log("1: sceneToLoad = " + sceneToLoad);
            if  ((sceneToLoad != "") && (SceneManager.GetSceneByName(sceneToLoad) != null)) {
                Debug.Log("2: sceneToLoad = " + sceneToLoad);
                SceneManager.LoadScene(sceneToLoad);
            }
        }
        else {
            health = Mathf.Clamp(health, 0, 1000);
        }
     }
    }

【问题讨论】:

    标签: c# animation unity3d load


    【解决方案1】:

    将SceneManager.LoadScene(sceneToLoad); 放入另一个函数中,然后使用Invoke("myfunction",25); 调用该函数。它将等待25 秒,然后调用myfunction,然后调用SceneManager.LoadScene(sceneToLoad); 加载您的场景。

    您也可以启动一个协程并使用yield return new WaitForSeconds(25f); 等待,然后执行SceneManager.LoadScene。

    至于您的代码,将 ModifyHealth 函数替换为以下函数:

    public void ModifyHealth(int amount)
    {
        if (alive)
            health = health - amount;
    
        if (health <= 0)
        {
            Debug.Log("1: sceneToLoad = " + sceneToLoad);
            if ((sceneToLoad != "") && (SceneManager.GetSceneByName(sceneToLoad) != null))
            {
                Debug.Log("2: sceneToLoad = " + sceneToLoad);
                //Play your animation
    
                //Call loadNewScene after 25 seconds
                Invoke("loadNewScene",25);
            }
        }
        else
        {
            health = Mathf.Clamp(health, 0, 1000);
        }
    }
    
    void loadNewScene()
    {
        SceneManager.LoadScene(sceneToLoad);
    }
    

    【讨论】:

    • 动画是混合树的一部分。没有不能让它工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多