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. 我告诉你MSDN

    https://msdn.itellyou.cn/ 微软的软件,系统,官方下载

  2. thinkphp去重统计数据sql

    DISTINCT 方法用于返回唯一不同的值 官方文档给出的示例: $Model->distinct(true)->field('userName')->select(); 解析的SQ ...

  3. weblogic生成和配置SSL证书

    采用Linux自带的OPENSSL进行证书的生成工作 准备阶段 以root登录,同时在el01gbcn01上完成 [oracle@el01gbcn01]# which java /u01/FMW/we ...

  4. NGUI自适应屏幕分辨率

    unity官方承诺的新ui系统一直没有推出来,我们的UI使用的是原生的OnGUI系统,刚好UI需要改版,索性就想迁到NGUI上面来,于是看了一下NGUI源码,发现NGUI可以大大的降低DrawCall ...

  5. hdu1227 Fast Food

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1227 #include <iostream> #include <cstdio&g ...

  6. Android面试题(2)

    1.activity的生命周期 方法 描述 可被杀死 下一个 onCreate() 在activity第一次被创建的时候调用.这里是你做所有初始化设置的地方──创建视图.设置布局.绑定数据至列表等.如 ...

  7. JS 数字左补零函数

    /* 左边自动补零 质朴长存法 by lifesinger */ function pad(num, n) { var len = num.toString().length; while(len & ...

  8. 修改tcp数据内容

    http://blog.sina.com.cn/s/blog_6f0c85fb0100xi1x.html 2.6内核基于NetFilter处理框架修改TCP数据包实现访问控制 参考上面的钩子函数,结合 ...

  9. POJ2406-Power Strings(kmp循环节)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31111   Accepted: 12982 D ...

  10. cpu时间 / cpu利用率计算

    CPU时间即反映CPU全速工作时完成该进程所花费的时间 cpu时间计算CPU TIME = (# of CPU Clock Cycles) x Clock Period     // “#” 表示消耗 ...