zoukankan      html  css  js  c++  java
  • 加载场景不销毁的实现

    GameObject a; 

     GameObject.DontDestroyOnLoad(a);

    源码,具体在案例中应用为:

    第一段代码中定义了UserName,而方法中又有许多操作,如果在Update里加入上面的代码就会在影响下一个场景的操作,所以就必须封装一下,来解决这个问题!!!

     1 using System.Collections;
     2 using System.Collections.Generic;
     3 using UnityEngine;
     4 using UnityEngine.SceneManagement;
     5 
     6 public class S0Script : MonoBehaviour {
     7 
     8     //按下键盘q键,向持久化里面写入数据 张三,0
     9     //如果这个时候有张三这个key就代表已经有分数就不设置了 或里面的value不是0
    10     // Use this for initialization
    11 
    12     public string UserName;
    13     void Awake()
    14     {
    15 
    16         UserName = "zhangsan";
    17     }
    18     
    19     // Update is called once per frame
    20     void Update () {
    21         if (Input.GetKeyDown(KeyCode.Q))
    22         {
    23             if (PlayerPrefs.HasKey("zhangsan"))
    24             {
    25                 Debug.LogWarning("已经注册,该操作不会重置分数");
    26             }
    27             else
    28             {
    29                 PlayerPrefs.SetInt("zhangsan",0);
    30             }
    31         }
    32 //这里应用到了playerprefs,跟字典类似,是键值对组成的(key,value)key唯一,value不唯一

    33 if (Input.GetKeyDown(KeyCode.Space)) 34 { 35 //加载场景 36 //index string 37 SceneManager.LoadScene("S1"); 38 } 39 40 } 41 }

     第二段代码:

    public class S0UserInfo : MonoBehaviour {
    
        // Use this for initialization
        private string _username;
    //此处封装了一下,是可读取的;
        public string Username
        {
            get { return _username; }
        }
    
        void Start ()
        {
            _username = GameObject.Find("Manager").GetComponent<S0Script>().UserName;
            DontDestroyOnLoad(this);
    
        }
        
        // Update is called once per frame
        void Update () {
            
        }
    }

    如果在用名字就在其他的脚本引用就好了!

  • 相关阅读:
    python中os模块和sys模块的常见用法
    Python 十进制转换为二进制 高位补零
    Invalid prop: custom validator check failed for prop "pagerCount"的报错
    Centos7 安装使用virtualenvwrapper
    如何使用CORS解决跨域问题
    内置模块
    文件操作的相关
    小数据池的概念
    set()集合的概念与一般操作
    有关于dict(字典)的特性与操作方法
  • 原文地址:https://www.cnblogs.com/Future-Better/p/9829902.html
Copyright © 2011-2022 走看看