コード例 #1
0
    public void Restart(PlatformData data, Vector3 location)
    {
        gameObject.SetActive(true);
        BoxCollider2D collider = GetComponent <BoxCollider2D>();

        platformRenderer.sprite = data.sprite;
        platformRenderer.color  = Color.white;
        playerPresent           = false;
        speed                   = data.speed;
        isGood                  = data.isGood;
        timeLeft                = data.timeLeft;
        InitialTimeLeft         = timeLeft;
        shouldFade              = data.shouldFade;
        collider.size           = data.Size;
        isLeaf                  = platformRenderer.sprite.name.Contains("Leaf");
        transform.localRotation = Quaternion.Euler(data.Rotation);
        transform.localScale    = data.Scale;
        transform.position      = location;

        // The property info for the ParticleSystem class type and the saved instance
        PropertyInfo[] particleProps = particles.GetType().GetProperties();
        ParticleSystem savedSystem   = data.ParticlePrefab.GetComponent <ParticleSystem>();

        // For each property in this class,
        foreach (var property in particleProps)
        {
            // Grab each particle module (except subemitters which aren't used)
            if (property.PropertyType.Name.Contains("Module") && !property.PropertyType.Name.Contains("SubEmitters"))
            {
                // Grab the correspoinding module object on this GameObject, grab its properties,
                object         module     = property.GetValue(particles);
                PropertyInfo[] properties = module.GetType().GetProperties();
                object         newModule  = savedSystem.GetType().GetProperty(property.Name).GetValue(savedSystem);

                // Then loop through these properties and set them to the value from the prefab object.
                foreach (var prop in properties)
                {
                    if (prop.GetIndexParameters().Length == 0 && prop.CanWrite)
                    {
                        prop.SetValue(module, prop.GetValue(newModule));
                    }
                }
            }
        }

        // Repeat the process for the ParticleSystemRenderer.
        ParticleSystemRenderer renderer      = GetComponentInChildren <ParticleSystemRenderer>(),
                               savedRenderer = data.ParticlePrefab.GetComponent <ParticleSystemRenderer>();

        particleProps = renderer.GetType().GetProperties();

        foreach (var property in particleProps)
        {
            if (property.GetIndexParameters().Length == 0 && property.CanRead && property.CanWrite && !property.Name.Contains("materials", true))
            {
                if (property.Name.Contains("material", true) && !property.Name.Equals("sharedMaterial"))
                {
                    continue;
                }
                property.SetValue(renderer, property.GetValue(savedRenderer));
            }
        }

        transform.GetChild(1).localPosition = data.ParticlePrefab.transform.localPosition;
    }