少年修仙传客户端代码仓库
client_Wu Xijin
2019-02-13 c7f64d977c4e2884d5411a5a2d0f37b6afa52963
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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);
        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/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
}