예제 #1
0
    /// <summary>
    /// 创建场景对象
    /// </summary>
    /// <param name="objInfo"></param>
    public void CreateObject(int objectIndex, int serverID, ProtoCreateObject sync)
    {
        bool isPreSet = objectIndex >= 0;
        int  index    = isPreSet ? objectIndex : -objectIndex;

        if (index < cratePrefabs.Length)
        {
            GameObject prefab = cratePrefabs[index];
            if (prefab == null)
            {
                Debug.LogError("生成AI:无效对象索引" + objectIndex);
                return;
            }
            GameObject go = GameObject.Instantiate(prefab);
#if UNITY_EDITOR
            go.name = "" + serverID;
#endif
            if (!go.activeSelf)
            {
                go.SetActive(true);
            }
            //获取/初始化配置数据
            BaseAI ai = go.GetComponent <BaseAI>();
            if (ai != null)
            {
                if (Connection.GetInstance().isHost == true)
                {
                    AppearObjectData objCfg = AICreater.GetObjectCfg(sync.hashCode);
                    if (objCfg != null)
                    {
                        ai.InitAIData(objCfg);
                    }
                }
                sceneModel.RegisterAI(ai);
            }

            //设置同步组件
            SceneGameObject sgo = go.GetComponent <SceneGameObject>();
            if (sgo != null)
            {
                ProtoPlayerInfo selfInfo = playerModel.GetPlayerInfo();
                if (selfInfo != null)
                {
                    sgo.SetSyncStatus(objectIndex, serverID, true, selfInfo.pos, sync.GetPos(), sync.GetRot());
                    sceneModel.AddSceneObject(sgo);
                }
            }
        }
        else
        {
            Debug.LogError("生成AI:无效对象索引" + objectIndex);
        }
    }