| | |
| | | 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 Canvas canvas; |
| | | |
| | | // Start is called before the first frame update |
| | | private void Start() |
| | |
| | | } |
| | | |
| | | /// <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> |
| | |
| | | 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; |
| | | drop.AutoPickupTime = AutoPickupTime; |
| | | CreateDrop(drop, pos, list.Count > 0); |
| | | AllDropList.Add(drop); |
| | | } |
| | |
| | | /// 在场景中创建掉落的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); |
| | | EndlessDropReward dropReward = obj.GetComponent<EndlessDropReward>(); |
| | | |
| | | dropReward.DropData = drop; |
| | | dropReward.SetIcon(); |
| | | |
| | | GameObject mainUI = GameObject.Find("MainUI"); |
| | | |
| | | Transform mainUITransform = mainUI.GetComponent<Transform>(); |
| | | obj.GetComponent<Transform>().SetParent(mainUITransform, true); |
| | | Vector3 screenPos = Camera.main.WorldToScreenPoint(pos); |
| | | screenPos.z = 0; |
| | | obj.transform.position = screenPos; |
| | | |
| | | if (isRandom) |
| | | { |
| | | 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; |
| | | } |
| | | // Vector3 screenPos = Camera.main.WorldToScreenPoint(worldPos); |
| | | // Vector2 targetPos; |
| | | // RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, screenPos, canvas.worldCamera, out targetPos); |
| | | // RectTransform rectTransform = obj.GetComponent<RectTransform>(); |
| | | // rectTransform.anchoredPosition = pos; |
| | | // obj.transform.localRotation = Quaternion.identity; |
| | | |
| | | obj.GetComponent<EndlessDropReward>().DropData = drop; |
| | | // Vector3 screenPos = Camera.main.WorldToScreenPoint(worldPos); |
| | | // screenPos.z = 0; |
| | | |
| | | obj.transform.position = worldPos; |
| | | 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.y += pos1.y; |
| | | // obj.transform.position = objPos; |
| | | // } |
| | | |
| | | dropReward.PlayParticle(); |
| | | DropObjDic.Add(drop.Id, obj); |
| | | } |
| | | |
| | |
| | | |
| | | return true; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 拾取所有还未拾取的掉落 |
| | | /// </summary> |
| | | public void PickUpAllDrop() |
| | | { |
| | | for (int i = 0; i < AllDropList.Count; ++i) |
| | | { |
| | | if (AllDropList[i].IsPickupCompleted) continue; |
| | | |
| | | AllDropList[i].IsPickupCompleted = true; |
| | | SafelyCallObtainDrop(AllDropList[i]); |
| | | RemoveDrop(AllDropList[i].Id); |
| | | } |
| | | } |
| | | } |
| | | } |