【问题标题】:How to programatically put same sprite mutiple times in unity?如何以编程方式将相同的精灵多次统一?
【发布时间】:2014-09-18 22:19:53
【问题描述】:

我正在尝试仅使用一个精灵在我的场景中生成多个精灵。当我尝试使用精灵数组和精灵渲染器时,我发现它很困难,因为在我的场景中我只看到一个精灵。我还想在 Unity 场景的不同区域显示精灵。

主要代码:

using UnityEngine;
using System.Collections;

public class SampleCode : MonoBehaviour {

    float posX = -4.5F;
    float posY = -0.65F;

    public IEnumerator Start(){
        for(int i = 0; i < 5; i++){
            posX = posX + 0.65F;

            if(posX > 3.5F){
                for(float x = 0.50F; x < 2.5F; x = x + 0.50F){
                    posY = -1.5F + x;
                    posX = -4.15F + x;;
                }
            }

        string path = "file:///C:/Users/Pankaj Sharma/Documents/untitled.bmp";
            SpriteRenderer rend = this.GetComponent<SpriteRenderer>();
            Sprite sprite = new Sprite();
            WWW www = new WWW(path);
            yield return www;
            sprite = Sprite.Create(www.texture, new Rect(0, 0, 50, 50),new Vector2(posX, posY),100.0f);
            rend.sprite = sprite;
        }
    }   
}

【问题讨论】:

    标签: c# unity3d sprite


    【解决方案1】:

    你只有一个SpriteRenderer,所以你只能看到一个精灵。

    如果您的场景中有多个带有SpriteRenderer 组件的对象,您可以将您的sprite 设置为GetComponent&lt;SpriteRenderer&gt;().sprite,以获得您想要在场景中使用的对象。

    这对你有意义吗?

    【讨论】:

      猜你喜欢
      • 2017-06-09
      • 2014-04-23
      • 1970-01-01
      • 1970-01-01
      • 2014-04-21
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多