using System.IO; #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; namespace H2Engine { public class Bhv_SceneObjectData : MonoBehaviour { [HideInInspector] public string resName; public bool isObstacle = false; #if UNITY_EDITOR public void Save(BinaryWriter bw) { bw.Write(resName); Debug.LogFormat("resName: {0}", resName); bw.Write(isObstacle); Debug.LogFormat("isObstacle: {0}", isObstacle); bw.Write((float)System.Math.Round(transform.position.x, 2)); Debug.LogFormat("x: {0}", (float)System.Math.Round(transform.position.x, 2)); bw.Write((float)System.Math.Round(transform.position.y, 2)); Debug.LogFormat("y: {0}", (float)System.Math.Round(transform.position.y, 2)); bw.Write((float)System.Math.Round(transform.position.z, 2)); Debug.LogFormat("z: {0}", (float)System.Math.Round(transform.position.z, 2)); bw.Write((float)System.Math.Round(transform.eulerAngles.y, 2)); Debug.LogFormat("angle: {0}", transform.eulerAngles.y); } public void Load(BinaryReader br) { // 加载逻辑原因在父节点进行读取 // resName = br.ReadString(); isObstacle = br.ReadBoolean(); float _x = br.ReadSingle(); float _y = br.ReadSingle(); float _z = br.ReadSingle(); float _eurlerAngle = br.ReadSingle(); name = "RefreshSceneObject_" + resName; transform.position = new Vector3(_x, _y, _z); transform.eulerAngles = new Vector3(0, _eurlerAngle, 0); } public void Export(BinaryWriter bw) { Save(bw); } private Bhv_SceneObjectData CreateSceneObject(string sceneName = null) { if (!string.IsNullOrEmpty(sceneName)) { string _path = "Assets/ResourcesOut/Gmodels/Prefab_Race_" + sceneName + ".prefab"; var _obj = AssetDatabase.LoadAssetAtPath(_path); if (!_obj) { Debug.LogError("所要创建的资源不存在: " + _path); return null; } _obj = Instantiate(_obj); var _sceneObjData = _obj.AddComponent(); _sceneObjData.resName = sceneName; RaycastHit _hit; Ray _ray = SceneView.lastActiveSceneView.camera.ViewportPointToRay(new Vector3(.5f, .5f, 0)); if (Physics.Raycast(_ray, out _hit, 1000f, LayerUtility.WalkbleMask)) { _sceneObjData.transform.position = _hit.point; } _sceneObjData.transform.SetParent(transform); _sceneObjData.transform.eulerAngles = Vector3.zero; _sceneObjData.transform.localScale = Vector3.one; Selection.activeGameObject = _sceneObjData.gameObject; if (Selection.activeGameObject) { SceneView.lastActiveSceneView.LookAt(Selection.activeGameObject.transform.position); } return _sceneObjData; } return null; } #endif } }