简体   繁体   English

将Sprite数组更改为Unity中的GameObject数组

[英]Change Sprite array to GameObject array in Unity

[EDIT]: current inspector output image [编辑]: 当前检查器输出图像

I'm beginning with unity and I want to convert this public array of sprites into a public array of gameobjects. 我从团结开始,我想将这个精灵的公共数组转换成游戏对象的公共数组。 How would I do this? 我该怎么做?

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

public class Obstacle : MonoBehaviour, IRecyle 
{

    public Sprite[] sprites;

    public void Restart()
    {
        var renderer = GetComponent<SpriteRenderer>();
        renderer.sprite = sprites[Random.Range(0, sprites.Length)];        
    }

    public void Shutdown()
    {

    }
}

if you have prefabs with the sprites you want to use try to change your code: 如果您具有要使用的精灵的预制件,请尝试更改代码:

this: 这个:

    public Sprite[] sprites;

to this: 对此:

    public GameObject[] spriteGameObjects;`

and also this: 还有这个:

renderer.sprite = sprites[Random.Range(0, sprites.Length)];

to this: 对此:

renderer.sprite = spritesGameObjects[Random.Range(0, spritesGameObjects.Length)]
.GetComponent<SpriteRenderer>().sprite;

I don't have access to Unity right now, so I haven't checked if it's working, but I think it should. 我现在没有访问Unity的权限,所以我没有检查它是否有效,但是我认为应该可以。 Kindly let me know if this helps or not. 请让我知道是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM