| | |
| | | public class EndlessDropManager : Singleton<EndlessDropManager> |
| | | { |
| | | /// <summary> |
| | | /// 掉落道具预制体列表 |
| | | /// </summary> |
| | | public List<GameObject> PropList; |
| | | |
| | | /// <summary> |
| | | /// 获得掉落事件 |
| | | /// </summary> |
| | | public event Action<EndlessDrop> ObtainDropEvent; |
| | |
| | | /// </summary> |
| | | private Dictionary<int, GameObject> DropObjDic; |
| | | |
| | | private string dropPath = "UI/Props/EndlessDropEffect"; |
| | | |
| | | /// <summary> |
| | | /// 自动拾取时间,先统一处理 |
| | | /// </summary> |
| | | public float AutoPickupTime = 5f; |
| | | |
| | | /// <summary> |
| | | /// 掉落半径 |
| | | /// </summary> |
| | | public float DropRadius { get; set; } = 50f; |
| | | public float DropRadius { get; set; } = 5f; |
| | | |
| | | public Canvas canvas; |
| | | |
| | | // Start is called before the first frame update |
| | | private void Start() |
| | | { |
| | | AllDropList = new List<EndlessDrop>(); |
| | | DropObjDic = new Dictionary<int, GameObject>(); |
| | | allIconLis = new List<FlyImage>(); |
| | | } |
| | | |
| | | // Update is called once per frame |
| | |
| | | |
| | | if (AllDropList[i].ElapsedTime >= AllDropList[i].AutoPickupTime) |
| | | { |
| | | AllDropList[i].IsPickupCompleted = true; |
| | | SafelyCallObtainDrop(AllDropList[i]); |
| | | RemoveDrop(AllDropList[i].Id); |
| | | EndlessDropReward dropReward = DropObjDic[AllDropList[i].Id].GetComponent<EndlessDropReward>(); |
| | | // dropReward?.OnClick(); |
| | | |
| | | //AllDropList[i].IsPickupCompleted = true; |
| | | //SafelyCallObtainDrop(AllDropList[i]); |
| | | //RemoveDrop(AllDropList[i].Id); |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据reward获得预制体对象 |
| | | /// </summary> |
| | | /// <param name="data"></param> |
| | | private GameObject GetPrefabByReward(reward data) |
| | | { |
| | | switch (data.type) |
| | | { |
| | | case CURRENCY.Money: |
| | | return PropList[0]; |
| | | case CURRENCY.Gold: |
| | | return PropList[1]; |
| | | case CURRENCY.Box: |
| | | return PropList[1 + data.id]; |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获得一些掉落 |
| | | /// </summary> |
| | | /// <param name="list"></param> |
| | | /// <param name="pos">小怪的世界坐标</param> |
| | | public void AddDrop(List<reward> list, Vector3 pos) |
| | | { |
| | | for (int i = 0; i < list.Count; ++i) |
| | | { |
| | | EndlessDrop drop = new EndlessDrop(); |
| | | drop.Reward = list[i]; |
| | | drop.Id = GetDropId(); |
| | | |
| | | GameObject prefabObj = GetPrefabByReward(list[i]); |
| | | |
| | | if (prefabObj == null) |
| | | { |
| | | Debug.LogError($"--------------------- type: {list[i].type} id:{list[i].id} 没有对应的预制体 ---------------------"); |
| | | continue; |
| | | } |
| | | |
| | | EndlessDropReward dropReward = prefabObj.GetComponent<EndlessDropReward>(); |
| | | drop.AutoPickupTime = dropReward.AutoPickupTime; |
| | | CreateDrop(drop, pos, list.Count > 0); |
| | | AllDropList.Add(drop); |
| | | } |
| | | // for (int i = 0; i < list.Count; ++i) |
| | | // { |
| | | // EndlessDrop drop = new EndlessDrop(); |
| | | // drop.Reward = list[i]; |
| | | // drop.Id = GetDropId(); |
| | | // drop.AutoPickupTime = AutoPickupTime; |
| | | // CreateDrop(drop, pos, list.Count > 0); |
| | | // AllDropList.Add(drop); |
| | | // } |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// 在场景中创建掉落的icon |
| | | /// </summary> |
| | | /// <param name="drop"></param> |
| | | /// <param name="pos"></param> |
| | | /// <param name="worldPos">世界坐标</param> |
| | | /// <param name="isRandom">是否需要随机位置</param> |
| | | private void CreateDrop(EndlessDrop drop, Vector3 pos, bool isRandom = false) |
| | | private void CreateDrop(EndlessDrop drop, Vector3 worldPos, bool isRandom = false) |
| | | { |
| | | GameObject dropLayer = GameObject.Find("DropLayer"); |
| | | GameObject prefabObj = GetPrefabByReward(drop.Reward); |
| | | GameObject prefabObj = Resources.Load<GameObject>(dropPath); |
| | | GameObject obj = Instantiate(prefabObj); |
| | | GameObject mainUI = GameObject.Find("MainUI"); |
| | | EndlessDropReward dropReward = obj.GetComponent<EndlessDropReward>(); |
| | | |
| | | Transform mainUITransform = mainUI.GetComponent<Transform>(); |
| | | dropReward.DropData = drop; |
| | | dropReward.SetIcon(); |
| | | |
| | | GameObject mainUI = GameObject.Find("BottomUI"); |
| | | |
| | | Transform mainUITransform = mainUI.transform; |
| | | obj.transform.SetParent(mainUITransform, false); |
| | | |
| | | // Vector3 screenPos = Camera.main.WorldToScreenPoint(worldPos); |
| | | // Vector2 pos; |
| | | // RectTransformUtility.ScreenPointToLocalPointInRectangle(mainUITransform as RectTransform, screenPos, Camera.main, out pos); |
| | | // (obj.transform as RectTransform).anchoredPosition = pos; |
| | | |
| | | obj.GetComponent<Transform>().SetParent(mainUITransform, true); |
| | | Vector3 screenPos = Camera.main.WorldToScreenPoint(pos); |
| | | screenPos.z = 0; |
| | | obj.transform.position = screenPos; |
| | | |
| | | if (isRandom) |
| | | obj.transform.position = worldPos; |
| | | |
| | | Vector3 offect = obj.GetComponent<RectTransform>().anchoredPosition3D; |
| | | offect.z = 0; |
| | | obj.GetComponent<RectTransform>().anchoredPosition3D = offect; |
| | | // obj.transform.localRotation = Quaternion.identity; |
| | | // obj.transform.localScale = Vector3.one; |
| | | |
| | | // if (isRandom) |
| | | // { |
| | | // Vector2 p = UnityEngine.Random.insideUnitCircle * DropRadius; |
| | | // Vector3 pos1 = p.normalized * p.magnitude; |
| | | // Vector3 objPos = obj.transform.position; |
| | | // objPos.x += pos1.x; |
| | | // objPos.z += pos1.y; |
| | | // obj.transform.position = objPos; |
| | | // } |
| | | |
| | | dropReward.PlayParticle(); |
| | | DropObjDic.Add(drop.Id, obj); |
| | | } |
| | | |
| | | |
| | | List<FlyImage> allIconLis; |
| | | public GameObject drapIcon; |
| | | |
| | | public FlyImage CreateIcon(Transform ts) |
| | | { |
| | | for (int i = 0; i < allIconLis.Count; i++) |
| | | { |
| | | Vector2 p = UnityEngine.Random.insideUnitCircle * DropRadius; |
| | | Vector3 pos1 = p.normalized * p.magnitude; |
| | | Vector3 objPos = obj.transform.position; |
| | | objPos.x += pos1.x; |
| | | objPos.y += pos1.y; |
| | | obj.transform.position = objPos; |
| | | if (!allIconLis[i].gameObject.activeSelf) |
| | | { |
| | | allIconLis[i].gameObject.SetActive(true); |
| | | return allIconLis[i]; |
| | | } |
| | | } |
| | | |
| | | obj.GetComponent<EndlessDropReward>().DropData = drop; |
| | | DropObjDic.Add(drop.Id, obj); |
| | | GameObject obj = Instantiate(drapIcon, ts); |
| | | FlyImage fly = obj.GetComponent<FlyImage>(); |
| | | allIconLis.Add(fly); |
| | | return fly; |
| | | |
| | | } |
| | | |
| | | public void FlyIcon(Transform ts, Sprite sp, Vector3 startP, Vector3 endP, bool useBezier) |
| | | { |
| | | StartCoroutine(CreateIcon(ts, sp, startP, endP, useBezier)); |
| | | } |
| | | |
| | | IEnumerator CreateIcon(Transform ts, Sprite sp, Vector3 startP, Vector3 endP, bool useBezier) |
| | | { |
| | | FlyImage fly; |
| | | |
| | | // for (int i = 0; i < 5; i++) |
| | | // { |
| | | // fly = CreateIcon(ts); |
| | | // fly.SetDestination(sp, startP, endP, useBezier); |
| | | // } |
| | | // yield return new WaitForSeconds(0.2f); |
| | | |
| | | for (int i = 0; i < 15; i++) |
| | | { |
| | | fly = CreateIcon(ts); |
| | | fly.SetDestination(sp, startP, endP, useBezier); |
| | | yield return new WaitForSeconds(0.01f); |
| | | |
| | | } |
| | | |
| | | yield break; |
| | | } |
| | | |
| | | /// <summary> |