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; ...
随机推荐
- UVA 10163 - Storage Keepers(dp)
本文出自 http://blog.csdn.net/shuangde800 题目链接: 点击打开链接 题意 有n个仓库,让m个人来看管.一个仓库只能由一个人来看管,一个人可以看管多个仓库. 每个人 ...
- 【shiro】报错:Caused by: java.lang.ClassNotFoundException: org.apache.shiro.spring.LifecycleBeanPostProcessor
Caused by: java.lang.ClassNotFoundException: org.apache.shiro.spring.LifecycleBeanPostProcessor at o ...
- CFR Java Decompiler 反编译
java反编译,JAD & CFR 目前我们开发中大都使用JAD进行java反编译.这个工具已经过于陈旧,最突出的问题就是经常反编译出错. 使用CFR反编译工具能够很好的解决这个问题,并且支持 ...
- 用phonegap和jquery-mobile写android应用
今天纪录的是学习用phonegap和jquery-moblie来写android的过程.环境搭建. 我的习惯是直接上官网看文档,看get-started guide之类的文档.然后在看参考手册.然后就 ...
- stylus使用文档总结:选择器+变量+插值+运算符+混合书写+方法
建立好项目后我们来安装stylus npm install stylus stylus-loader --save-dev 这样就安装上了stylus. 接下来就可以使用了,使用方式分两种.一种是在. ...
- Maven Web项目配置Mybatis
一.添加Mybatis和数据库相关的包 1 pom.xml中添加的包有mybatis,mybatis-spring,druid,MySQL-connector-Java,commons-io,refl ...
- spring的条件装配bean
1 应用程序环境的迁移 问题: 开发软件时,有一个很大的挑战,就是将应用程序从一个环境迁移到另一个环境. 例如,开发环境中很多方式的处理并不适合生产环境,迁移后需要修改,这个过程可能会莫名的出现很多b ...
- mysql5.7用户密码策略问题
密码策略问题 ERROR 1819 (HY000): Your password does not satisfy the current policy requirements 查看 mysql 初 ...
- My SqL 常用命令
1:使用SHOW语句找出在服务器上当前存在什么数据库:mysql> SHOW DATABASES;2:2.创建一个数据库MYSQLDATAmysql> CREATE DATABASE MY ...
- sublime HtmlPrettify
用sublime都快一年多了,终于找到一款称心如意的format工具,可以同时格式化 html css js. HTML-CSS-JS Prettify 如果安装的时候出现问题,多调试调试,改改参数