zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    bzoj3729-Gty的游戏【Splay,博弈论】
    pkusc2021游记
    P4922-[MtOI2018]崩坏3?非酋之战!【dp】
    P5782-[POI2001]和平委员会【2-SAT】
    CF1511G-Chips on a Board【倍增】
    noip范围
    [LGOJ5558]心上秋(倍增)
    学习笔记——动态DP
    [NOIP校内集训]不正常的国家
    [BZOJ4712]洪水(树链剖分+DP)
  • 原文地址:https://www.cnblogs.com/dabiaoge/p/9016160.html
Copyright © 2011-2022 走看看