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文件写入与读取的更多相关文章

  1. Python学习笔记——文件写入和读取

    1.文件写入 #coding:utf-8 #!/usr/bin/env python 'makeTextPyhton.py -- create text file' import os ls = os ...

  2. 【PHP】文件写入和读取详解

    文章提纲: 一.实现文件读取和写入的基本思路 二.使用fopen方法打开文件 三.文件读取和文件写入操作 四.使用fclose方法关闭文件 五.文件指针的移动 六.Windows和UNIX下的回车和换 ...

  3. Java文件写入与读取实例求最大子数组

    出现bug的点:输入数组无限大: 输入的整数,量大: 解决方案:向文件中输入随机数组,大小范围与量都可以控制. 源代码: import java.io.BufferedReader; import j ...

  4. PHP 文件写入和读取(必看篇)

    文章提纲: 一.实现文件读取和写入的基本思路 二.使用fopen方法打开文件 三.文件读取和文件写入操作 四.使用fclose方法关闭文件 五.文件指针的移动 六.Windows和UNIX下的回车和换 ...

  5. sql注入文件写入和读取

    系统固定文件路径:https://blog.csdn.net/ncafei/article/details/54616826 /etc/passwd c:/windows/win.ini 文件读取使用 ...

  6. c语言 文件写入和读取

    #include<stdio.h> #include<stdlib.h> #include<string.h> #define N 10 struct studen ...

  7. 从PCD文件写入和读取点云数据

    (1)学习向PCD文件写入点云数据 建立工程文件ch2,然后新建write_pcd.cpp  CMakeLists.txt两个文件 write_pcd.cpp : #include <iostr ...

  8. Java 通过 BufferReader 实现 文件 写入读取 示例

    package com.javatest.techzero.gui; import java.io.BufferedReader; import java.io.File; import java.i ...

  9. 文件操作ofstream,open,close,ifstream,fin,依照行来读取数据, fstream,iosin iosout,fio.seekg(),文件写入和文件读写,文件拷贝和文件

     1.ofstream,open,close 写入文件 #include<iostream> #include<fstream> using namespace std; ...

随机推荐

  1. Linux命令-挂载命令:mount

    设置虚拟机放入光盘,并且选中“已连接” mkdir /mnt/cdrom 设置光盘目录 ll /dev/cdrom 查看cdrom的软连接长格式信息 mount -t iso9660 /dev/sr0 ...

  2. Clover 的功能就是给资源管理器加上 Chrome 一样的标签页

    官网下载地址:http://cn.ejie.me/

  3. gridview展开嵌套显示

    最近实在是太忙了,好几个月没有更新博客了,近来项目需要用到GRIDVIEW嵌套的,在这里跟大家分享一下,大家如有更好的解决方案,请不吝贴出.在ASP.NET中,GridView嵌套可以显示当前选定的父 ...

  4. 认识udev

    转:http://www.360doc.com/content/11/0415/21/1317564_109923795.shtml 因为本身从事存储行业,在工作中多次碰到用户有这样的要求:我的lin ...

  5. TStringList的DelimitedText的空格问题

    TStringList的DelimitedText的空格问题 如果DelimitedText的字符串中有空格,TStringList遇到空格会进行自动换行,而这显然不是我们想要的结果.经测试发现D7~ ...

  6. luci范例

    转自:http://www.cnblogs.com/souroot/p/4511760.html LuCI (Lua Configiration Interface) 是OpenWRT 的Web 管理 ...

  7. Linux上安装使用SSH(ubuntu&&redhat)

    转自:http://blog.csdn.net/zxs9999/article/details/6930736 Ubuntu安装使用SSH ubuntu默认并没有安装ssh服务,如果通过ssh链接ub ...

  8. easyui combobox可编辑的情况下,只能首字母开始过滤的问题选项

    esayui中的combobox下拉列表在可编辑的情况下可以输入内容来过滤下拉框的选项,而1.4.2版本只能在从首字母开始过滤例如其中一个选项是"test", 只能输入" ...

  9. mysql: Error Codes and Messages

    Appendix B. Error Codes and MessagesTable of Contents B.1. Server Error Codes and MessagesB.2. Clien ...

  10. CardLayout使用

    相对于BoxLayout,GridBugLayut等常用的Swing layout,CardLayout是特殊的,前者是一个容器内布置组件,而后者是在一个容器内放置很多页面(但一个时间只用显示一个). ...