summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Effects/SparksManager.cs
blob: 6825f6155ed60046b90f2ba1cbd4d4c3ddf68778 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class SparksManager : MonoBehaviour
{
    public static SparksManager Instance;

    public Spark[] SparkTemplates;

    public Transform Root_Pool;

    List<Spark> m_Pool = new List<Spark>();

    private void Awake()
    {
        Instance = this;
    }

    Spark GetSparkTemplate(string name)
    {
        foreach (var effect in SparkTemplates)
        {
            if (effect != null && effect.Name == name)
            {
                return effect;
            }
        }
        return null;
    }

    Spark RecycleSpark(string name)
    {
        foreach (var effect in m_Pool)
        {
            if (effect != null && effect.Name == name)
            {
                return effect;
            }
        }
        return null;
    }

    public void PlaySpark(string name, Vector3 position)
    {
        Spark effect = RecycleSpark(name);
        if (effect == null)
        {
            Spark temp = GetSparkTemplate(name);
            effect = UnityEngine.Object.Instantiate(temp);
        }
        else
        {
            m_Pool.Remove(effect);
        }

        effect.Host = null;
        effect.transform.position = position;
        effect.transform.SetParent(this.transform);
        effect.gameObject.SetActive(true);
    }


    public void PlaySpark(string name, Transform host)
    {
        Spark effect = RecycleSpark(name);
        if (effect == null)
        {
            Spark temp = GetSparkTemplate(name);
            effect = UnityEngine.Object.Instantiate(temp);
        }
        else
        {
            m_Pool.Remove(effect);
        }
        effect.Host = host;
        effect.transform.position = host.position;
        effect.transform.SetParent(this.transform);
        effect.gameObject.SetActive(true);
    }

    // 回收特效
    public void CycleSpark(Spark effect)
    {
        effect.gameObject.SetActive(false);
        effect.transform.SetParent(Root_Pool);
        m_Pool.Add(effect);
    }

}