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 PHP 编译参数详解(一)

    Fast-CGI: ./configure --prefix=/usr/local/php --enable-fastcgi --enable-force-cgi-redirect --with-co ...

  2. 怎样打开查看mysql binlog

    1 在my.ini(window)配置文件里面 [mysqld]log-bin=mysql-bin(名字可以随便起) 我们每次进行操作的时候,File_size都会增长 2.show binlog e ...

  3. 微服务:spring-cloud-archaius 起步

    原文:http://blog.csdn.net/qq_18675693/article/details/53337941 微服务:spring-cloud-archaius 起步 原创 2016年11 ...

  4. Struct2_使用Ajax调用Action方法并返回值

    一.Login.jsp 1.<head>引入jquery: <script type="text/javascript" src="http://aja ...

  5. Linux用来抗衡Win的那些桌面环境

    作为一个 Linux 的爱好者,参加了 9 月 22 日 Linux Deepin 在北京举行的用户与开发者大会(给大家一个链接).名为参加会议,实为打酱油.但 Linux Deepin 团队的一些理 ...

  6. 五种算法实现IP到地址的转换

    条件: 给出一个文件,其中每行一个IP段(IPv4,其实IPv6类似,只是规模剧增)及其对应的信息(例如物理地址信息),内容及格式为: <start_IP> <end_IP> ...

  7. JAVA应用获取本机IP

    在应用开发的时候如何获取本机的IP呢? 本人在开发一个线上系统的时候有这样的需求,办法很简单啊,google一下,于是乎有了下面的代码: 方案一: Enumeration<NetworkInte ...

  8. 自定义标签(JspFragment类、invoke方法、开发带属性的标签)

    自定义标签(JspFragment类.invoke方法.开发带属性的标签) 一.JspFragment类 javax.servlet.jsp.tagext.JspFragment类是在JSP2.0中定 ...

  9. 机器学习:K-近邻算法(KNN)

    机器学习:K-近邻算法(KNN) 一.KNN算法概述 KNN作为一种有监督分类算法,是最简单的机器学习算法之一,顾名思义,其算法主体思想就是根据距离相近的邻居类别,来判定自己的所属类别.算法的前提是需 ...

  10. Spring框架学习(1)Spring简介

    内容源自:Spring 框架简介 Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序 ...