当前位置: 首页 > news >正文

建筑公司愿景口号大全杭州搜索引擎优化公司

建筑公司愿景口号大全,杭州搜索引擎优化公司,怎样弄一个网站,科技有限公司网站建设策划书Unity persistentDataPath使用案例 一、Application.persistentDataPath 1、概念 persistentDataPath:此属性用于返回一个持久化数据存储目录的路径,可以在此路径下存储一些持久化的数据文件;是一个可读写的目录;此文件夹在Edi…

Unity persistentDataPath使用案例

一、Application.persistentDataPath

1、概念

persistentDataPath:此属性用于返回一个持久化数据存储目录的路径,可以在此路径下存储一些持久化的数据文件;是一个可读写的目录;此文件夹在Editor阶段没有,手机安装App后自动生成;

关联的概念-Unity项目文件夹StreamingAssets:只能读,不能写。

2、应用场景

1)手机应用场景中存储当前游戏的进度,以便下次运行游戏时读取

如下代码文件,存储游戏中的下列数据coins、starCount = 0、collection

    [Serializable]

    public class SaveData

    {

        //Current loaded save file

        public static SaveData save;

        private static string fileName;

        //Unsaved global variables

        public static bool checkpoint = false;

        public static Vector3 checkpointPos;

        public static Quaternion checkpointRot;

        public static bool hubPositionSet = false;

        //Saved variables

        public int coins = 0;

        public int starCount = 0;

        public List<string> collection = new List<string>();

        //Save

        public void Save()

        {

            System.IO.File.WriteAllText(fileName, JsonUtility.ToJson(this));

        }

        //Load (Make sure fileName is set, will be loaded if it exists)

        public static bool Load()

        {

            if (System.IO.File.Exists(fileName))

            {

                save = JsonUtility.FromJson<SaveData>(System.IO.File.ReadAllText(fileName));

                hubPositionSet = true;

                return true;

            }

            else

                return false;

        }

        //Create new game

        public static void NewGame()

        {

            save = new SaveData();

            hubPositionSet = false;

        }

        public static void SetFileName(string name)

        {

            fileName = Path.Combine(Application.persistentDataPath, name + ".json");

        }

        //Null check

        public static void NullCheck()

        {

            if (save == null)

            {

                SetFileName("TestSave");

                if (!Load()) NewGame();

                Debug.Log("Hello Mario Framework: Using test save file!");

                hubPositionSet = false;

            }

        }

        //Get coin count

        public int GetCoins()

        {

            return coins;

        }

        //Add or remove coins

        public void AddCoins(int i)

        {

            coins += i;

        }

        //Collect a single coin

        public void CollectCoin()

        {

            coins++;

        }

        //Get star count

        public int GetStarCount()

        {

            return starCount;

        }

        //Collect a star (Or ignore if already collected)

        public bool CollectStar(string name)

        {

            if (AddCollection(name))

            {

                starCount++;

                return true;

            }

            else return false;

        }

        //Add to collection

        public bool AddCollection(string id)

        {

            if (!collection.Contains(id))

            {

                collection.Add(id);

                return true;

            }

            else return false;

        }

        //Check if something is in the collection

        public bool CheckCollection(string id)

        {

            return collection.Contains(id);

        }

    }

2)放置一些首次运行时需要的可读写的数据文件

将游戏运行时需要的可读写的文件模板放到只读文件夹“StreamingAssets”,手机首次运行时再拷贝到可读写的目录“Application.persistentDataPath”,

如下实例,游戏在手机平台首次运行时将我们的数据模板文件”data.txt”拷贝到“persistentDataPath”目录,以便于以后在手机端游戏运行时实时读写。

public void copy(string fileName = "data.txt")

{

    string url;

    //Mac,Windows或Linux平台

    #if UNITY_EDITOR || UNITY_STANDALONE

    url = $"file://{Application.dataPath}/StreamingAssets/{fileName}";

    //ios平台路径

    #elif UNITY_IPHONE

            url = $"file://{Application.dataPath}/Raw/{fileName}";

    //安卓路径

    #elif UNITY_ANDROID

            url = $"jar:file://{Application.dataPath}!/assets/{fileName}";

    #endif

    string persistentUrl = $"{Application.persistentDataPath}/{fileName}";

    if (!File.Exists(persistentUrl))

    {

        Debug.Log($"{persistentUrl} 文件不存在,从StreamingAssets中Copy!");

        WWW www = new WWW(url);

        while (true)

        {

            if (www.isDone)

            {

                if (www.error == null)

                {

                    //本次读的文本

                    File.WriteAllText(persistentUrl, www.text);   

                    File.WriteAllBytes(path, www.bytes);

                    Debug.Log($"持久化目录: {persistentUrl}");

                    break;

                }

                else

                {

                    Debug.LogWarning($"没得到StreamingAssets的文件 : {fileName}");

                }

            }

        }

    }

    else

    {

        Debug.Log($"{persistentUrl} 文件已存在!");

    }

}

http://www.sczhlp.com/news/35740/

相关文章:

  • 内蒙古住房和建设厅网站搜搜
  • 广州做内销鞋的网站环球网疫情最新消息
  • net的电商网站建设淘大象排名查询
  • 做网站烧钱百度关键词投放
  • 美工培训班百度网站排名优化
  • 佛山品牌推广网络营销策略优化
  • rss 网站插件seo整站优化更能准确获得客户
  • 做电商需要投入多少钱seo 是什么
  • 有没有做宠物的网站seo推广专员工作内容
  • wordpress 登陆网址重庆二级站seo整站优化排名
  • 厦门有什么网站制作公司网站收录免费咨询
  • 有什么网站可以做免费推广网络软文营销
  • 制作网站建设的seo快速排名点击
  • 作品集怎么做网站2022年最近一周新闻大事
  • 东莞p2p网站开发价钱新闻头条今日新闻
  • 网页策划方案网站关键词在线优化
  • 网站开发费属于什么费用浏览器搜索引擎大全
  • 加盟营销型网站制作网络兼职平台
  • app营销网站建设网络信息发布平台
  • 做海报可以借鉴的网站什么是seo标题优化
  • 南昌制作企业网站关键词英文
  • 界首工程建设信息网站seo排名分析
  • 系统官网网站模板下载windows优化大师官方网站
  • 淘宝网站是怎么做的企业管理咨询
  • 网站制作产品优化网站外链怎么发布
  • 建站公司网站源码社区广告商对接平台
  • 网站后台的东西都是要上传到在虚拟主机上面吗seo自学网视频教程
  • 签证网站建设网站开发公司哪家好
  • ip地址直接访问网站做博客的seo技巧
  • wordpress模板 段子seo网络推广机构