许可优化
产品
解决方案
服务支持
关于
软件库
当前位置:服务支持 >  软件文章 >  unity3D之系统代码的编写

unity3D之系统代码的编写

阅读数 6
点赞 0
article_banner

一、脚本代码

1.创建SingleTon.cs

代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SingleTon<T> : MonoBehaviour where T : SingleTon<T>
{
    private static T instance;
    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType<T>();
                if (instance == null)
                {
                    instance = new GameObject("_" + typeof(T).Name).AddComponent<T>();
                    DontDestroyOnLoad(instance);
                }
            }
            return instance;
        }
        set { }
    }
}


csharp
运行
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

2.创建JsonHelper.cs

代码:

using UnityEngine;
using System.Collections;
using LitJson;
using System.Text;   //使用StringBuilder
using System.IO;     //使用文件流

public class JsonHelper : MonoBehaviour
{
    public static string GetJson()
    {
        StringBuilder sb = new StringBuilder();
        JsonWriter writer = new JsonWriter(sb);

        bool mode = GameManager.Instance.mode;
        int totalMoves = GameManager.Instance.totalMoves;
        int[,] chessBoard = GameManager.Instance.chessBoard;
        MapData map = new MapData();
        map.mode = mode;
        map.totalMoves = totalMoves;
        map.chessBoard = new int[3 * 3];
        string str = "";    // 打印用
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                map.chessBoard[i * 3 + j] = chessBoard[i, j];
                str = str + " " + chessBoard[i, j].ToString();
            }
        }
        writer.WriteObjectStart();//字典开始
        writer.WritePropertyName("Map");  // 记录宽度
        writer.Write(JsonMapper.ToJson(map));
        writer.WriteObjectEnd();

        return sb.ToString();  //返回Json格式的字符串
    }

    //保存Json格式字符串
    public static void SaveJsonString(string JsonString)
    {
        FileInfo file = new FileInfo(Application.persistentDataPath + "/JsonData.Json");
        StreamWriter writer = file.CreateText();
        writer.Write(JsonString);
        writer.Close();
        writer.Dispose();
    }

    //从文件里面读取json数据
    public static string GetJsonString()
    {
        try
        {
            StreamReader reader = new StreamReader(Application.persistentDataPath + "/JsonData.Json");
            string jsonData = reader.ReadToEnd();
            reader.Close();
            reader.Dispose();
            return jsonData;
        }
        catch
        {
            return "";
        }
    }
}

public class MapData
{
    public bool mode;
    public int totalMoves;
    public int[] chessBoard;
}



csharp
运行
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删
相关文章
QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空