/// <summary> /// 明牌 /// </summary> /// <param name="pos"></param> /// <param name="pokers"></param> public void ShowPoker(int playerId, List <Poker> pokers) { SeatEntity seat = GetSeatByPlayerId(playerId); AppDebug.LogWarning(pokers.Count); if (seat == null) { return; } seat.pokerList.Clear(); seat.pokerList.AddRange(pokers); seat.pokerList.Sort(); //if (CurrentRoom.Times == 0) //{ // CurrentRoom.Times = 1; //} //CurrentRoom.Times *= 2; TransferData data = new TransferData(); data.SetValue("SeatEntity", seat); SendNotification(ON_SHOW_POKER, data); }
public GameObject Load(ResourceType type, string name, bool isCache = true, bool isInstantiate = true) { GameObject go = null; string path = ""; switch (type) { case ResourceType.UIScene: path += "UIScenes/"; break; case ResourceType.UIWindow: path += "UIWindows/"; break; case ResourceType.Prefab: path += "Prefabs/"; break; case ResourceType.UIItem: path += "UIItems/"; break; case ResourceType.UIAnimation: path += "UIAnimations/"; break; } path += name; if (m_pDictionary.ContainsKey(path)) { go = m_pDictionary[path]; } else { go = Resources.Load <GameObject>(path); if (go == null) { AppDebug.LogWarning("资源" + path + "为空"); return(null); } if (isCache) { m_pDictionary.Add(path, go); } } if (isInstantiate) { return(Object.Instantiate(go)); } else { return(go); } }
/// <summary> /// 从网络下载 /// </summary> /// <param name="avatarUrl"></param> /// <param name="isCache"></param> /// <returns></returns> private IEnumerator LoadHeadCoroutine(string avatarUrl, bool isCache) { LogSystem.Log("下载图片:" + avatarUrl); WWW www = new WWW(avatarUrl + "?t=" + TimeUtil.GetTimestampMS().ToString()); float timeOut = Time.time; float progress = www.progress; while (www != null && !www.isDone) { if (progress < www.progress) { timeOut = Time.time; progress = www.progress; } if (Time.time - timeOut > DownloadManager.DOWNLOAD_TIME_OUT) { www.Dispose(); AppDebug.LogWarning("下载超时"); LoadHead(avatarUrl, isCache); yield break; } yield return(null); } yield return(www); if (www.error == null) { int hash = avatarUrl.GetHashCode(); if (isCache) { if (!IOUtil.DirectoryExists(CACHE_PATH)) { IOUtil.CreateDirectory(CACHE_PATH); } IOUtil.Write(CACHE_PATH + hash.ToString(), www.bytes); } if (!m_TextureDic.ContainsKey(hash)) { m_TextureDic.Add(hash, www.texture); } if (m_LoadingDic.ContainsKey(hash) && m_LoadingDic[hash] != null) { m_LoadingDic[hash](m_TextureDic[hash]); m_LoadingDic.Remove(hash); } www.Dispose(); } yield return(null); }
public static void SetParent(this GameObject obj, Transform parent, bool isUI = false) { if (parent == null) { AppDebug.LogWarning("设置的父节点为空!请检查引用对象"); return; } obj.transform.SetParent(parent, !isUI); obj.transform.localPosition = Vector3.zero; obj.transform.localScale = Vector3.one; obj.transform.localEulerAngles = Vector3.zero; //obj.SetLayer(parent.gameObject.layer); }
/// <summary> /// 添加群成员 /// </summary> /// <param name="groupId">群Id</param> /// <param name="entity">玩家数据实体</param> public void AddMember(int groupId, PlayerEntity entity, bool isPlayer) { if (entity == null) { return; } ChatGroupEntity group = GetGroup(groupId); if (group == null) { return; } if (GetMember(groupId, entity.id) != null) { AppDebug.LogWarning("这个玩家存在了"); return; } AppDebug.Log("玩家" + entity.nickname + "加入群" + group.name); if (group.ownerId == entity.id) { entity.isOwner = true; } if (!group.isRequested) { ++group.currMemberCount; } else { group.members.Add(entity); group.currMemberCount = group.members.Count; } group.members.Sort(); if (isPlayer) { group.playerEntity = entity; } TransferData data = new TransferData(); data.SetValue("GroupEntity", group); data.SetValue("PlayerEntity", entity); data.SetValue("IsPlayer", isPlayer); data.SetValue("Index", group.members.IndexOf(entity)); data.SetValue("OneselfEntity", group.playerEntity); SendNotification(ON_ADD_MEMBER, data); SendGroupInfoChangedNotification(group); }
/// <summary> /// 添加群 /// </summary> /// <param name="entity">群数据实体</param> public void AddGroup(ChatGroupEntity entity) { if (GetGroup(entity.id) != null)//有这个群的信息了就不加了 { AppDebug.LogWarning("这个群存在了"); return; } AllGroup.Add(entity); TransferData data = new TransferData(); data.SetValue("ChatGroup", entity); SendNotification(ON_ADD_GROUP, data); }
/// <summary> /// 添加房间 /// </summary> /// <param name="groupId"></param> /// <param name="entity"></param> public void AddRoom(int groupId, RoomEntityBase entity, List <int> lstPlayerId) { ChatGroupEntity group = GetGroup(groupId); if (group == null) { return; } if (entity == null) { return; } if (GetRoom(groupId, entity.gameId) != null) { AppDebug.LogWarning("房间存在了"); return; } AppDebug.Log("有一个新的房间"); if (lstPlayerId != null) { for (int i = 0; i < lstPlayerId.Count; ++i) { bool isExists = false; for (int j = 0; j < group.members.Count; ++j) { if (lstPlayerId[i] == group.members[j].id) { entity.players.Add(group.members[j]); isExists = true; break; } } if (!isExists) { entity.players.Add(null); } } } group.rooms.Add(entity); group.roomCount = group.rooms.Count; TransferData data = new TransferData(); data.SetValue("GroupEntity", group); data.SetValue("RoomEntity", entity); SendNotification(ON_ADD_ROOM, data); SendGroupInfoChangedNotification(group); }
/// <summary> /// 播放UI动画 /// </summary> /// <param name="animationName"></param> public void PlayUIAnimation(string animationName) { animationName = animationName.ToLower(); string path = string.Format("download/{0}/prefab/uiprefab/uianimations/{1}.drb", ConstDefine.GAME_NAME, animationName); GameObject go = AssetBundleManager.Instance.LoadAssetBundle <GameObject>(path, animationName); if (go != null) { go = Instantiate(go); go.SetParent(m_UIAnimationContainer); } else { AppDebug.LogWarning("UI动画" + animationName + "不存在"); } }
/// <summary> /// 播放座位UI动画 /// </summary> /// <param name="type"></param> public void PlayUIAnimation(UISeatAnimationType type) { string path = string.Format("download/{0}/prefab/uiprefab/uianimations/{1}.drb", ConstDefine.GAME_NAME, type.ToString().ToLower()); AssetBundleManager.Instance.LoadOrDownload(path, type.ToString().ToLower(), (GameObject go) => { if (go != null) { go = Instantiate(go); go.SetParent(m_UIAnimationContainer); } else { AppDebug.LogWarning("UI动画" + type + "不存在"); } }); }
/// <summary> /// 询问下注 /// </summary> public void AskBet(int playerId) { AppDebug.LogWarning("询问下注"); SeatEntity seat = GetSeatByPlayerId(playerId); if (seat == null) { return; } CurrentRoom.roomStatus = RoomEntity.RoomStatus.Bet; seat.status = SeatEntity.SeatStatus.Bet; TransferData data = new TransferData(); data.SetValue("SeatEntity", seat); data.SetValue("BaseScore", CurrentRoom.baseScore); SendNotification(ON_ASK_BET, data); }
/// <summary> /// 移除群成员 /// </summary> /// <param name="groupId">群Id</param> /// <param name="playerId">玩家Id</param> public void RemoveMember(int groupId, int playerId) { ChatGroupEntity group = GetGroup(groupId); if (group == null) { return; } if (!group.isRequested) { --group.currMemberCount; } else { bool isExists = false; for (int i = 0; i < group.members.Count; ++i) { if (group.members[i].id == playerId) { isExists = true; group.members.RemoveAt(i); group.currMemberCount = group.members.Count; break; } } if (!isExists) { AppDebug.LogWarning("要移除的玩家不存在"); return; } TransferData data = new TransferData(); data.SetValue("GroupId", groupId); data.SetValue("PlayerCount", group.currMemberCount); data.SetValue("PlayerId", playerId); SendNotification(ON_REMOVE_MEMBER, data); } SendGroupInfoChangedNotification(group); }
/// <summary> /// 同步加载图片精灵 /// </summary> /// <param name="path"></param> /// <param name="spriteName"></param> /// <returns></returns> public Sprite LoadSprite(string path, string spriteName) { #if UNITY_EDITOR && DISABLE_ASSETBUNDLE path = path.Insert(path.LastIndexOf('.'), "/" + spriteName); Texture2D tex = LoadAssetBundle <Texture2D>(path, spriteName); Rect iconRect; try { Debug.Log("图片宽度:" + tex.width + "----图片高度:" + tex.height); iconRect = new Rect(0, 0, tex.width, tex.height); } catch { AppDebug.LogWarning(path + "加载图片失败"); AppDebug.LogWarning(spriteName + "加载图片失败"); return(null); } Sprite iconSprite = Sprite.Create(tex, iconRect, new Vector2(0.5f, 0.5f)); return(iconSprite); #else Sprite sprite = LoadAssetBundle <Sprite>(path, spriteName); return(sprite); #endif }
/// <summary> /// 请求服务器 /// </summary> /// <param name="www"></param> /// <returns></returns> private IEnumerator Request(WWW www, HttpSendPackage package) { LogSystem.Log(www.url); CallBackArgs args = new CallBackArgs(); float timeOut = Time.time; float progress = www.progress; while (www != null && !www.isDone) { if (progress < www.progress) { timeOut = Time.time; progress = www.progress; } if (Time.time - timeOut > TIME_OUT) { www.Dispose(); AppDebug.LogWarning("HTTP超时"); ++package.timeOutCount; if (package.timeOutCount >= MAX_TIME_OUT_COUNT) { if (package.callBack != null) { args.HasError = true; args.ErrorMsg = "请求超时"; package.callBack(args); } } else { if (package.isPost) { PostUrl(package); } else { GetUrl(package); } } yield break; } yield return(null); } yield return(www); if (www.error == null) { AppDebug.Log(www.text); if (www.text.Equals("null", StringComparison.OrdinalIgnoreCase)) { if (package.callBack != null) { args.HasError = true; args.ErrorMsg = "未请求到数据"; package.callBack(args); } } else { if (package.callBack != null) { try { args.HasError = false; LitJson.JsonData jsonData = LitJson.JsonMapper.ToObject(www.text); args.Value = new Ret() { code = jsonData["code"].ToString().ToInt(), data = jsonData["data"], msg = jsonData["msg"].ToString() }; } catch { AppDebug.Log(www.text); args.HasError = true; args.ErrorMsg = "数据异常"; } finally { package.callBack(args); if (args.Value != null && (args.Value.code == -91017 || args.Value.code == -91018)) { if (OnTokenError != null) { OnTokenError(args); } } } } } } else { if (package.callBack != null) { args.HasError = true; args.ErrorMsg = "网络异常"; package.callBack(args); } AppDebug.Log("连接失败" + www.error); } www.Dispose(); }
private IEnumerator DownloadData() { if (NeedDownloadCount == 0) { Debug.Log("需要下载的数量是0"); yield break; } m_CurrentDownloadData = m_List[0]; string fullName = m_CurrentDownloadData.FullName.Replace('\\', '/'); string dataUrl = DownloadManager.Instance.DownloadUrl + fullName; int lastIndex = fullName.LastIndexOf('/'); string path = ""; if (lastIndex > 0) { path = fullName.Substring(0, lastIndex); } string localFilePath = DownloadManager.Instance.LocalFilePath + path; if (!IOUtil.DirectoryExists(localFilePath)) { IOUtil.CreateDirectory(localFilePath); } WWW www = new WWW(dataUrl + "?t=" + TimeUtil.GetTimestampMS().ToString()); float timeOut = Time.time; float progress = www.progress; while (www != null && !www.isDone) { if (progress < www.progress) { timeOut = Time.time; progress = www.progress; m_CurrentDownloadSize = (int)(m_CurrentDownloadData.Size * progress); } if (Time.time - timeOut > DownloadManager.DOWNLOAD_TIME_OUT) { www.Dispose(); AppDebug.LogWarning("下载超时"); StartCoroutine(DownloadData()); yield break; } yield return(null); } yield return(www); if (www != null && www.error == null) { using (FileStream fs = new FileStream(DownloadManager.Instance.LocalFilePath + fullName, FileMode.Create, FileAccess.Write)) { fs.Write(www.bytes, 0, www.bytes.Length); } www.Dispose(); } else { AppDebug.LogWarning(www.url + "下载失败" + www.error); yield break; } m_CurrentDownloadSize = 0; m_DownloadSize += m_CurrentDownloadData.Size; DownloadManager.Instance.ModifyLocalData(m_CurrentDownloadData); m_List.RemoveAt(0); ++CompleteCount; if (m_List.Count == 0) { m_List.Clear(); } else { StartDownload(); } }
public IEnumerator DownloadData(DownloadDataEntity entity, Action <bool> onComplete) { string dataUrl = DownloadManager.Instance.DownloadUrl + entity.FullName; Debug.Log("下载资源:" + dataUrl); int lastIndex = entity.FullName.LastIndexOf('/'); string path = ""; if (lastIndex > 0) { path = entity.FullName.Substring(0, lastIndex); } string localFilePath = DownloadManager.Instance.LocalFilePath + path; if (!IOUtil.DirectoryExists(localFilePath)) { IOUtil.CreateDirectory(localFilePath); } WWW www = new WWW(dataUrl + "?t=" + TimeUtil.GetTimestampMS().ToString()); float timeOut = Time.time; float progress = www.progress; while (www != null && !www.isDone) { if (progress < www.progress) { timeOut = Time.time; progress = www.progress; } if (Time.time - timeOut > DownloadManager.DOWNLOAD_TIME_OUT) { www.Dispose(); AppDebug.LogWarning("下载超时"); if (onComplete != null) { onComplete(false); } yield break; } yield return(null); } yield return(www); if (www != null && www.error == null) { using (FileStream fs = new FileStream(DownloadManager.Instance.LocalFilePath + entity.FullName, FileMode.Create, FileAccess.Write)) { fs.Write(www.bytes, 0, www.bytes.Length); } www.Dispose(); } else { AppDebug.LogWarning("下载失败" + www.url + " " + www.error); if (onComplete != null) { onComplete(false); } www.Dispose(); yield break; } DownloadManager.Instance.ModifyLocalData(entity); if (onComplete != null) { onComplete(true); } }
/// <summary> /// 接收数据回调 /// </summary> /// <param name="ar"></param> private void ReceiveCallBack(IAsyncResult ar) { if (Socket == null || !Socket.Connected) { //Debug.Log("接收数据回调返回"); return; } try { int nLength = Socket.EndReceive(ar); //Debug.Log("从套接字缓冲区读取了" + nLength.ToString() + "长度的数据"); if (nLength > 0) { m_ReceiveMemoryStream.Position = m_ReceiveMemoryStream.Length; m_ReceiveMemoryStream.Write(m_ReceiveBuffer, 0, nLength); if (m_ReceiveMemoryStream.Length > DATA_HEAD_LENGTH) { while (true) { m_ReceiveMemoryStream.Position = 0; m_ReceiveMemoryStream.Read(m_HeadData, 0, DATA_HEAD_LENGTH); int currentMsgLen = ((m_HeadData[0] & 0xff) << 24) + ((m_HeadData[1] & 0xff) << 16) + ((m_HeadData[2] & 0xff) << 8) + (m_HeadData[3] & 0xff); int currentFullMsgLen = DATA_HEAD_LENGTH + currentMsgLen; if (m_ReceiveMemoryStream.Length >= currentFullMsgLen) // 说明至少接收到了一个整包 { AppDebug.Log("客户端收到了一个" + currentFullMsgLen + "长度的消息"); byte[] buffer = new byte[currentMsgLen]; m_ReceiveMemoryStream.Position = DATA_HEAD_LENGTH; m_ReceiveMemoryStream.Read(buffer, 0, currentMsgLen); buffer = EncryptUtil.NetEncrypt(buffer, SystemProxy.Instance.NetKey, SystemProxy.Instance.NetCorrected); m_ReceiveQueue.Enqueue(buffer); //===============处理剩余字节数组====================== int remainLen = (int)m_ReceiveMemoryStream.Length - currentFullMsgLen; if (remainLen > 0) { m_ReceiveMemoryStream.Position = currentFullMsgLen; byte[] remainBuffer = new byte[remainLen]; m_ReceiveMemoryStream.Read(remainBuffer, 0, remainLen); m_ReceiveMemoryStream.Position = 0; m_ReceiveMemoryStream.SetLength(0); m_ReceiveMemoryStream.Write(remainBuffer, 0, remainBuffer.Length); remainBuffer = null; } else // 没有剩余字节 { m_ReceiveMemoryStream.Position = 0; m_ReceiveMemoryStream.SetLength(0); break; } } else { break; } } } ReceiveMessage(); } else { AppDebug.LogWarning(string.Format("{0}断开连接", m_isClientClose ? "客户端断的" : "服务器断的")); Close(false); } } catch (Exception ex) { AppDebug.LogWarning(string.Format("{0}断开连接,{1}", m_isClientClose ? "客户端断的" : "服务器断的", ex)); Close(false); } }