美文网首页
unity 场景切换部分代码实现。。。

unity 场景切换部分代码实现。。。

作者: Bis_12e2 | 来源:发表于2019-12-19 22:22 被阅读0次

    今在我的2d小游戏中实现了一个场景转换,逻辑如下:

    分数达到条件—触发UI———按键——延迟1s后跳转至下个场景;

    分数达到条件后触发UI的 条件代码为:(此条放在分数文本的项目上,新建canvas-UI-panel命名为enterdialog)ps,这里新建了一个public类 需要将enterdialog的ui拖至 栏中。

    
    using System.Collections;
    
    using System.Collections.Generic;
    
    using UnityEngine;
    
    using UnityEngine.UI;
    
    using UnityEngine.SceneManagement;
    
    public class gamecontroller : MonoBehaviour
    
    {
    
        public int score;
    
        public Text scoretext;
    
        public GameObject enterdialog;
    
        // Start is called before the first frame update
    
        void Start()
    
        {
    
            score = 0;
    
        }
    
        // Update is called once per frame
    
        void Update()
    
        {
    
            scoretext.text = "SCORE:" + score;
    
            if (score == 9){
    
       enterdialog.SetActive(true);//那这个名为enterdialog的小框会出现
    
    
    
            }
    
        }
    
    

    之后 新建scrip 放置在 enterdialog项目上,代码如下:

    
    using System.Collections;
    
    using System.Collections.Generic;
    
    using UnityEngine;
    
    using UnityEngine.SceneManagement;
    
    public class enterhouse : MonoBehaviour
    
    {
    
        // Start is called before the first frame update
    
        void Start()
    
        {
    
        }
    
        // Update is called once per frame
    
        void Update()
    
        {
    
            if (Input.GetKeyDown(KeyCode.Return))//或者 input.getkeycode.return 也行
    
            {
    
                Invoke("Next", 1f);//回车键按了以后 延迟1秒 这里新建了一个函数“next” next里面是具体执行办法
    
            }
    
        }
    
    
    
         void Next()
    
                {
    
                    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);//换到下一个场景(场景编号是“现在+1”)
    
                }
    
            }
    
    

    相关文章

      网友评论

          本文标题:unity 场景切换部分代码实现。。。

          本文链接:https://www.haomeiwen.com/subject/rdgvnctx.html