//删除一个prefab实例化出来的物体 private void DeleteItem(int itemNum) { SceneItemInfo itemInfo = m_mapInfo.items[itemNum]; if (itemInfo == null) { #if UNITY_EDITOR Debug.LogWarning(string.Format("无法找到编号为{0}的itemInfo", itemNum)); #endif return; } if (!m_dicItemObject.ContainsKey(itemNum)) { #if UNITY_EDITOR Debug.LogWarning("m_dicPrefabObject中,无法找到编号为 " + itemNum + " 的prefab实例化对象"); #endif itemInfo.useCount = 0; return; } itemInfo.useCount--; //物体在各个格子被开启的总数为0,可以回收了 if (itemInfo.useCount <= 0) { GameObject gameObj = m_dicItemObject[itemNum]; m_dicItemObject.Remove(itemNum); //if (m_dicCheckBlockItem.ContainsKey(itemNum)) //{ // m_dicCheckBlockItem.Remove(itemNum); //} m_stackReleaseObject.Push(gameObj); if (itemInfo.useCount < 0) { itemInfo.useCount = 0; } } }
//创建一个物体 private void CreateItem(int itemNum, bool isSync = false) { Debug.Log(string.Format("编号为{0}的itemInfo", itemNum)); SceneItemInfo itemInfo = m_mapInfo.items[itemNum]; if (itemInfo == null) { #if UNITY_EDITOR Debug.LogWarning(string.Format("无法找到编号为{0}的itemInfo", itemNum)); #endif return; } itemInfo.useCount++; if (m_dicItemObject.ContainsKey(itemNum)) { return; } m_dicItemObject.Add(itemNum, null); UnityEngine.Object prefabResource = null; if (m_dicItemPrefab.ContainsKey(itemNum)) { prefabResource = m_dicItemPrefab[itemNum]; } if (originAssetDic.ContainsKey(itemInfo.prefabAssetPath)) { prefabResource = originAssetDic[itemInfo.prefabAssetPath]; } if (prefabResource == null) //如果字典里获取不到 { m_waitLoadNum++; //等待加载的资源数++ if (isSync) { m_waitLoadNum--; var prefabRef = AssetDatabase.LoadAssetAtPath <GameObject>(itemInfo.prefabAssetPath); if (prefabRef == null) { #if DEBUG Debug.LogError("无法加载prefab: " + itemInfo.prefabName); #endif return; } var prefabObj = prefabRef; if (prefabObj == null) { #if DEBUG Debug.LogWarning("无法加载prefab: " + itemInfo.prefabName); #endif return; } if (m_dicItemPrefab.ContainsKey(itemNum)) { m_dicItemPrefab.Remove(itemNum); } m_dicItemPrefab.Add(itemNum, prefabObj); if (!originAssetDic.ContainsKey(itemInfo.prefabAssetPath)) { originAssetDic.Add(itemInfo.prefabAssetPath, prefabObj); } InstanceGameObj(prefabObj, itemInfo); } else { // AssetDatabase.LoadAssetAtPath<GameObject>(TestScenePrefabsDirPath + itemInfo.prefabName, prefab => // { // m_waitLoadNum--; // if (prefab == null) // { //#if DEBUG // Debug.LogWarning("无法加载prefab: " + itemInfo.prefabName); //#endif // return; // } // if (m_dicItemPrefab.ContainsKey(itemNum)) // m_dicItemPrefab.Remove(itemNum); // m_dicItemPrefab.Add(itemNum, prefab); // InstanceGameObj(prefab, itemInfo); // }); } } else //如果字典里获取到了 { InstanceGameObj(prefabResource, itemInfo); //m_instanceList.Enqueue(() => { }); //if (!m_isWatchUpdate) //{ // m_eventModule.WatchUnityEvent(YuUnityEventType.Update, InstanceByUpdate); // m_isWatchUpdate = true; //} } }
//实例化一个GameObject private void InstanceGameObj(UnityEngine.Object prefabResource, SceneItemInfo itemInfo) { //如果已经要求被销毁,则跳出 //if (!m_dicItemObject.ContainsKey(itemInfo.objNum)) //{ // return; //} UnityEngine.Object newObj = GameObject.Instantiate(prefabResource); if (newObj != null && newObj is GameObject) { GameObject gameObj = (GameObject)newObj; //if ((int)itemInfo.parentLayer > 1) //{ // foreach (var item in gameObj.GetComponentsInChildren<Transform>()) // { // switch (itemInfo.parentLayer) // { // case EMapLayer.AdornEffect: // item.gameObject.layer = YuUnityConstant.LAYER_PARTICLE; // break; // case EMapLayer.Building: // item.gameObject.layer = YuUnityConstant.LAYER_BUILDING; // break; // case EMapLayer.Terrain: // item.gameObject.layer = YuUnityConstant.LAYER_TERRAIN; // break; // case EMapLayer.Shadow: // item.gameObject.layer = YuUnityConstant.LAYER_SHADOW; // break; // case EMapLayer.Tree: // item.gameObject.layer = YuUnityConstant.LAYER_TREE; // break; // case EMapLayer.Animation: // item.gameObject.layer = YuUnityConstant.LAYER_DEFAULT; // break; // } // } //} gameObj.transform.position = itemInfo.pos.ToVector3(); gameObj.transform.eulerAngles = itemInfo.rot.ToVector3(); gameObj.transform.localScale = itemInfo.scal.ToVector3(); gameObj.name = itemInfo.objName; Renderer[] renders = gameObj.GetComponentsInChildren <Renderer>(); if (renders.Length == itemInfo.lightmapIndexes.Length) { for (int i = 0; i < renders.Length; i++) { if (itemInfo.lightmapIndexes[i] > -1) { renders[i].lightmapIndex = itemInfo.lightmapIndexes[i]; renders[i].lightmapScaleOffset = itemInfo.lightmapScaleOffsets[i].ToVector4(); } if (itemInfo.realtimeLightmapIndexes[i] > -1) { renders[i].realtimeLightmapIndex = itemInfo.realtimeLightmapIndexes[i]; renders[i].realtimeLightmapScaleOffset = itemInfo.realtimeLightmapScaleOffsets[i].ToVector4(); } } } else { Debug.LogWarning("Renderer数量与itemInfo的lightmap数据数量不一致,将导致lightmap无法正常工作"); } //m_dicItemObject[itemInfo.objNum] = gameObj; //if ((itemInfo.parentLayer == EMapLayer.Building || // itemInfo.parentLayer == EMapLayer.Tree) && // !m_dicCheckBlockItem.ContainsKey(itemInfo.objNum)) //{ // var blockItem = new CheckBlockItem(gameObj); // blockItem.obj = gameObj; // m_dicCheckBlockItem.Add(itemInfo.objNum, blockItem); //} string parentName = MyMapTool.GetNameByELater(itemInfo.parentLayer, m_curMapName); Transform parentTrans = m_dicLayer.ContainsKey(parentName) ? m_dicLayer[parentName].transform : null; gameObj.transform.parent = parentTrans; } else { Debug.LogWarning(prefabResource.name + "实例化异常"); } }