Unity中建立文本保存数据

Posted SpringDragon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity中建立文本保存数据相关的知识,希望对你有一定的参考价值。

  public void CreateYunYD()
    {
        GameToolsManager.Instance.effectType = EFFECTTYPE.YunYD;
        CreateYunOrWu("Effect/yunwu-yd", Vector3.zero);
    }

    public void CreateYunZY()
    {
        GameToolsManager.Instance.effectType = EFFECTTYPE.YunZY;
        CreateYunOrWu("Effect/yunwu-zy", Vector3.zero);
    }

    public void CreateWU()
    {
        GameToolsManager.Instance.effectType = EFFECTTYPE.Wu;
        CreateYunOrWu("Effect/wu",Vector3.zero);
    }

    public GameObject CreateYunOrWu(string path,Vector3 vec)
    {
        GameObject parent;
        parent = GameObject.Find("Scene/Effect");
        effect = GameObject.Instantiate(Resources.Load(path)) as GameObject;
        effect.SetActive(true);
        effect.transform.SetParent(parent.transform);
       
        effect.transform.localPosition = vec;
        effect.name = effect.name.Replace("(Clone)",string.Empty);
        return effect;
        
    }
    /// <summary>
    /// 保存云和雾的数据
    /// </summary>
    public void SaveYunOrWu()
    {
        Transform child;
        GameObject parent;
        parent = GameObject.Find("Scene/Effect");
        if (IsExistTxt())
        {
            ClearEffectData();
        }
        for (int i=0; i< parent.transform.childCount; i++)
        {
            child = parent.transform.GetChild(i);
            EffectData effectData = new EffectData();
            effectData.effectName = child.name;
            effectData.vectorX = child.localPosition.x;
            effectData.vectorY = child.localPosition.y;
            effectData.vectorZ = child.localPosition.z;
            CreateTextFile("EffectData", System.Convert.ToString(effectData.effectName));
            CreateTextFile("EffectData", System.Convert.ToString(effectData.vectorX));
            CreateTextFile("EffectData", System.Convert.ToString(effectData.vectorY));
            CreateTextFile("EffectData", System.Convert.ToString(effectData.vectorZ));

        }
        ReadTextData("EffectData");
    }
    /// <summary>
    /// 清空文本中的数据
    /// </summary>
    public void ClearEffectData()
    {
        ClearData("EffectData");
    }
    /// <summary>
    /// txt文件是否存在
    /// </summary>
    /// <returns></returns>
    public bool IsExistTxt()
    {
        string path = Application.dataPath + "/Effect/EffectData.txt";
        if (!File.Exists(path))
        {
            return false;
        }
        return true;
    }
    /// <summary>
    /// 创建文本文件,并写入文本数据
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="strFileData"></param>
    public void CreateTextFile(string fileName, string strFileData)
    {
        string strWriteFileData;
        strWriteFileData = strFileData;
        string path = Application.dataPath + "/Effect/" + fileName + ".txt";
        if (!File.Exists(path))
        {
            StartCoroutine(CreatTxt(path));
        }        
        StreamWriter writer;
        writer = File.AppendText(path);
        writer.Write(strWriteFileData + "_");
        writer.Close();
    }
    IEnumerator CreatTxt(string path)
    {
        FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
        yield return fs;
        fs.Close();
    }
    /// <summary>
    /// 读取文本数据
    /// </summary>
    public string ReadTextData(string fileName)
    {
        string data;
        string path = Application.dataPath + "/Effect/" + fileName + ".txt";
        if (!File.Exists(path))
        {            
            return null;
        }
        StreamReader reader;
        reader = File.OpenText(path);
        data = reader.ReadToEnd();
        if (data != null)
        {
            reader.Close();
            Debug.Log(data);
            return data;
        }
        reader.Close();
        return data;
    }

    /// <summary>
    /// 清空已保存的数据文件
    /// </summary>
    public void ClearData(string fileName)
    {
        string path = Application.dataPath + "/Effect/" + fileName + ".txt";
        if (File.Exists(path))
        {
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Write);
            fs.SetLength(0);
            fs.Close();
        }
        return;
    }
    /// <summary>
    /// 删除已经加载好的云和雾
    /// </summary>
    public void DeleteYunOrWu()
    {
        GameObject parent;
        parent = GameObject.Find("Scene/Effect");
        for (int i = 0; i < parent.transform.childCount; i++)
        {
            Destroy(parent.transform.GetChild(i).gameObject);
        }
    }
    /// <summary>
    /// 特效数据
    /// </summary>
    public struct EffectData
    {
        public string effectName;
        public double vectorX;
        public double vectorY;
        public double vectorZ;
    }
    Dictionary<int, List<float>> effectData;
    Dictionary<int, string> effectNameDic;

    /// <summary>
    /// 初始化特效数据
    /// </summary>
    public void InitEffectData()
    {
        Vector3 vec;
        string name;
        effectData = GetEffectData();
        Debug.Log("进来了吗");
        if (effectData == null)
        {
            return;
        }
        for(int i = 0; i < effectNameDic.Count; i++)
        {
            Debug.Log("进来了吗?");

            name = effectNameDic[i];
            List <float> vecData = effectData[i];
            Debug.Log(vecData[1]);
            vec = new Vector3(vecData[0],vecData[1],vecData[2]);
            StartCoroutine(CreateSaveEffect(name, vec));
        }
    }
    /// <summary>
    /// 生成特效
    /// </summary>
    /// <returns></returns>
    IEnumerator CreateSaveEffect(string name,Vector3 vec)
    {
        string fullPath = "Effect/" + name;
        yield return CreateYunOrWu(fullPath, vec);        
    }

    /// <summary>
    /// 获得特效数据
    /// </summary>
    public Dictionary<int, List<float>> GetEffectData()
    {
        bool changeList = false;
        effectData = new Dictionary<int, List<float>>();
        effectNameDic = new Dictionary<int, string>();
        string effectName = null;
        List<float> vectorData = new List<float>();
        string data = ReadTextData("EffectData");
        int count = 0;
        if(data == null)
        {
            return null;
        }
        char delimiter = ‘_‘;
        string[] stringData = data.Split(delimiter);
        for (int i = 0; i < stringData.Length - 1; i++)
        {
            if (changeList)
            {
                vectorData = new List<float>();
                changeList = false;
            }
            if (i % 4 == 0)
            {
                effectName = stringData[i];
            }
            if (i % 4 == 1)
            {
               double vectorX = System.Convert.ToDouble(stringData[i]);
                vectorData.Add((float)vectorX);
            }
            if (i % 4 == 2)
            {
                double vectorY = System.Convert.ToDouble(stringData[i]);
                vectorData.Add((float)vectorY);
            }
            if (i % 4 == 3)
            {
                double vectorZ = System.Convert.ToDouble(stringData[i]);
                vectorData.Add((float)vectorZ);
                effectData.Add(count, vectorData);
                effectNameDic.Add(count, effectName);
                vectorData = new List<float>();
                count++;
                Debug.Log(count+"计数");
            }
        }
        //Debug.Log(effectData);
        //Debug.Log(effectData[2][1]+ "effectData");
        return effectData;
    }
    RaycastHit2D hit = new RaycastHit2D();
    ParticleSystem par;
    Vector3 mousePos;
    
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            GameDataManager.Instance.IsMoveCamera = false;
            mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
            hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
        }
        if (Input.GetMouseButton(0)&&hit.transform!=null&&(hit.transform.name.Equals("wu")||hit.transform.name.Equals("yunwu-yd")||hit.transform.name.Equals("yunwu-zy")))
        {
            mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
            //if (hit.transform != null && hit.transform.name.Equals("yunwu-yd") || hit.transform.name.Equals("yunwu-zy"))
            //{
            //    par = hit.transform.GetComponent<ParticleSystem>();
            //    par.Pause();
            //}
            if (hit.transform.name.Equals("wu")){
                hit.transform.position = mousePos2D;
            }
        }
        if (Input.GetMouseButtonUp(0) && hit.transform != null && (hit.transform.name.Equals("wu") || hit.transform.name.Equals("yunwu-yd") || hit.transform.name.Equals("yunwu-zy")))
        {
 
            par = hit.transform.GetComponent<ParticleSystem>();
            //Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            //Vector3 mousePos3D = new Vector3(mousePos.x, mousePos.y, 0);
            Vector3 vec = new Vector3(mousePos.x, mousePos.y, 0);

            if (hit.transform.name.Equals("yunwu-yd"))
            {
                //Vector3 vec = hit.transform.position;
                par.Stop();
                Destroy(hit.transform.gameObject);
                StartCoroutine(CreateSaveEffect("yunwu-yd", vec));
            }
            if (hit.transform.name.Equals("yunwu-zy"))
            {
                //Vector3 vec = new Vector3(mousePos.x, mousePos.y, 0);
                Destroy(hit.transform.gameObject);
                StartCoroutine(CreateSaveEffect("yunwu-zy", vec));
            }
            hit.transform.localScale = Vector3.one;

            GameDataManager.Instance.IsMoveCamera = true;
        }

    }

  

以上是关于Unity中建立文本保存数据的主要内容,如果未能解决你的问题,请参考以下文章

SQL 表已经建立好了 如何在字段中插入GUID数据

unity如何给csv文本新增行数据信息

Unity常用标签

中字头 中建二局项目一期 aop

在oracle数据库中建表

在oracle数据库中建表