2017-09-21  本文已影响25人  萧非子

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MainController : MonoBehaviour {

private List<CardController> cardList;
private List<Toggle> toggleGrp;

private bool isInit = true;
private bool updateFlag = false;

// Use this for initialization
void Start () {
    cardList = new List<CardController>();
    for(int i = 1; i < 12; i++) {
        CardController con = transform.Find("CardCollection/Card_" + i).gameObject.GetComponent<CardController>();
        cardList.Add(con);
    }
    Transform selectPanel = transform.Find("SelectPanel");
    toggleGrp = new List<Toggle>();
    for(int i = 0; i < selectPanel.childCount; i++) {
        Toggle t = selectPanel.GetChild(i).gameObject.GetComponent<Toggle>();
        t.onValueChanged.AddListener(onSelectChange);
        toggleGrp.Add(t);
    }
}

// Update is called once per frame
void Update () {
    if(isInit) {
        cardList[0].onSelect();
        for(int i = 1; i < cardList.Count; i++) {
            cardList[i].shuffleCard();
        }
        isInit = false;
    }
    if(updateFlag) {
        for(int i = 0; i < cardList.Count; i++) {
            if(toggleGrp[i].isOn) {
                cardList[i].onSelect();
            } else {
                cardList[i].shuffleCard();
            }
        }
        updateFlag = false;
    }
}

void onSelectChange(bool isOn) {
    if(isOn) {
        updateFlag = true;
    }
}

}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CardController : MonoBehaviour {

public const int AXIS_X_MIN = -450;
public const int AXIS_X_MAX = 450;
public const int AXIS_Y_MIN = -330;
public const int AXIS_Y_MAX = 330;

public const float ANGLE_MIN = 0;
public const float ANGLE_MAX = 360;

public const float SCALAR_MIN = 0.6f;
public const float SCALAR_MAX = 1.25f;

private Vector2 targetPosition;
private Vector3 targetAngle;
private Vector3 targetScale;

private float fillAmount = 0f;
private bool isAnimating = false;

// Use this for initialization
void Start () {
    randomState();
}

// Update is called once per frame
void Update () {
    if(isAnimating) {
        StartCoroutine(playAnimation());
    }
}

private IEnumerator playAnimation() {
    while(isAnimating && fillAmount < 1f) {
        transform.localPosition = Vector3.MoveTowards(transform.localPosition, targetPosition, fillAmount);
        transform.localEulerAngles = Vector3.MoveTowards(transform.localEulerAngles, targetAngle, fillAmount);
        transform.localScale = Vector3.Lerp(transform.localScale, targetScale, fillAmount);
        fillAmount += 0.008f*Time.deltaTime;
        yield return 0;
    }
    if(isAnimating) {
        isAnimating = false;
    }
}

private void randomState() {
    int x = Random.Range(AXIS_X_MIN, AXIS_X_MAX);
    int y = Random.Range(AXIS_Y_MIN, AXIS_Y_MAX);
    targetPosition = new Vector2(x, y);
    float angle = Random.Range(ANGLE_MIN, ANGLE_MAX);
    targetAngle = new Vector3(0, 0, angle);
    float scale = Random.Range(SCALAR_MIN, SCALAR_MAX);
    targetScale = new Vector3(scale, scale, 1f);
}

public void shuffleCard() {
    if (!isAnimating) {
        randomState();
        fillAmount = 0f;
        isAnimating = true;
    }
}

public void onSelect() {
    if (!isAnimating) {
        targetPosition = new Vector2(0, 0);
        targetAngle = new Vector3(0, 0, 0);
        targetScale = new Vector3(2,2,2);
        transform.SetAsLastSibling();
        fillAmount = 0f;
        isAnimating = true;
    }
}

}

上一篇下一篇

猜你喜欢

热点阅读