| | |
| | | using System.IO; |
| | | #if UNITY_EDITOR |
| | | using UnityEditor; |
| | | #endif |
| | | using UnityEngine; |
| | | |
| | | public class Bhv_SceneObjectData : MonoBehaviour |
| | | { |
| | | [HideInInspector] |
| | | public string resName; |
| | | public bool isObstacle = false; |
| | | |
| | | #if UNITY_EDITOR |
| | | |
| | | public void Save(BinaryWriter bw) |
| | | { |
| | | bw.Write(resName); |
| | | bw.Write(isObstacle); |
| | | bw.Write((float)System.Math.Round(transform.position.x, 2)); |
| | | bw.Write((float)System.Math.Round(transform.position.y, 2)); |
| | | bw.Write((float)System.Math.Round(transform.position.z, 2)); |
| | | bw.Write((float)System.Math.Round(transform.eulerAngles.y, 2)); |
| | | } |
| | | |
| | | public void Load(BinaryReader br) |
| | | { |
| | | resName = br.ReadString(); |
| | | name = "RefreshMonster_" + resName; |
| | | // 加载逻辑原因在父节点进行读取 |
| | | // 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/Mob/Prefab_Race_" + sceneName + ".prefab"; |
| | | var _obj = AssetDatabase.LoadAssetAtPath<GameObject>(_path); |
| | | if (!_obj) |
| | | { |
| | | Debug.LogError("所要创建的资源不存在: " + _path); |
| | | return null; |
| | | } |
| | | _obj = Instantiate(_obj); |
| | | var _sceneObjData = _obj.AddComponent<Bhv_SceneObjectData>(); |
| | | _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 |
| | | } |