0

我正在尝试循环一个对象列表(广告),以便将它们一个接一个地移动到它们的目标位置(即 Vector3(0,0,0))。问题是它们都同时移动而不是单独移动。不知道我做错了什么,花了几个小时搜索论坛并尝试使用 foreach 循环和 IEnumerators。任何帮助是极大的赞赏。

脚本 1 (AdStartBoard)

//////////////////// Script 1 (AdStartBoard) /////////////////////////////
// This first script works, I just posted it for background information
// Script#2 is below and is the one giving me issues
public List<GameObject> ads = new List<GameObject>();
public List<RectTransform> adRectTransforms = new List<RectTransform>();
public List<SpriteRenderer> adSpriteRenderers = new List<SpriteRenderer>();
public List<Vector3> adStartingPosition = new List<Vector3>();
public List<float> adStartingRotation = new List<float>();
private List<Vector2> adStartingSize = new List<Vector2>();

// At Start, 7 ads will be randmly placed across a canvas background 
private void Start()
        {
            
            DeterminePositionsRow();
            for (int i = 0; i < transform.childCount; i++)
            {
                LeanTween.move(adRectTransforms[i], adStartingPosition[i], timeToStart);
                LeanTween.rotateAround(ads[i], Vector3.forward, adStartingRotation[i], timeToStart);
                adSpriteRenderers[i].size = adStartingSize[i];
                LeanTween.alpha(ads[i], 1f, timeToStart);
            }       
        }

// This function will handle the random positions for the ads 
 public void DeterminePositionsRow()
        {
            int numAdsRowOne = (transform.childCount / 2);
            int numAdsRowTwo = transform.childCount - numAdsRowOne;
            float rowOneGap = (Screen.width * percentageScreenWidthToFill) / numAdsRowOne;
            float rowTwoGap = (Screen.width * percentageScreenWidthToFill) / numAdsRowTwo;

            for (int i = 0; i < transform.childCount; i++)
            {
                if (i < numAdsRowOne)
                {
                    adStartingPosition.Add(new Vector3((i + 1) * rowOneGap - ((rowOneGap * numAdsRowOne + averageAdWidth) / 2f) +       Random.Range(xMin, xMax), Random.Range(rowOneYMin, rowOneYMax), 0f));
                }
                else
                {
                    adStartingPosition.Add(new Vector3((i + 1 - numAdsRowOne) * rowTwoGap - ((rowTwoGap * numAdsRowTwo + averageAdWidth) / 2f) + Random.Range(xMin, xMax), Random.Range(rowTwoYMin, rowTwoYMax), 0f));
                }
                adStartingRotation.Add(Random.Range(rotateMin, rotateMax));
                float newSize = Random.Range(sizeMin, sizeMax);
                adStartingSize.Add(new Vector2(newSize, newSize));
            }
        }

脚本 2 - 广告调整器

//////////////////////// Script 2 ///////////////////////////////////
AdStartBoard adStartBoardScript;
private List<Vector3> adInitalLocations = new List<Vector3>();
private List<float> adInitalRotation = new List<float>();
private List<RectTransform> adRectTransforms = new List<RectTransform>();
private List<GameObject> adObjects = new List<GameObject>();
private RectTransform currentAd;
private RectTransform previousAd;

private void Awake()
        {
            adStartBoardScript = GetComponent<AdStartBoard>();
            adInitalLocations = adStartBoardScript.adStartingPosition;
            adInitalRotation = adStartBoardScript.adStartingRotation;
            adRectTransforms = adStartBoardScript.adRectTransforms;
            adObjects = adStartBoardScript.ads;
            
        }

private void Start()
        {
            //Debug Tools to see if Script 2 gets the correct values from Script 1
            foreach (Vector3 pos in adInitalLocations)
            {
                Debug.Log("Starting Ad Position is: " + pos);
            }
           
            // Here I want to loop thru the Ads List to move the ads to its target Position 
            // one by one, but they all move at the same time instead.
            for(int i = 0; i < adStartBoardScript.ads.Count; i++)
            {
                LeanTween.move(adObjects[i], targetPosition, moveTime);
                // For now there is only 7 ads and I can get them to work if I do it manually...
                // (ad[0], ad[1], etc) but over time I will have dozens of ads I want to 
                // loop thru individually and I am unable to create a proper for loop to do so.
                
                // The main idea is to get the starting position from Script#1 and move the 
                // ad to target position in Script#2
                
                // The target position is center of screen (Vector3(0,0,0))
                
                // Once an individal Ad has moved to its target position, the goal is to have 
                // it move back to its original place from Script#1 (adStartingPosition) 
                // creating an infinite loop of ads rotating to center and back one after another.
                
                // any help is appreciated 
                
            }
        }
4

1 回答 1

1

您在同一帧(在 中Start())触发所有动作。如果您的补间允许延迟,您可以使用那个,否则您必须使用 aCoroutine来触发它们延迟:

private void Start()
{
    StartCoroutine(TriggerMovement(1));
}

private IEnumerator TriggerMovement(float delayPerObject)
{
    var wait = new WaitForSeconds(delayPerObject);
    for(int i = 0; i < adStartBoardScript.ads.Count; i++)
    {
        LeanTween.move(adObjects[i], targetPosition, moveTime);
        yield return wait;
    }
}

但是,如果您想以乒乓方式移动它们,您可以查看Mathf.PingPong。不要使用 tweener,而只需将控制广告移动的自定义脚本添加到每个广告,并让它们执行类似transform.position = Vector3.Lerp(startPos, targetPos, Mathf.PingPong(Time.time + delay, 1)).

于 2020-11-20T07:34:19.763 回答