unity文件写入与读取
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/";
public static float _perGridSize = 0.5f;
public static float _gridX = ;
public static float _gridZ = ;
public static float _bottomHeight = -;
public static float _hafRayLength = ; [MenuItem("Tools/GenerateGrid")]
private static void GenerateGrid()
{
Scene scene = SceneManager.GetActiveScene();
if (!scene.IsValid())
{
EditorUtility.DisplayDialog("", "", "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 * , maskLayerHeight);
if (isHit)
{
height = (float)Math.Round(rayHit.point.y, ) * 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();
} private static short[] heightData;
[MenuItem("Tools/ReadGrid")]
private static void ReadGrid()
{
TextAsset heightAsset = (TextAsset)GetByteAsset("GridTest");
if (heightAsset)
{
heightData = new short[(int)(_gridX / _perGridSize) * (int)(_gridZ / _perGridSize)];
System.IntPtr heightPtr = Marshal.UnsafeAddrOfPinnedArrayElement(heightAsset.bytes, );
Marshal.Copy(heightPtr, heightData, , heightData.Length);
}
else
{
Debug.Log("read height Asset error");
return;
} for (int j = ; j < heightData.Length; j++)
{
//Debug.Log(heightData[j] * 0.01f);
}
} private static 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;
}
}
unity文件写入与读取的更多相关文章
- Python学习笔记——文件写入和读取
1.文件写入 #coding:utf-8 #!/usr/bin/env python 'makeTextPyhton.py -- create text file' import os ls = os ...
- 【PHP】文件写入和读取详解
文章提纲: 一.实现文件读取和写入的基本思路 二.使用fopen方法打开文件 三.文件读取和文件写入操作 四.使用fclose方法关闭文件 五.文件指针的移动 六.Windows和UNIX下的回车和换 ...
- Java文件写入与读取实例求最大子数组
出现bug的点:输入数组无限大: 输入的整数,量大: 解决方案:向文件中输入随机数组,大小范围与量都可以控制. 源代码: import java.io.BufferedReader; import j ...
- PHP 文件写入和读取(必看篇)
文章提纲: 一.实现文件读取和写入的基本思路 二.使用fopen方法打开文件 三.文件读取和文件写入操作 四.使用fclose方法关闭文件 五.文件指针的移动 六.Windows和UNIX下的回车和换 ...
- sql注入文件写入和读取
系统固定文件路径:https://blog.csdn.net/ncafei/article/details/54616826 /etc/passwd c:/windows/win.ini 文件读取使用 ...
- c语言 文件写入和读取
#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 10 struct studen ...
- 从PCD文件写入和读取点云数据
(1)学习向PCD文件写入点云数据 建立工程文件ch2,然后新建write_pcd.cpp CMakeLists.txt两个文件 write_pcd.cpp : #include <iostr ...
- Java 通过 BufferReader 实现 文件 写入读取 示例
package com.javatest.techzero.gui; import java.io.BufferedReader; import java.io.File; import java.i ...
- 文件操作ofstream,open,close,ifstream,fin,依照行来读取数据, fstream,iosin iosout,fio.seekg(),文件写入和文件读写,文件拷贝和文件
1.ofstream,open,close 写入文件 #include<iostream> #include<fstream> using namespace std; ...
随机推荐
- HDU 3473 Minimum Sum(划分树)
Minimum Sum Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- NHibernate 之数据操作 (第五篇)
数据操作,在这里主要介绍INSERT.UPDATE.DELETE.我们在使用NHibernate的时候,如果只是查询数据,不需要改变数据库的值,那么是不需要提交或者回滚到数据库的. 一.INSERT ...
- 如何使用 DBCC MEMORYSTATUS 命令来监视 SQL Server 2005 中的内存使用情况
https://technet.microsoft.com/en-us/solutionaccelerators/dd537566.aspx 注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完 ...
- Hibernate 注解@Column(nullable = false) 和 @Column(unique=true)
unique=true是指这个字段的值在这张表里不能重复,所有记录值都要唯一,就像主键那样; nullable=false是这个字段在保存时必需有值,不能还是null值就调用save去保存入库;
- [转]sa不能远程连接sql server 2008的解决办法
本文转自:http://www.cnblogs.com/chendaoyin/archive/2012/08/25/2656900.html 方法: 开始->Microsoft SQL Serv ...
- 阿里巴巴Android开发手册(规约)
阿里巴巴Android开发手册(规约) 学习了:https://www.cnblogs.com/jb2011/p/8487889.html 这个猛 https://blog.csdn.net/ali ...
- sprintf、vsprintf、sprintf_s、vsprintf_s、_snprintf、_vsnprintf、snprintf、vsnprintf 函数辨析
看了题目中的几个函数名是不是有点头晕?为了防止以后总在这样的细节里纠缠不清,今天我们就来好好地辨析一下这几个函数的异同. 实验环境: Windows下使用VS2017Linux下使用gcc4.9.4 ...
- 嵌入web字体
@font-face模块 可以帮助我们轻松解决Web页面中使用优雅字体的方式,而且我们可以根据需要自定义多种字体,但是每个@font-face只能定义一种,若需要多个字体就启用多个@f ...
- JavaScript 中的string 方法
创建string的方法 var str ="abc"; var str = new String("abc"); var str = String(" ...
- 【Spark】Spark的Standalone模式安装部署
Spark执行模式 Spark 有非常多种模式,最简单就是单机本地模式,还有单机伪分布式模式,复杂的则执行在集群中,眼下能非常好的执行在 Yarn和 Mesos 中.当然 Spark 还有自带的 St ...