/// <summary> /// 更新透明度 /// </summary> /// <param name="agent"></param> private void UpdateAlpha(FlockAgent agent) { float z = agent.GetComponent <RectTransform>().anchoredPosition3D.z; // 判断Z在距离中的位置 float distance = Mathf.Abs(_manager.cutEffectConfig.StarEffectOriginPoint - _manager.cutEffectConfig.StarEffectEndPoint); float offset = Mathf.Abs(z - _manager.cutEffectConfig.StarEffectOriginPoint) / distance; // 当OFFSET 位于前 1/10 或后 1/10 时,更新透明度 if (offset < 0.05) { float k = Mathf.Abs(offset - 0.05f); float alpha = Mathf.Lerp(1, 0, k / 0.05f); agent.UpdateImageAlpha(alpha); } else if (offset > 0.95) { float k = Mathf.Abs(1 - offset); float alpha = Mathf.Lerp(0, 1, k / 0.05f); agent.UpdateImageAlpha(alpha); } else { agent.UpdateImageAlpha(1); } }
/// <summary> /// 执行动画效果 /// </summary> private void DoAnimation() { List <FlockAgent> agentsNeedClear = new List <FlockAgent>(); for (int i = 0; i < _activeAgents.Count; i++) { if (_activeAgents[i].GetComponent <RectTransform>().anchoredPosition3D.z < _manager.cutEffectConfig.StarEffectEndPoint) { // 清理agent, agentsNeedClear.Add(_activeAgents[i]); // 创建新 agent FlockAgent agent = CreateNewAgent(false); agent.GetComponent <RectTransform>().SetAsFirstSibling(); //Debug.Log("Create star card!"); } else { // 移动 Vector3 to = new Vector3(0, 0, -(Time.deltaTime * _manager.cutEffectConfig.StarEffectMoveFactor)); _activeAgents[i].GetComponent <RectTransform>().transform.Translate(to); // 更新透明度 UpdateAlpha(_activeAgents[i]); } } //Debug.Log(_activeAgents.Count); for (int i = 0; i < agentsNeedClear.Count; i++) { ClearAgent(agentsNeedClear[i]); //TODO有问题 _activeAgents.Remove(agentsNeedClear[i]); } }
private Vector2 GetCardGeneratePosition(FlockAgent flockAgent) { AgentContainerType _agentContainerType = flockAgent.agentContainerType; var rect = flockAgent.GetComponent <RectTransform>(); // 获取卡片生成位置 //Vector3 cardGenPosition = new Vector3(rect.anchoredPosition.x - _manager.PanelOffsetX - 1f, // rect.anchoredPosition.y - _manager.PanelOffsetY - 1f, // 200); Vector3 cardGenPosition = Vector3.zero; if (_agentContainerType == AgentContainerType.MainPanel) { // 获取屏幕坐标 //var sposition = Camera.main.WorldToScreenPoint(flockAgent.transform.position); var sposition = flockAgent.transform.position; Debug.Log("屏幕坐标: " + sposition); RectTransformUtility.ScreenPointToWorldPointInRectangle(_manager.operateCardManager.container.GetComponent <RectTransform>(), sposition, null, out cardGenPosition); Debug.Log("调整后: " + cardGenPosition); //cardGenPosition = new Vector3(rect.anchoredPosition.x - _manager.PanelOffsetX - 1f, rect.anchoredPosition.y - _manager.PanelOffsetY - 1f, 200); } else if (_agentContainerType == AgentContainerType.BackPanel) { var sposition = flockAgent.transform.position; Debug.Log("屏幕坐标: " + sposition); RectTransformUtility.ScreenPointToWorldPointInRectangle(_manager.operateCardManager.container.GetComponent <RectTransform>(), sposition, null, out cardGenPosition); Debug.Log("调整后: " + cardGenPosition); } else if (_agentContainerType == AgentContainerType.StarContainer) { var flockTransform = flockAgent.transform; // 获取屏幕坐标 Vector2 v = RectTransformUtility.WorldToScreenPoint(_manager.starCamera, flockTransform.position); // 需要屏幕坐标转为某UGUI容器内的坐标 Vector2 refp; RectTransformUtility.ScreenPointToLocalPointInRectangle(_manager.OperationPanel, v, null, out refp); refp = new Vector2(refp.x + _manager.OperationPanel.rect.width / 2, refp.y + _manager.OperationPanel.rect.height / 2); cardGenPosition = refp; } return(cardGenPosition); }
public void Starting() { float time = Time.time - _startTime; // 当前已运行的时间; for (int i = 0; i < _manager.agentManager.Agents.Count; i++) { FlockAgent agent = _manager.agentManager.Agents[i]; Vector2 agent_vector2 = agent.GenVector2; Vector2 ori_vector2 = agent.OriVector2; // 获取此 agent 需要的动画时间 float aniTime = time; // 如果总动画时间超出 agent 需要的动画时间,则不进行处理 if (aniTime > _entranceDisplayTime) { // 此时可能未走完动画 if (!agent.isCreateSuccess) { agent.SetChangedPosition(ori_vector2); agent.isCreateSuccess = true; } } else { float t = aniTime / _entranceDisplayTime; Func <float, float> ef = EasingFunction.Get(_manager.cutEffectConfig.UpDownDisplayEaseEnum); t = ef(t); Vector2 to = Vector2.Lerp(agent_vector2, ori_vector2, t); float a = Mathf.Lerp(0f, 1f, t); agent.GetComponent <Image>().color = new Color(1, 1, 1, a); agent.SetChangedPosition(to); } } if ((time - _entranceDisplayTime * 0.5f) > 0) { if (!_hasCallDisplay) { _hasCallDisplay = true; _onDisplayStart.Invoke(); } } if ((time - _entranceDisplayTime) > 0) { Reset(); _onEffectCompleted.Invoke(); } }
private FlockAgent CreateNewAgent(bool randomZ) { if (!randomZ) { //Debug.Log("添加星空块。"); } // 获取数据 //FlockData data = _daoService.GetFlockData(_dataType,_manager);\ FlockData data = _daoService.GetFlockDataByScene(_dataType, _manager.SceneIndex); // 获取出生位置 Vector2 randomPosition = UnityEngine.Random.insideUnitSphere; Vector3 position = new Vector3(); position.x = (randomPosition.x / 2 + 0.5f) * _manager.GetScreenRect().x; position.y = (randomPosition.y / 2 + 0.5f) * _manager.GetScreenRect().y; // 获取长宽 Sprite logoSprite = data.GetCoverSprite(); float width = _sceneUtil.ResetTexture(new Vector2(logoSprite.rect.width, logoSprite.rect.height)).x; float height = _sceneUtil.ResetTexture(new Vector2(logoSprite.rect.width, logoSprite.rect.height)).y; //FlockAgent go = _itemFactory.Generate(position.x, position.y, position.x, position.y, 0, 0, // width, height, data, AgentContainerType.StarContainer); FlockAgent go = FlockAgentFactoryInstance.Generate(_manager, position, AgentContainerType.StarContainer, position.x, position.y, 0, 0, width, height, data, _sceneConfig.daoTypeEnum); go.UpdateImageAlpha(0); // 星空效果不会被物理特效影响 //go.CanEffected = false; go.flockStatus = FlockStatusEnum.STAR; go.isStarEffect = true; // 设置Z轴 float z; if (randomZ) { z = Mathf.Lerp(_manager.cutEffectConfig.StarEffectOriginPoint, _manager.cutEffectConfig.StarEffectEndPoint, UnityEngine.Random.Range(0f, 1f)); } else { z = _manager.cutEffectConfig.StarEffectOriginPoint; } go.GetComponent <RectTransform>().anchoredPosition3D = go.GetComponent <RectTransform>().anchoredPosition3D + new Vector3(0, 0, z); go.Z = z; go.name = "Agent-" + Mathf.RoundToInt(go.Z); _activeAgents.Add(go); return(go); }
/// <summary> /// REF: https://www.yuque.com/u314548/fc6a5l/dozp0e /// </summary> /// <param name="flockAgent"></param> private void DoChooseForKinect(FlockAgent flockAgent) { int _data_id = flockAgent.DataId; var _dataType = flockAgent.dataTypeEnum; CardAgent _cardAgent; if (CanChoose(flockAgent)) { flockAgent.flockStatus = FlockStatusEnum.TOHIDE; var flockAgentPosition = flockAgent.GetComponent <RectTransform>().transform.position; RectTransform flockRect = flockAgent.GetComponent <RectTransform>(); // 获取kinect obj 的位置 var agents = _manager.kinectManager.kinectAgents; var distance = 3000f; KinectAgent targetKinectAgent = null; for (int i = 0; i < agents.Count; i++) { var kinectPosition = agents[i].GetComponent <RectTransform>().transform.position; var d = Vector2.Distance(kinectPosition, flockAgentPosition); // 添加了安全距离 if (d < distance && d < 1500f) { targetKinectAgent = agents[i]; distance = d; } } if (targetKinectAgent == null) { flockAgent.flockStatus = FlockStatusEnum.NORMAL; DoChooseForCommon(flockAgent); return; } /// 遮罩中存在贴附的卡片 if (targetKinectAgent.refFlockAgent != null) { var refFlockAgent = targetKinectAgent.refFlockAgent; targetKinectAgent.RecoverColliderEffect(); RemoveRefCard(targetKinectAgent.refFlockAgent); } targetKinectAgent.refFlockAgent = flockAgent; if (targetKinectAgent.status == KinectAgentStatusEnum.Small) { // 恢复大小并打开检测 targetKinectAgent.SetDisableEffect(false); targetKinectAgent.status = KinectAgentStatusEnum.Recovering; targetKinectAgent.GetComponent <RectTransform>().DOScale(1f, 0.5f).OnComplete( () => { targetKinectAgent.status = KinectAgentStatusEnum.Hide; } ); } if (targetKinectAgent.status == KinectAgentStatusEnum.Destoring) { targetKinectAgent.SetDisableEffect(false); targetKinectAgent.CancelClose(); } // 点击的卡片移动至遮罩位置,生成卡片并放大 targetKinectAgent.status = KinectAgentStatusEnum.WaitingHiding; targetKinectAgent.SetDisableEffect(false); var ani_time = 1.5f; var scaleAni = flockAgent.transform.DOScale(new Vector3(0.2f, 0.2f, 0.2f), ani_time - 0.2f); var moveAni = flockAgent.transform.DOMove(targetKinectAgent.transform.position, ani_time) .OnUpdate(() => {}) .OnComplete(() => { flockAgent.transform.SetParent(_manager.OperationPanel); targetKinectAgent.Hide(); flockAgent.flockStatus = FlockStatusEnum.HIDE; flockAgent.gameObject.SetActive(false); var _cardGenPos = flockAgent.GetComponent <RectTransform>().anchoredPosition; // 创建卡片 _cardAgent = _manager.operateCardManager.CreateNewOperateCard(_data_id, _dataType, _cardGenPos, flockAgent); _cardAgent.SetDisableEffect(true); _cardAgent.GoToFront(() => { _cardAgent.SetDisableEffect(false); //targetKinectAgent.SetDisableEffect(true); }); }); flockAgent.flockTweenerManager.Add(FlockTweenerManager.Kinnect_Choose_Scale, scaleAni); flockAgent.flockTweenerManager.Add(FlockTweenerManager.Kinnect_Choose_Move, moveAni); } }
private void DoChooseForCommon(FlockAgent flockAgent) { int _data_id = flockAgent.DataId; var _dataType = flockAgent.dataTypeEnum; CardAgent _cardAgent; if (CanChoose(flockAgent)) { flockAgent.flockStatus = FlockStatusEnum.TOHIDE; //_isChoosing = true; // 先缩小(向后退) RectTransform rect = flockAgent.GetComponent <RectTransform>(); Vector2 positionInMainPanel = rect.anchoredPosition; // 移到后方、缩小、透明 //rect.DOScale(0.1f, 0.3f); Vector3 to = new Vector3(0.2f, 0.2f, 0.7f); var _cardGenPos = GetCardGeneratePosition(flockAgent); // 完成缩小与移动后创建十字卡片 rect.DOScale(0.5f, 0.3f).OnComplete(() => { flockAgent.flockStatus = FlockStatusEnum.HIDE; flockAgent.gameObject.SetActive(false); //Debug.Log("chose :" + _data_id); _cardAgent = _manager.operateCardManager.CreateNewOperateCard(_data_id, _dataType, _cardGenPos, flockAgent); //靠近四周边界需要偏移 float w = _cardAgent.GetComponent <RectTransform>().rect.width; float h = _cardAgent.GetComponent <RectTransform>().rect.height; // 如果点击时,出生位置在最左侧 if (_cardGenPos.x < w / 2) { _cardGenPos.x = w / 2; } // 出身位置在最右侧 if (_cardGenPos.x > _manager.OperationPanel.rect.width - w / 2) { _cardGenPos.x = _manager.OperationPanel.rect.width - w / 2; } // 出生位置在最下侧 if (_cardGenPos.y < h / 2) { _cardGenPos.y = h / 2; } // 出生位置在最上侧 if (_cardGenPos.y > _manager.OperationPanel.rect.height - h / 2) { _cardGenPos.y = _manager.OperationPanel.rect.height - h / 2; } _cardAgent.GetComponent <RectTransform>().anchoredPosition = _cardGenPos; _cardAgent.GoToFront(() => { }); }); } }
public void Starting() { float time = Time.time - _startTime; // 当前已运行的时间; for (int i = 0; i < _manager.agentManager.Agents.Count; i++) { FlockAgent agent = _manager.agentManager.Agents[i]; Vector2 agent_vector2 = agent.GenVector2; Vector2 ori_vector2 = agent.OriVector2; // 获取总运行时间 float run_time = _startingTimeWithOutDelay + agent.Delay; if (time > run_time) { // 此时可能未走完动画 if (!agent.isCreateSuccess) { agent.SetChangedPosition(ori_vector2); agent.flockStatus = FlockStatusEnum.NORMAL; agent.isCreateSuccess = true; } continue; } float t = time / run_time; Func <float, float> moveEase = EasingFunction.Get(_manager.cutEffectConfig.MidDisperseMoveEaseEnum); t = moveEase(t); Vector2 to = Vector2.Lerp(agent_vector2, ori_vector2, t); agent.SetChangedPosition(to); // 透明度动画 var distance = Vector2.Distance(ori_vector2, to); var agentHeight = agent.GetComponent <RectTransform>().rect.height; var maxDistance = agentHeight * _manager.cutEffectConfig.MidDisperseAlphaMinDistanceFactor; if (distance < maxDistance) { float min_alpha = 0.1f; float max_alpha = 1f; float k = distance / maxDistance; Func <float, float> easeFunction = EasingFunction.Get(_manager.cutEffectConfig.MidDisperseAlphaEaseEnum); k = easeFunction(k); float newAlpha = Mathf.Lerp(max_alpha, min_alpha, k); agent.UpdateImageAlpha(newAlpha); } } if ((time - _entranceDisplayTime * 0.5f) > 0) { if (!_hasCallDisplay) { _hasCallDisplay = true; _onDisplayStart.Invoke(); } } if ((time - _entranceDisplayTime) > 0) { Reset(); _onEffectCompleted.Invoke(); } }