首页 / UNITY / unity文件写入与读取
unity文件写入与读取
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了unity文件写入与读取,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2521字,纯文字阅读大概需要4分钟。
内容图文
![unity文件写入与读取](/upload/InfoBanner/zyjiaocheng/1328/e8ea376a7956432298a953609316f6e3.jpg)
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using UnityEngine.SceneManagement; using System; using System.IO; using System.Runtime.InteropServices; public class GridEditor : EditorWindow { public static string _gridPath = "Assets/Scence/Data/Grid/"; publicstaticfloat _perGridSize = 0.5f; publicstaticfloat _gridX = 256; publicstaticfloat _gridZ = 256; publicstaticfloat _bottomHeight = -327; publicstaticfloat _hafRayLength = 500; [MenuItem("Tools/GenerateGrid")] privatestaticvoid GenerateGrid() { Scene scene = SceneManager.GetActiveScene(); if (!scene.IsValid()) { EditorUtility.DisplayDialog("1", "2", "OK"); return; } string heightPath = _gridPath + scene.name + ".bytes"; if (File.Exists(heightPath)) File.Delete(heightPath); FileStream fs_h = new FileStream(heightPath, FileMode.CreateNew); BinaryWriter bw_h = new BinaryWriter(fs_h); float haf = _perGridSize * 0.5f; float height = _bottomHeight * 100f; Vector3 rayDir = Vector3.down; Vector3 rayOrig = Vector3.zero; int maskLayerHeight = LayerMask.GetMask("Ground"); RaycastHit rayHit; bool isHit = false; for (float z = haf; z < _gridX + haf; z += _perGridSize) { for (float x = haf; x < _gridZ + haf; x += _perGridSize) { rayOrig.Set(x, _hafRayLength, z); height = _bottomHeight * 100f; isHit = Physics.Raycast(rayOrig, rayDir, out rayHit, _hafRayLength * 2, maskLayerHeight); if (isHit) { height = (float)Math.Round(rayHit.point.y, 2) * 100f; height = Mathf.Max(height, _bottomHeight * 100f); height = Mathf.Min(height, -_bottomHeight * 100f); } bw_h.Write((short)height); } } bw_h.Flush(); bw_h.Close(); fs_h.Close(); } privatestaticshort[] heightData; [MenuItem("Tools/ReadGrid")] privatestaticvoid ReadGrid() { TextAsset heightAsset = (TextAsset)GetByteAsset("GridTest"); if (heightAsset) { heightData = newshort[(int)(_gridX / _perGridSize) * (int)(_gridZ / _perGridSize)]; System.IntPtr heightPtr = Marshal.UnsafeAddrOfPinnedArrayElement(heightAsset.bytes, 0); Marshal.Copy(heightPtr, heightData, 0, heightData.Length); } else { Debug.Log("read height Asset error"); return; } for (int j = 0; j < heightData.Length; j++) { //Debug.Log(heightData[j] * 0.01f); } } privatestatic UnityEngine.Object GetByteAsset(string resName, string ext = ".bytes") { string resPath = string.Format("{0}{1}" + ext, _gridPath, resName); UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath(resPath, typeof(UnityEngine.Object)); return obj; } }
原文:https://www.cnblogs.com/dabiaoge/p/9016160.html
内容总结
以上是互联网集市为您收集整理的unity文件写入与读取全部内容,希望文章能够帮你解决unity文件写入与读取所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。