Unity 跨场景数据传输

Unity 跨场景数据传输
# 使用单例模式,DontDestroyOnLoad (go);  对象保存信息

```csharp
using UnityEngine;
using System.Collections;

// 对象数据想被跨场景后保存,只能采用,DontDestroyOnLoad (go); 方法保存数据,数据保存在堆中
// 拖入场景多个,但是只有一个起作用
public class C : MonoBehaviour {
  public int age;
  private C(){
  }
  private static C _instance;
  /// <summary>
  /// Gets the instance.
  /// </summary>
  /// <returns>The instance.</returns>
  public static C GetInstance(){
    GameObject g = GameObject.Find ("InstanceC");
    if (g == null) {
      GameObject go = new GameObject ();
      go.name = "InstanceC";
      DontDestroyOnLoad (go);
      if (_instance == null) {
        if (go.GetComponent<C> () == null) {
          // 通过脚本 自动添加对象,必须先实例化,否则是空,无法保存信息,没有申请空间
           _instance = new C ();
          go.AddComponent<C> ();
        }
      } else {
        return _instance;
      }
    }
    return _instance;
  }
  // Use this for initialization
  void Start () { }

  // Update is called once per frame
  void Update () { }
}

使用静态字段

using UnityEngine;
using System.Collections;

// 继承 MONO 和不继承数据保存没区别,只是差在,能否拖入场景
// 手动拖入场景相当与做了实例化
// 即使手动拖入场景多个,单只有一个静态字段是有作用,对象数据是用多个的
public class G :MonoBehaviour {
  // 静态的是全局的,和对象没有关系;即使跨场景数据也会保存,静态数据是保存在类的,在静态区
  // 静态数据在面板显示不出来的  public static int m;
     // 对象数据,有对象才能保存对应的数据;
  G g ;
  // Use this for initialization
  void Start () {
    C.GetInstance ();
           C.GetInstance ().age = 90;

    G.m = 1000;
     g = GetComponent<G> ();
    g.Z = 100;
  }
}

使用 PlayerPrefs

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

public class D : MonoBehaviour {
  C cc;
  G g ;
  // Use this for initialization
  void Start () {
    C.GetInstance ();
           C.GetInstance ().age = 90;

    G.m = 1000;
     g = GetComponent<G> ();
    g.Z = 100;

    PlayerPrefs.SetFloat("hight",10.5f);
    PlayerPrefs.SetInt ("age", 100);
    PlayerPrefs.SetString ("name", "xixi");
  }

  // Update is called once per frame
  void Update () { }
  public void OnBtnClick() {
    SceneManager.LoadScene ("2");
  }
}
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class E : MonoBehaviour {
  public static int m;
  G g ;
  // Use this for initialization
  void Start () {
    C.GetInstance ();
     g = GetComponent<G> ();

    int age= PlayerPrefs.GetInt ("age");
    float hight =PlayerPrefs.GetFloat ("hight", 0f);
    string name = PlayerPrefs.GetString ("name");
  }

  // Update is called once per frame
  void Update () { }
  public void OnBtnClick() {
    SceneManager.LoadScene ("1");
  }
}

Read more

超棒的雅思资源!

超棒的雅思资源!

雅思真题材料地址: https://github.com/zeeklog/IETLS 感谢所有人。材料来自:@shah0150 & @kbtxwer * 超棒的雅思资源 * 雅思简介 * 听力 * 阅读 * 写作 * 口语 * 词汇 * 其他 * YouTube 频道 * [播客] (#podcasts) 雅思简介 * 什么是雅思 - 了解什么是雅思 听力 * 高级听力 * 雅思官方网站 * 考试英语 * 英国广播公司节目 * 乔治梅森大学口音学习网站 - 学习不同的口音 * 英国广播公司播客 * 英国文化协会听力练习 阅读 * 雅思提升阅读 写作 * 雅思提升写作 * 雅思从 6 分到 9 分 * 迷你雅思 口语 * Verbling 提供在线英语家教服务

By Ne0inhk
🚀Zeek.ai一款基于 Electron 和 Vite 打造的跨平台(支持 Windows、macOS 和 Linux) AI 浏览器

🚀Zeek.ai一款基于 Electron 和 Vite 打造的跨平台(支持 Windows、macOS 和 Linux) AI 浏览器

是一款基于 Electron 和 Vite 打造的跨平台(支持 Windows、macOS 和 Linux) AI 浏览器。 集成了 SearXNG AI 搜索、开发工具集合、 市面上最流行的 AI 工具门户,以及代码编写和桌面快捷工具等功能, 通过模块化的 Monorepo 架构,提供轻量级、可扩展且高效的桌面体验, 助力 AI 驱动的日常工作流程。

By Ne0inhk