• 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 = 256;
        public static float _gridZ = 256;
        public static float _bottomHeight = -327;
        public static float _hafRayLength = 500;
    
        [MenuItem("Tools/GenerateGrid")]
        private static void GenerateGrid()
        {
            Scene scene = SceneManager.GetActiveScene();
            if (!scene.IsValid())
            {
                EditorUtility.DisplayDialog("1", "2", "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 * 2, maskLayerHeight);
                    if (isHit)
                    {
                        height = (float)Math.Round(rayHit.point.y, 2) * 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, 0);
                Marshal.Copy(heightPtr, heightData, 0, heightData.Length);
            }
            else
            {
                Debug.Log("read height Asset error");
                return;
            }
    
            for (int j = 0; 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;
        }
    }
  • 相关阅读:
    无缝世界场景加载的解决方案研究
    3D物体绘制不见
    dx sdk中关于常用dx api的performace性能参数
    OpenGL/DirectX渲染技巧集
    每天送你一個simle
    [原创] 一种页面数据错误输入提示方法
    [原创] ASP.NET 中如何弹出提示窗口然后导向另外一个页面
    [原创] 部署含有ReportView的控件的ASPX页面时出现错误
    公布一个简单的日志记录方法
    [原创] 如何在没有ASP.NET AjaxEnabled Web Site 向导的情况下加入Ajax支持
  • 原文地址:https://www.cnblogs.com/dabiaoge/p/9016160.html
Copyright © 2020-2023  润新知